Ceph入门到精通-S3分片上传


    private  boolean multipartUpload(String key, String bucketName) {
       try {
           int mB = 1024 * 1024;
           // snippet-start:[s3.java2.s3_object_operations.upload_multi_part]
           // First create a multipart upload and get the upload id
           CreateMultipartUploadRequest createMultipartUploadRequest = CreateMultipartUploadRequest.builder()
                   .bucket(bucketName)
                   .key(key)
                   .build();

           CreateMultipartUploadResponse response = destS3Client.createMultipartUpload(createMultipartUploadRequest);
           String uploadId = response.uploadId();
           System.out.println(uploadId);

           // Upload all the different parts of the object
           UploadPartRequest uploadPartRequest1 = UploadPartRequest.builder()
                   .bucket(bucketName)
                   .key(key)
                   .uploadId(uploadId)
                   .partNumber(1).build();

           String etag1 = destS3Client.uploadPart(uploadPartRequest1, RequestBody.fromByteBuffer(getRandomByteBuffer(5 * mB))).eTag();

           CompletedPart part1 = CompletedPart.builder().partNumber(1).eTag(etag1).build();

           UploadPartRequest uploadPartRequest2 = UploadPartRequest.builder().bucket(bucketName).key(key)
                   .uploadId(uploadId)
                   .partNumber(2).build();
           String etag2 = S3Client.uploadPart(uploadPartRequest2, RequestBody.fromByteBuffer(getRandomByteBuffer(3 * mB))).eTag();
           CompletedPart part2 = CompletedPart.builder().partNumber(2).eTag(etag2).build();


           // Finally call completeMultipartUpload operation to tell S3 to merge all uploaded
           // parts and finish the multipart operation.
           CompletedMultipartUpload completedMultipartUpload = CompletedMultipartUpload.builder()
                   .parts(part1, part2)
                   .build();

           CompleteMultipartUploadRequest completeMultipartUploadRequest =
                   CompleteMultipartUploadRequest.builder()
                           .bucket(bucketName)
                           .key(key)
                           .uploadId(uploadId)
                           .multipartUpload(completedMultipartUpload)
                           .build();

           S3Client.completeMultipartUpload(completeMultipartUploadRequest);
           // snippet-end:[s3.java2.s3_object_operations.upload_multi_part]
       }catch (Exception e){

       }

        return true;
    }
    

按chunksize分割


    private byte[][] splitByte(byte[] bytes, int chunkSize){
        int numOfChunks = (int) Math.ceil((double) bytes.length / chunkSize);

        byte[][] chunks = new byte[numOfChunks][];

        for (int i = 0; i < numOfChunks; i++) {
            int start = i * chunkSize;
            int length = Math.min(bytes.length - start, chunkSize);

            chunks[i] = Arrays.copyOfRange(bytes, start, start + length);
        }

        // 打印切割后的小数组
        return chunks;
    }

你可能感兴趣的:(Ceph入门到精通,java,开发语言)