Search in sources :

Example 16 with PartETag

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

the class COSRecoverableSerializer method serialize.

@Override
public byte[] serialize(COSRecoverable obj) throws IOException {
    final List<PartETag> partList = obj.getPartETags();
    final PartETag[] parts = partList.toArray(new PartETag[partList.size()]);
    final byte[] keyBytes = obj.getObjectName().getBytes(CHARSET);
    final byte[] uploadIdBytes = obj.getUploadId().getBytes(CHARSET);
    final byte[][] etags = new byte[parts.length][];
    int partEtagBytes = 0;
    for (int i = 0; i < parts.length; i++) {
        etags[i] = parts[i].getETag().getBytes(CHARSET);
        partEtagBytes += etags[i].length + 2 * Integer.BYTES;
    }
    final String lastObjectKey = obj.getInCompleteObjectName();
    final byte[] lastPartBytes = lastObjectKey == null ? null : lastObjectKey.getBytes(CHARSET);
    final byte[] targetBytes = new byte[Integer.BYTES + // magic number
    Integer.BYTES + keyBytes.length + Integer.BYTES + uploadIdBytes.length + Integer.BYTES + partEtagBytes + Long.BYTES + Integer.BYTES + (lastPartBytes == null ? 0 : lastPartBytes.length) + Long.BYTES];
    ByteBuffer bb = ByteBuffer.wrap(targetBytes).order(ByteOrder.LITTLE_ENDIAN);
    bb.putInt(MAGIC_NUMBER);
    bb.putInt(keyBytes.length);
    bb.put(keyBytes);
    bb.putInt(uploadIdBytes.length);
    bb.put(uploadIdBytes);
    bb.putInt(etags.length);
    for (int i = 0; i < parts.length; i++) {
        PartETag pe = parts[i];
        bb.putInt(pe.getPartNumber());
        bb.putInt(etags[i].length);
        bb.put(etags[i]);
    }
    bb.putLong(obj.getNumBytesInParts());
    if (lastPartBytes == null) {
        bb.putInt(0);
    } else {
        bb.putInt(lastPartBytes.length);
        bb.put(lastPartBytes);
    }
    bb.putLong(obj.getInCompleteObjectLength());
    return targetBytes;
}
Also used : ByteBuffer(java.nio.ByteBuffer) PartETag(com.qcloud.cos.model.PartETag)

Example 17 with PartETag

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

the class RequestXmlFactory method convertToXmlByteArray.

/**
 * Converts the specified list of PartETags to an XML fragment that can be sent to the
 * CompleteMultipartUpload operation of Qcloud COS.
 *
 * @param partETags The list of part ETags containing the data to include in the new XML
 *                  fragment.
 * @return A byte array containing the data
 */
public static byte[] convertToXmlByteArray(List<PartETag> partETags) {
    XmlWriter xml = new XmlWriter();
    xml.start("CompleteMultipartUpload");
    if (partETags != null) {
        Collections.sort(partETags, new Comparator<PartETag>() {

            public int compare(PartETag tag1, PartETag tag2) {
                if (tag1.getPartNumber() < tag2.getPartNumber())
                    return -1;
                if (tag1.getPartNumber() > tag2.getPartNumber())
                    return 1;
                return 0;
            }
        });
        for (PartETag partEtag : partETags) {
            xml.start("Part");
            xml.start("PartNumber").value(Integer.toString(partEtag.getPartNumber())).end();
            xml.start("ETag").value(partEtag.getETag()).end();
            xml.end();
        }
    }
    xml.end();
    return xml.getBytes();
}
Also used : PartETag(com.qcloud.cos.model.PartETag)

Example 18 with PartETag

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

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

the class AbstractCOSClientTest method testGetEachPart.

protected void testGetEachPart(String key, long partSize, long fileSize, List<PartETag> partETags, List<String> originDataMd5Array) throws IOException {
    File downloadFile = buildTestFile(0L);
    long partBegin = 0;
    long partEnd = 0;
    assertEquals(partETags.size(), originDataMd5Array.size());
    try {
        for (PartETag partETag : partETags) {
            int partNumber = partETag.getPartNumber();
            partBegin = (partNumber - 1) * partSize;
            partEnd = partNumber * partSize - 1;
            if (partEnd >= fileSize) {
                partEnd = fileSize - 1;
            }
            long[] range = new long[] { partBegin, partEnd };
            getObject(key, downloadFile, range, partEnd - partBegin + 1, originDataMd5Array.get(partNumber - 1));
            assertEquals(partEnd - partBegin + 1, downloadFile.length());
            assertEquals(originDataMd5Array.get(partNumber - 1), Md5Utils.md5Hex(downloadFile));
            assertEquals(!useClientEncryption, originDataMd5Array.get(partNumber - 1).equals(partETag.getETag()));
        }
    } catch (Exception e) {
        downloadFile.delete();
    }
}
Also used : File(java.io.File) PartETag(com.qcloud.cos.model.PartETag) CosServiceException(com.qcloud.cos.exception.CosServiceException) IOException(java.io.IOException)

