Search in sources :

Example 1 with ClientConfig

use of com.qcloud.cos.ClientConfig in project alluxio by Alluxio.

the class COSUnderFileSystem method createInstance.

/**
 * Constructs a new instance of {@link COSUnderFileSystem}.
 *
 * @param uri the {@link AlluxioURI} for this UFS
 * @param conf the configuration for this UFS
 * @return the created {@link COSUnderFileSystem} instance
 */
public static COSUnderFileSystem createInstance(AlluxioURI uri, UnderFileSystemConfiguration conf) throws Exception {
    String bucketName = UnderFileSystemUtils.getBucketName(uri);
    Preconditions.checkArgument(conf.isSet(PropertyKey.COS_ACCESS_KEY), "Property %s is required to connect to COS", PropertyKey.COS_ACCESS_KEY);
    Preconditions.checkArgument(conf.isSet(PropertyKey.COS_SECRET_KEY), "Property %s is required to connect to COS", PropertyKey.COS_SECRET_KEY);
    Preconditions.checkArgument(conf.isSet(PropertyKey.COS_REGION), "Property %s is required to connect to COS", PropertyKey.COS_REGION);
    Preconditions.checkArgument(conf.isSet(PropertyKey.COS_APP_ID), "Property %s is required to connect to COS", PropertyKey.COS_APP_ID);
    String accessKey = conf.getString(PropertyKey.COS_ACCESS_KEY);
    String secretKey = conf.getString(PropertyKey.COS_SECRET_KEY);
    String regionName = conf.getString(PropertyKey.COS_REGION);
    String appId = conf.getString(PropertyKey.COS_APP_ID);
    COSCredentials cred = new BasicCOSCredentials(accessKey, secretKey);
    ClientConfig clientConfig = createCOSClientConfig(regionName, conf);
    COSClient client = new COSClient(cred, clientConfig);
    return new COSUnderFileSystem(uri, client, bucketName, appId, conf);
}
Also used : COSClient(com.qcloud.cos.COSClient) COSCredentials(com.qcloud.cos.auth.COSCredentials) BasicCOSCredentials(com.qcloud.cos.auth.BasicCOSCredentials) BasicCOSCredentials(com.qcloud.cos.auth.BasicCOSCredentials) ClientConfig(com.qcloud.cos.ClientConfig)

Example 2 with ClientConfig

use of com.qcloud.cos.ClientConfig in project xian by happyyangyuan.

the class AbsFileOperate method getCosClient.

public COSClient getCosClient() {
    if (cosClient == null) {
        cosClient = new COSClient(10053621, "AKID5iJcsYewRYIJhqQsoaLQ7Ks1XIO6eYPs", "Gm0nqHOPzUG1MRRJnBLX4UwwQMoh8v4y");
        ClientConfig config = new ClientConfig();
        config.setRegion("sh");
        cosClient.setConfig(config);
    }
    return cosClient;
}
Also used : COSClient(com.qcloud.cos.COSClient) ClientConfig(com.qcloud.cos.ClientConfig)

Example 3 with ClientConfig

use of com.qcloud.cos.ClientConfig in project halo by ruibaby.

the class TencentCosFileHandler method delete.

@Override
public void delete(String key) {
    Assert.notNull(key, "File key must not be blank");
    // Get config
    String region = optionService.getByPropertyOfNonNull(TencentCosProperties.COS_REGION).toString();
    String secretId = optionService.getByPropertyOfNonNull(TencentCosProperties.COS_SECRET_ID).toString();
    String secretKey = optionService.getByPropertyOfNonNull(TencentCosProperties.COS_SECRET_KEY).toString();
    String bucketName = optionService.getByPropertyOfNonNull(TencentCosProperties.COS_BUCKET_NAME).toString();
    COSCredentials cred = new BasicCOSCredentials(secretId, secretKey);
    Region regionConfig = new Region(region);
    ClientConfig clientConfig = new ClientConfig(regionConfig);
    // Init OSS client
    COSClient cosClient = new COSClient(cred, clientConfig);
    try {
        cosClient.deleteObject(bucketName, key);
    } catch (Exception e) {
        throw new FileOperationException("附件 " + key + " 从腾讯云删除失败", e);
    } finally {
        cosClient.shutdown();
    }
}
Also used : COSClient(com.qcloud.cos.COSClient) COSCredentials(com.qcloud.cos.auth.COSCredentials) BasicCOSCredentials(com.qcloud.cos.auth.BasicCOSCredentials) BasicCOSCredentials(com.qcloud.cos.auth.BasicCOSCredentials) FileOperationException(run.halo.app.exception.FileOperationException) Region(com.qcloud.cos.region.Region) ClientConfig(com.qcloud.cos.ClientConfig) FileOperationException(run.halo.app.exception.FileOperationException)

