Search in sources :

Example 11 with PartETag

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

the class MultipartUploadDemo method UploadPartDemo.

// 分块上传(上传某一个分片的数据)
public static List<PartETag> UploadPartDemo(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获取)
    boolean userTrafficLimit = false;
    List<PartETag> partETags = new LinkedList<>();
    // 生成要上传的数据, 这里初始化一个10M的数据
    for (int i = 0; i < 10; i++) {
        byte[] data = new byte[1024 * 1024];
        UploadPartRequest uploadPartRequest = new UploadPartRequest();
        uploadPartRequest.setBucketName(bucketName);
        uploadPartRequest.setKey(key);
        uploadPartRequest.setUploadId(uploadId);
        // 设置分块的数据来源输入流
        uploadPartRequest.setInputStream(new ByteArrayInputStream(data));
        // 设置分块的长度
        // 设置数据长度
        uploadPartRequest.setPartSize(data.length);
        // 假设要上传的part编号是10
        uploadPartRequest.setPartNumber(i + 1);
        if (userTrafficLimit) {
            uploadPartRequest.setTrafficLimit(8 * 1024 * 1024);
        }
        try {
            UploadPartResult uploadPartResult = cosclient.uploadPart(uploadPartRequest);
            PartETag partETag = uploadPartResult.getPartETag();
            partETags.add(partETag);
            String crc64 = uploadPartResult.getCrc64Ecma();
        } catch (CosServiceException e) {
            throw e;
        } catch (CosClientException e) {
            throw e;
        }
    }
    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) UploadPartRequest(com.qcloud.cos.model.UploadPartRequest) PartETag(com.qcloud.cos.model.PartETag) LinkedList(java.util.LinkedList) COSClient(com.qcloud.cos.COSClient) UploadPartResult(com.qcloud.cos.model.UploadPartResult) CosServiceException(com.qcloud.cos.exception.CosServiceException) ByteArrayInputStream(java.io.ByteArrayInputStream) Region(com.qcloud.cos.region.Region) ClientConfig(com.qcloud.cos.ClientConfig)

Example 12 with PartETag

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

the class MultipartUploadDemo method copyPartUploadDemo.

// 分块copy, 表示该块的数据来自另外一个文件的某一范围, 支持跨园区, 跨bucket copy
public static void copyPartUploadDemo(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";
    CopyPartRequest copyPartRequest = new CopyPartRequest();
    // 要拷贝的源文件所在的region
    copyPartRequest.setSourceBucketRegion(new Region("ap-guangzhou"));
    // 要拷贝的源文件的bucket名称
    copyPartRequest.setSourceBucketName(bucketName);
    // 要拷贝的源文件的路径
    copyPartRequest.setSourceKey("aaa/ccc.txt");
    // 指定要拷贝的源文件的数据范围(类似content-range)
    copyPartRequest.setFirstByte(0L);
    copyPartRequest.setLastByte(1048575L);
    // 目的bucket名称
    copyPartRequest.setDestinationBucketName(bucketName);
    // 目的路径名称
    copyPartRequest.setDestinationKey(key);
    copyPartRequest.setPartNumber(1);
    // uploadId
    copyPartRequest.setUploadId(uploadId);
    try {
        CopyPartResult copyPartResult = cosclient.copyPart(copyPartRequest);
        PartETag partETag = copyPartResult.getPartETag();
    } catch (CosServiceException e) {
        e.printStackTrace();
    } catch (CosClientException e) {
        e.printStackTrace();
    }
    cosclient.shutdown();
}
Also used : COSClient(com.qcloud.cos.COSClient) CopyPartRequest(com.qcloud.cos.model.CopyPartRequest) 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) ClientConfig(com.qcloud.cos.ClientConfig) CopyPartResult(com.qcloud.cos.model.CopyPartResult) PartETag(com.qcloud.cos.model.PartETag)

Example 13 with PartETag

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

use of com.qcloud.cos.model.PartETag in project flink-cos-fs by tencentyun.

