我有一个应用程序,我想将 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 它工作正常。
任何帮助表示赞赏!
问候,哈西娜