Example 4 with ClientConfig

use of com.qcloud.cos.ClientConfig in project litemall by linlinjava.

the class TencentStorage method getCOSClient.

private COSClient getCOSClient() {
    if (cosClient == null) {
        // 1 初始化用户身份信息(secretId, secretKey)
        COSCredentials cred = new BasicCOSCredentials(secretId, secretKey);
        // 2 设置bucket的区域, COS地域的简称请参照 https://cloud.tencent.com/document/product/436/6224
        ClientConfig clientConfig = new ClientConfig(new Region(region));
        cosClient = new COSClient(cred, clientConfig);
    }
    return cosClient;
}
Also used : COSClient(com.qcloud.cos.COSClient) COSCredentials(com.qcloud.cos.auth.COSCredentials) BasicCOSCredentials(com.qcloud.cos.auth.BasicCOSCredentials) BasicCOSCredentials(com.qcloud.cos.auth.BasicCOSCredentials) Region(com.qcloud.cos.region.Region) ClientConfig(com.qcloud.cos.ClientConfig)

Example 5 with ClientConfig

use of com.qcloud.cos.ClientConfig in project e3mall by colg-cloud.

the class TencentCosTest method testName.

@Test
public void testName() {
    COSCredentials cred = new BasicCOSCredentials("AKIDUvjV6VEvvjS4Vliw360iEYpvpgMrqMKF", "sY3NVSL8kLk8KK8lIftzud9VggU5Vkne");
    // 采用了新的region名字,可用region的列表可以在官网文档中获取,也可以参考下面的XML SDK和JSON SDK的地域对照表
    ClientConfig clientConfig = new ClientConfig(new Region("ap-guangzhou"));
    COSClient cosclient = new COSClient(cred, clientConfig);
    // bucket的名字需要的包含appId
    String bucketName = "colg-1256242877";
    // 上传文件
    File localFile = new File(PROJECT_PATH + "\\src\\test\\resources\\images\\FastDfs架构.png");
    String key = "colg.png";
    PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, key, localFile);
    // 设置存储类型, 默认是标准(Standard), 低频(standard_ia)
    putObjectRequest.setStorageClass(StorageClass.Standard);
    PutObjectResult putObjectResult = cosclient.putObject(putObjectRequest);
    // putObjectResult会返回文件的etag
    String etag = putObjectResult.getETag();
    log.info("etag: {}", etag);
    // 关闭客户端
    cosclient.shutdown();
}
Also used : COSClient(com.qcloud.cos.COSClient) COSCredentials(com.qcloud.cos.auth.COSCredentials) BasicCOSCredentials(com.qcloud.cos.auth.BasicCOSCredentials) BasicCOSCredentials(com.qcloud.cos.auth.BasicCOSCredentials) PutObjectResult(com.qcloud.cos.model.PutObjectResult) Region(com.qcloud.cos.region.Region) ClientConfig(com.qcloud.cos.ClientConfig) File(java.io.File) PutObjectRequest(com.qcloud.cos.model.PutObjectRequest) BaseTest(cn.e3mall.common.tencent.cos.BaseTest) Test(org.junit.Test)

Aggregations

ClientConfig (com.qcloud.cos.ClientConfig)101 Region (com.qcloud.cos.region.Region)98 COSClient (com.qcloud.cos.COSClient)97 COSCredentials (com.qcloud.cos.auth.COSCredentials)94 BasicCOSCredentials (com.qcloud.cos.auth.BasicCOSCredentials)91 CosClientException (com.qcloud.cos.exception.CosClientException)39 CosServiceException (com.qcloud.cos.exception.CosServiceException)37 File (java.io.File)23 ObjectMetadata (com.qcloud.cos.model.ObjectMetadata)14 PutObjectResult (com.qcloud.cos.model.PutObjectResult)14 PutObjectRequest (com.qcloud.cos.model.PutObjectRequest)13 TransferManager (com.qcloud.cos.transfer.TransferManager)13 ExecutorService (java.util.concurrent.ExecutorService)13 AnonymousCOSCredentials (com.qcloud.cos.auth.AnonymousCOSCredentials)7 CopyObjectRequest (com.qcloud.cos.model.CopyObjectRequest)7 LinkedList (java.util.LinkedList)7 GetObjectRequest (com.qcloud.cos.model.GetObjectRequest)6 IOException (java.io.IOException)6 FileOperationException (run.halo.app.exception.FileOperationException)6 CopyResult (com.qcloud.cos.model.CopyResult)4