the class RecoverableMultipartUploadImpl method awaitPendingPartsUpload.

private void awaitPendingPartsUpload() throws IOException {
    checkState(this.currentUploadInfo.getRemainingParts() == this.uploadsInProgress.size());
    while (this.currentUploadInfo.getRemainingParts() > 0) {
        CompletableFuture<PartETag> next = this.uploadsInProgress.peekFirst();
        PartETag nextPart = awaitPendingPartUploadToComplete(next);
        this.currentUploadInfo.registerCompletePart(nextPart);
        this.uploadsInProgress.removeFirst();
    }
}
Also used : PartETag(com.qcloud.cos.model.PartETag)

Example 15 with PartETag

use of com.qcloud.cos.model.PartETag in project flink-cos-fs by tencentyun.

the class COSRecoverableSerializer method deserializeV1.

private static COSRecoverable deserializeV1(byte[] serialized) throws IOException {
    final ByteBuffer bb = ByteBuffer.wrap(serialized).order(ByteOrder.LITTLE_ENDIAN);
    if (bb.getInt() != MAGIC_NUMBER) {
        throw new IOException("Corrupt data: Unexpected magic number.");
    }
    final byte[] keyBytes = new byte[bb.getInt()];
    bb.get(keyBytes);
    final byte[] uploadIdBytes = new byte[bb.getInt()];
    bb.get(uploadIdBytes);
    final int numParts = bb.getInt();
    final ArrayList<PartETag> parts = new ArrayList<>(numParts);
    for (int i = 0; i < numParts; i++) {
        final int partNum = bb.getInt();
        final byte[] buffer = new byte[bb.getInt()];
        bb.get(buffer);
        parts.add(new PartETag(partNum, new String(buffer, CHARSET)));
    }
    final long numBytes = bb.getLong();
    final String lastPart;
    final int lastObjectArraySize = bb.getInt();
    if (lastObjectArraySize == 0) {
        lastPart = null;
    } else {
        byte[] lastPartBytes = new byte[lastObjectArraySize];
        bb.get(lastPartBytes);
        lastPart = new String(lastPartBytes, CHARSET);
    }
    final long lastPartLength = bb.getLong();
    return new COSRecoverable(new String(uploadIdBytes, CHARSET), new String(keyBytes, CHARSET), parts, numBytes, lastPart, lastPartLength);
}
Also used : ArrayList(java.util.ArrayList) IOException(java.io.IOException) ByteBuffer(java.nio.ByteBuffer) PartETag(com.qcloud.cos.model.PartETag)

Aggregations

PartETag (com.qcloud.cos.model.PartETag)20 LinkedList (java.util.LinkedList)8 UploadPartRequest (com.qcloud.cos.model.UploadPartRequest)7 CosClientException (com.qcloud.cos.exception.CosClientException)6 CosServiceException (com.qcloud.cos.exception.CosServiceException)6 CompleteMultipartUploadRequest (com.qcloud.cos.model.CompleteMultipartUploadRequest)6 CompleteMultipartUploadResult (com.qcloud.cos.model.CompleteMultipartUploadResult)6 UploadPartResult (com.qcloud.cos.model.UploadPartResult)5 IOException (java.io.IOException)5 COSClient (com.qcloud.cos.COSClient)4 ClientConfig (com.qcloud.cos.ClientConfig)4 BasicCOSCredentials (com.qcloud.cos.auth.BasicCOSCredentials)4 COSCredentials (com.qcloud.cos.auth.COSCredentials)4 InitiateMultipartUploadRequest (com.qcloud.cos.model.InitiateMultipartUploadRequest)4 InitiateMultipartUploadResult (com.qcloud.cos.model.InitiateMultipartUploadResult)4 Region (com.qcloud.cos.region.Region)4 File (java.io.File)4 PartSummary (com.qcloud.cos.model.PartSummary)3 CIObject (com.qcloud.cos.model.ciModel.persistence.CIObject)3 CIUploadResult (com.qcloud.cos.model.ciModel.persistence.CIUploadResult)3