Search in sources :

Example 1 with ListPartsRequest

use of com.qcloud.cos.model.ListPartsRequest 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 2 with ListPartsRequest

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

the class AbstractCOSClientTest method testListMultipart.

protected List<PartETag> testListMultipart(String key, String uploadId, int expectedPartNum, List<String> originDataMd5Array) {
    List<PartETag> partETags = new LinkedList<>();
    PartListing partListing = null;
    ListPartsRequest listPartsRequest = new ListPartsRequest(bucket, key, uploadId);
    int currentPartNum = 0;
    do {
        partListing = cosclient.listParts(listPartsRequest);
        for (PartSummary partSummary : partListing.getParts()) {
            ++currentPartNum;
            assertEquals(currentPartNum, partSummary.getPartNumber());
            if (useClientEncryption) {
                assertEquals(false, partSummary.getETag().equals(originDataMd5Array.get(currentPartNum - 1)));
            } else {
                assertEquals(true, partSummary.getETag().equals(originDataMd5Array.get(currentPartNum - 1)));
            }
            partETags.add(new PartETag(partSummary.getPartNumber(), partSummary.getETag()));
        }
        listPartsRequest.setPartNumberMarker(partListing.getNextPartNumberMarker());
    } while (partListing.isTruncated());
    assertEquals(expectedPartNum, currentPartNum);
    return partETags;
}
Also used : ListPartsRequest(com.qcloud.cos.model.ListPartsRequest) PartETag(com.qcloud.cos.model.PartETag) LinkedList(java.util.LinkedList) PartListing(com.qcloud.cos.model.PartListing) PartSummary(com.qcloud.cos.model.PartSummary)

Example 3 with ListPartsRequest

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

the class UploadCallable method identifyExistingPartsForResume.

private Map<Integer, PartSummary> identifyExistingPartsForResume(String uploadId) {
    Map<Integer, PartSummary> partNumbers = new HashMap<Integer, PartSummary>();
    if (uploadId == null) {
        return partNumbers;
    }
    int partNumber = 0;
    while (true) {
        ListPartsRequest listPartsRequest = new ListPartsRequest(origReq.getBucketName(), origReq.getKey(), uploadId).withPartNumberMarker(partNumber);
        TransferManagerUtils.populateEndpointAddr(origReq, listPartsRequest);
        PartListing parts = cos.listParts(listPartsRequest);
        for (PartSummary partSummary : parts.getParts()) {
            partNumbers.put(partSummary.getPartNumber(), partSummary);
        }
        if (!parts.isTruncated()) {
            return partNumbers;
        }
        partNumber = parts.getNextPartNumberMarker();
    }
}
Also used : ListPartsRequest(com.qcloud.cos.model.ListPartsRequest) HashMap(java.util.HashMap) PartSummary(com.qcloud.cos.model.PartSummary) PartListing(com.qcloud.cos.model.PartListing)

Aggregations

ListPartsRequest (com.qcloud.cos.model.ListPartsRequest)3 PartListing (com.qcloud.cos.model.PartListing)3 PartSummary (com.qcloud.cos.model.PartSummary)3 PartETag (com.qcloud.cos.model.PartETag)2 LinkedList (java.util.LinkedList)2 COSClient (com.qcloud.cos.COSClient)1 ClientConfig (com.qcloud.cos.ClientConfig)1 BasicCOSCredentials (com.qcloud.cos.auth.BasicCOSCredentials)1 COSCredentials (com.qcloud.cos.auth.COSCredentials)1 CosClientException (com.qcloud.cos.exception.CosClientException)1 CosServiceException (com.qcloud.cos.exception.CosServiceException)1 Region (com.qcloud.cos.region.Region)1 HashMap (java.util.HashMap)1