Example 20 with PartETag

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

the class ImagePersistenceDemo method persistenceImageWithMultipart.

public static void persistenceImageWithMultipart(COSClient cosClient) throws FileNotFoundException {
    // bucket名需包含appid
    // api 请参考 https://cloud.tencent.com/document/product/436/54050
    String bucketName = "examplebucket-1250000000";
    String key = "test.jpg";
    File localFile = new File("E://test.jpg");
    InitiateMultipartUploadRequest request = new InitiateMultipartUploadRequest(bucketName, key);
    InitiateMultipartUploadResult initResult = cosClient.initiateMultipartUpload(request);
    String uploadId = initResult.getUploadId();
    // 上传分块
    List<PartETag> partETags = new LinkedList<>();
    UploadPartRequest uploadPartRequest = new UploadPartRequest();
    uploadPartRequest.setBucketName(bucketName);
    uploadPartRequest.setKey(key);
    uploadPartRequest.setUploadId(uploadId);
    // 设置分块的数据来源输入流
    uploadPartRequest.setInputStream(new FileInputStream(localFile));
    // 设置分块的长度
    // 设置数据长度
    uploadPartRequest.setPartSize(localFile.length());
    // 假设要上传的part编号是10
    uploadPartRequest.setPartNumber(1);
    UploadPartResult uploadPartResult = cosClient.uploadPart(uploadPartRequest);
    PartETag partETag = uploadPartResult.getPartETag();
    partETags.add(partETag);
    // 合并分块并带上图像处理参数
    PicOperations picOperations = new PicOperations();
    picOperations.setIsPicInfo(1);
    List<PicOperations.Rule> ruleList = new LinkedList<>();
    PicOperations.Rule rule1 = new PicOperations.Rule();
    rule1.setBucket(bucketName);
    rule1.setFileId("test-1.jpg");
    rule1.setRule("imageMogr2/rotate/90");
    ruleList.add(rule1);
    PicOperations.Rule rule2 = new PicOperations.Rule();
    rule2.setBucket(bucketName);
    rule2.setFileId("test-2.jpg");
    rule2.setRule("imageMogr2/rotate/180");
    ruleList.add(rule2);
    picOperations.setRules(ruleList);
    CompleteMultipartUploadRequest completeMultipartUploadRequest = new CompleteMultipartUploadRequest(bucketName, key, uploadId, partETags);
    completeMultipartUploadRequest.setPicOperations(picOperations);
    CompleteMultipartUploadResult completeMultipartUploadResult = cosClient.completeMultipartUpload(completeMultipartUploadRequest);
    // 获取图片处理的结果
    CIUploadResult ciUploadResult = completeMultipartUploadResult.getCiUploadResult();
    System.out.println(completeMultipartUploadResult.getRequestId());
    System.out.println(ciUploadResult.getOriginalInfo().getEtag());
    for (CIObject ciObject : ciUploadResult.getProcessResults().getObjectList()) {
        System.out.println(ciObject.getLocation());
        System.out.println(ciObject.getEtag());
    }
}
Also used : InitiateMultipartUploadResult(com.qcloud.cos.model.InitiateMultipartUploadResult) PicOperations(com.qcloud.cos.model.ciModel.persistence.PicOperations) InitiateMultipartUploadRequest(com.qcloud.cos.model.InitiateMultipartUploadRequest) UploadPartRequest(com.qcloud.cos.model.UploadPartRequest) CIUploadResult(com.qcloud.cos.model.ciModel.persistence.CIUploadResult) CompleteMultipartUploadResult(com.qcloud.cos.model.CompleteMultipartUploadResult) PartETag(com.qcloud.cos.model.PartETag) LinkedList(java.util.LinkedList) FileInputStream(java.io.FileInputStream) UploadPartResult(com.qcloud.cos.model.UploadPartResult) File(java.io.File) CIObject(com.qcloud.cos.model.ciModel.persistence.CIObject) CompleteMultipartUploadRequest(com.qcloud.cos.model.CompleteMultipartUploadRequest)

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