在亚马逊S3文件合并(Merging files on S3 Amazon)

2019-10-18 21:21发布

我有我想要合并两个文件存在于S3到第三个文件的应用程序。 我想用多上传使用复制的对象。 下面是代码。

       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);
        }

但是,在最后一行CompleteMultipartUpload抛出异常。 下面是S3例外:您提出的上传超过允许的最小尺寸更小

在哪里,如果我只能上传copyRequest1它工作正常。

任何帮助表示赞赏!

问候,Haseena

Answer 1:

你设法解决这个问题? 它似乎无法使用S3 API来完成



Answer 2:

这是不可能的合并使用S3 API上传的文件,所以我用FTP下载和合并。



文章来源: Merging files on S3 Amazon