Search in sources :

Example 86 with ClientConfig

use of com.qcloud.cos.ClientConfig in project cos-java-sdk-v5 by tencentyun.

the class MultipartUploadDemo method listPartDemo.

// list part用于获取已上传的分片, 如果已上传的分片数量较多, 需要循环多次调用list part获取已上传的所有的分片
public static List<PartETag> listPartDemo(String uploadId) {
    // 1 初始化用户身份信息(secretId, secretKey)
    COSCredentials cred = new BasicCOSCredentials("AKIDXXXXXXXX", "1A2Z3YYYYYYYYYY");
    // 2 设置bucket的区域, COS地域的简称请参照 https://www.qcloud.com/document/product/436/6224
    ClientConfig clientConfig = new ClientConfig(new Region("ap-guangzhou"));
    // 3 生成cos客户端
    COSClient cosclient = new COSClient(cred, clientConfig);
    // bucket名需包含appid
    String bucketName = "mybucket-1251668577";
    String key = "aaa/bbb.txt";
    // uploadid(通过initiateMultipartUpload或者ListMultipartUploads获取)
    // 用于保存已上传的分片信息
    List<PartETag> partETags = new LinkedList<>();
    PartListing partListing = null;
    ListPartsRequest listPartsRequest = new ListPartsRequest(bucketName, key, uploadId);
    do {
        try {
            partListing = cosclient.listParts(listPartsRequest);
        } catch (CosServiceException e) {
            throw e;
        } catch (CosClientException e) {
            throw e;
        }
        for (PartSummary partSummary : partListing.getParts()) {
            partETags.add(new PartETag(partSummary.getPartNumber(), partSummary.getETag()));
        }
        listPartsRequest.setPartNumberMarker(partListing.getNextPartNumberMarker());
    } while (partListing.isTruncated());
    cosclient.shutdown();
    return partETags;
}
Also used : COSCredentials(com.qcloud.cos.auth.COSCredentials) BasicCOSCredentials(com.qcloud.cos.auth.BasicCOSCredentials) BasicCOSCredentials(com.qcloud.cos.auth.BasicCOSCredentials) CosClientException(com.qcloud.cos.exception.CosClientException) PartETag(com.qcloud.cos.model.PartETag) LinkedList(java.util.LinkedList) PartListing(com.qcloud.cos.model.PartListing) COSClient(com.qcloud.cos.COSClient) ListPartsRequest(com.qcloud.cos.model.ListPartsRequest) CosServiceException(com.qcloud.cos.exception.CosServiceException) Region(com.qcloud.cos.region.Region) ClientConfig(com.qcloud.cos.ClientConfig) PartSummary(com.qcloud.cos.model.PartSummary)

Example 87 with ClientConfig

use of com.qcloud.cos.ClientConfig in project cos-java-sdk-v5 by tencentyun.

the class MultipartUploadDemo method abortPartUploadDemo.

// 终止分块上传
public static void abortPartUploadDemo(String uploadId) {
    // 1 初始化用户身份信息(secretId, secretKey)
    COSCredentials cred = new BasicCOSCredentials("AKIDXXXXXXXX", "1A2Z3YYYYYYYYYY");
    // 2 设置bucket的区域, COS地域的简称请参照 https://www.qcloud.com/document/product/436/6224
    ClientConfig clientConfig = new ClientConfig(new Region("ap-guangzhou"));
    // 3 生成cos客户端
    COSClient cosclient = new COSClient(cred, clientConfig);
    // bucket名需包含appid
    String bucketName = "mybucket-1251668577";
    String key = "aaa/bbb.txt";
    // uploadid(通过initiateMultipartUpload或者ListMultipartUploads获取)
    AbortMultipartUploadRequest abortMultipartUploadRequest = new AbortMultipartUploadRequest(bucketName, key, uploadId);
    try {
        cosclient.abortMultipartUpload(abortMultipartUploadRequest);
    } catch (CosServiceException e) {
        e.printStackTrace();
    } catch (CosClientException e) {
        e.printStackTrace();
    }
    cosclient.shutdown();
}
Also used : COSClient(com.qcloud.cos.COSClient) COSCredentials(com.qcloud.cos.auth.COSCredentials) BasicCOSCredentials(com.qcloud.cos.auth.BasicCOSCredentials) CosServiceException(com.qcloud.cos.exception.CosServiceException) BasicCOSCredentials(com.qcloud.cos.auth.BasicCOSCredentials) CosClientException(com.qcloud.cos.exception.CosClientException) Region(com.qcloud.cos.region.Region) AbortMultipartUploadRequest(com.qcloud.cos.model.AbortMultipartUploadRequest) ClientConfig(com.qcloud.cos.ClientConfig)

