合并S3 Amazon上的文件

时间:2013-10-29 10:16:06

标签: c# amazon-web-services amazon-s3

我有一个应用程序,我想将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

2 个答案:

答案 0 :(得分:0)

你设法解决了这个问题吗?似乎无法使用S3 api

完成

答案 1 :(得分:0)

使用S3 API无法合并上传的文件,因此我使用FTP下载和合并。