Search in sources :

Example 71 with COSClient

use of com.qcloud.cos.COSClient in project mall by wangxl-git.

the class QcloudCloudStorageService method init.

private void init() {
    Credentials credentials = new Credentials(config.getQcloudAppId(), config.getQcloudSecretId(), config.getQcloudSecretKey());
    // 初始化客户端配置
    ClientConfig clientConfig = new ClientConfig();
    // 设置bucket所在的区域,华南:gz 华北:tj 华东:sh
    clientConfig.setRegion(config.getQcloudRegion());
    client = new COSClient(clientConfig, credentials);
}
Also used : COSClient(com.qcloud.cos.COSClient) ClientConfig(com.qcloud.cos.ClientConfig) Credentials(com.qcloud.cos.sign.Credentials)

Example 72 with COSClient

use of com.qcloud.cos.COSClient 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 73 with COSClient

use of com.qcloud.cos.COSClient in project yili-music by programmer-yili.

the class CosStorageServiceImpl method createCOSClient.

// Todo: 改造client获取方法
COSClient createCOSClient() {
    // 这里需要已经获取到临时密钥的结果。
    // 临时密钥的生成参考 https://cloud.tencent.com/document/product/436/14048#cos-sts-sdk
    COSCredentials cred = new BasicCOSCredentials(secretId, secretKey);
    // ClientConfig 中包含了后续请求 COS 的客户端设置:
    ClientConfig clientConfig = new ClientConfig();
    // 设置 bucket 的地域
    // COS_REGION 请参照 https://cloud.tencent.com/document/product/436/6224
    clientConfig.setRegion(new Region(region));
    // 设置请求协议, http 或者 https
    // 5.6.53 及更低的版本,建议设置使用 https 协议
    // 5.6.54 及更高版本,默认使用了 https
    clientConfig.setHttpProtocol(HttpProtocol.https);
    // 以下的设置,是可选的:
    // 设置 socket 读取超时,默认 30s
    clientConfig.setSocketTimeout(30 * 1000);
    // 设置建立连接超时,默认 30s
    clientConfig.setConnectionTimeout(30 * 1000);
    return new COSClient(cred, clientConfig);
}
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 74 with COSClient

use of com.qcloud.cos.COSClient in project yili-music by programmer-yili.

the class CosStorageServiceImpl method getFileUri.

@Override
public String getFileUri(String fileKey) {
    COSClient cosClient = createCOSClient();
    Date expirationDate = new Date(System.currentTimeMillis() + 30 * 60 * 1000);
    Map<String, String> params = new HashMap<String, String>();
    Map<String, String> headers = new HashMap<String, String>();
    HttpMethodName method = HttpMethodName.GET;
    URL url = cosClient.generatePresignedUrl(bucket, fileKey, expirationDate, method, headers, params);
    cosClient.shutdown();
    return url.toString();
}
Also used : COSClient(com.qcloud.cos.COSClient) HttpMethodName(com.qcloud.cos.http.HttpMethodName) HashMap(java.util.HashMap) Date(java.util.Date) URL(java.net.URL)

Example 75 with COSClient

use of com.qcloud.cos.COSClient 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

COSClient (com.qcloud.cos.COSClient)142 ClientConfig (com.qcloud.cos.ClientConfig)103 Region (com.qcloud.cos.region.Region)99 COSCredentials (com.qcloud.cos.auth.COSCredentials)96 BasicCOSCredentials (com.qcloud.cos.auth.BasicCOSCredentials)93 CosClientException (com.qcloud.cos.exception.CosClientException)38 CosServiceException (com.qcloud.cos.exception.CosServiceException)38 File (java.io.File)22 ObjectMetadata (com.qcloud.cos.model.ObjectMetadata)15 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 URL (java.net.URL)6 FileOperationException (run.halo.app.exception.FileOperationException)6 Date (java.util.Date)5