Example 88 with ClientConfig

use of com.qcloud.cos.ClientConfig in project RuoYi-Vue-Plus by JavaLionLi.

the class QcloudOssStrategy method init.

@Override
public void init(OssProperties ossProperties) {
    super.init(ossProperties);
    try {
        COSCredentials credentials = new BasicCOSCredentials(properties.getAccessKey(), properties.getSecretKey());
        // 初始化客户端配置
        ClientConfig clientConfig = new ClientConfig();
        // 设置bucket所在的区域,华南:gz 华北:tj 华东:sh
        clientConfig.setRegion(new Region(properties.getRegion()));
        if (OssConstant.IS_HTTPS.equals(properties.getIsHttps())) {
            clientConfig.setHttpProtocol(HttpProtocol.https);
        } else {
            clientConfig.setHttpProtocol(HttpProtocol.http);
        }
        client = new COSClient(credentials, clientConfig);
        createBucket();
    } catch (Exception e) {
        throw new OssException("腾讯云存储配置错误! 请检查系统配置:[" + e.getMessage() + "]");
    }
    isInit = true;
}
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) OssException(com.ruoyi.oss.exception.OssException) OssException(com.ruoyi.oss.exception.OssException)

Example 89 with ClientConfig

use of com.qcloud.cos.ClientConfig in project Ant-Live by PinTeh.

the class TencentConfig method getCosClient.

@Bean
public COSClient getCosClient() {
    // 1 初始化用户身份信息(secretId, secretKey)。
    COSCredentials cred = new BasicCOSCredentials(iii, kkk);
    // 2 设置 bucket 的区域, COS 地域的简称请参照 https://cloud.tencent.com/document/product/436/6224
    // clientConfig 中包含了设置 region, https(默认 http), 超时, 代理等 set 方法, 使用可参见源码或者常见问题 Java SDK 部分。
    Region region = new Region("ap-chengdu");
    ClientConfig clientConfig = new ClientConfig(region);
    // 3 生成 cos 客户端。
    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) Bean(org.springframework.context.annotation.Bean)

Example 90 with ClientConfig

use of com.qcloud.cos.ClientConfig in project RuoYi-Flowable-Plus by KonBAI-Q.

the class QcloudOssStrategy method init.

@Override
public void init(OssProperties ossProperties) {
    super.init(ossProperties);
    try {
        COSCredentials credentials = new BasicCOSCredentials(properties.getAccessKey(), properties.getSecretKey());
        // 初始化客户端配置
        ClientConfig clientConfig = new ClientConfig();
        // 设置bucket所在的区域,华南:gz 华北:tj 华东:sh
        clientConfig.setRegion(new Region(properties.getRegion()));
        if (OssConstant.IS_HTTPS.equals(properties.getIsHttps())) {
            clientConfig.setHttpProtocol(HttpProtocol.https);
        } else {
            clientConfig.setHttpProtocol(HttpProtocol.http);
        }
        client = new COSClient(credentials, clientConfig);
        createBucket();
    } catch (Exception e) {
        throw new OssException("腾讯云存储配置错误! 请检查系统配置:[" + e.getMessage() + "]");
    }
    isInit = true;
}
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) OssException(com.ruoyi.oss.exception.OssException) OssException(com.ruoyi.oss.exception.OssException)

Aggregations

ClientConfig (com.qcloud.cos.ClientConfig)107 COSClient (com.qcloud.cos.COSClient)103 Region (com.qcloud.cos.region.Region)103 COSCredentials (com.qcloud.cos.auth.COSCredentials)99 BasicCOSCredentials (com.qcloud.cos.auth.BasicCOSCredentials)96 CosClientException (com.qcloud.cos.exception.CosClientException)40 CosServiceException (com.qcloud.cos.exception.CosServiceException)38 File (java.io.File)22 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 FileOperationException (run.halo.app.exception.FileOperationException)6 IOException (java.io.IOException)5 CopyResult (com.qcloud.cos.model.CopyResult)4