Merging files on S3 Amazon

2019-08-08 13:16发布

问题:

I have an application where i want to merge two files present on s3 into the third file. I thought of using the Copy Object using multipart upload. Below is the code.

       AmazonS3Config config = new AmazonS3Config();
        AmazonS3 s3Client = new AmazonS3Client(accessKeyID, secretAccessKey, config);

        // List to store upload part responses.
        List<UploadPartResponse> uploadResponses =
            new List<UploadPartResponse>();

        List<CopyPartResponse> copyResponses =
                  new List<CopyPartResponse>();
        InitiateMultipartUploadRequest initiateRequest =
               new InitiateMultipartUploadRequest()
                .WithBucketName(targetBucket)
                .WithKey(targetObjectKey);

        InitiateMultipartUploadResponse initResponse =
            s3Client.InitiateMultipartUpload(initiateRequest);
        String uploadId = initResponse.UploadId;

        try
        {
            // Get object size.
            GetObjectMetadataRequest metadataRequest = new GetObjectMetadataRequest();
            metadataRequest.BucketName = sourceBucket;
            metadataRequest.Key = sourceObjectKey1;

            GetObjectMetadataResponse metadataResponse = s3Client.GetObjectMetadata(metadataRequest);
            long objectSize1 = metadataResponse.ContentLength; // in bytes

            // Get object size.
            GetObjectMetadataRequest metadataRequest2 = new GetObjectMetadataRequest();
            metadataRequest2.BucketName = sourceBucket;
            metadataRequest2.Key = sourceObjectKey2;

            GetObjectMetadataResponse metadataResponse2 = s3Client.GetObjectMetadata(metadataRequest2);
            long objectSize2 = metadataResponse2.ContentLength; // in bytes

            long bytePosition = 0;


            CopyPartRequest copyRequest1 = new CopyPartRequest()
               .WithDestinationBucket(targetBucket)
               .WithDestinationKey(targetObjectKey)
               .WithSourceBucket(sourceBucket)
               .WithSourceKey(sourceObjectKey1)
               .WithUploadID(uploadId)
               .WithFirstByte(bytePosition)
               .WithLastByte( objectSize1 - 1 )
               .WithPartNumber(1);

            copyResponses.Add(s3Client.CopyPart(copyRequest1));


            CopyPartRequest copyRequest2 = new CopyPartRequest()
               .WithDestinationBucket(targetBucket)
               .WithDestinationKey(targetObjectKey)
               .WithSourceBucket(sourceBucket)
               .WithSourceKey(sourceObjectKey2)
               .WithUploadID(uploadId)
               .WithFirstByte(bytePosition)
               .WithLastByte(objectSize2 - 1)
               .WithPartNumber(2);

            copyResponses.Add(s3Client.CopyPart(copyRequest2));

            ////
            CompleteMultipartUploadRequest completeRequest =
                  new CompleteMultipartUploadRequest()
                     .WithBucketName(targetBucket)
                     .WithKey(targetObjectKey)
                     .WithUploadId(initResponse.UploadId)
                     .WithPartETags(GetETags(copyResponses));

            CompleteMultipartUploadResponse completeUploadResponse =
                s3Client.CompleteMultipartUpload(completeRequest);

        }
        catch (Exception e)
        {
            Console.WriteLine(e.Message);
        }

But it is throwing exception at the last line CompleteMultipartUpload. Below is the S3 exception: Your proposed upload is smaller than the minimum allowed size

Where as if i only upload copyRequest1 it works fine.

Any help is appreciated!!

Regards, Haseena

回答1:

Did you manage to solve the problem? It seems that it can't be done using S3 api



回答2:

It is not possible to merge uploaded files using S3 API, so I am using FTP to download and Merge.