As per the Amazon documentation there is a API called UploadPartCopy which copies an object from source bucket into a part of a destination object. But I cant find this API in S3Client for .Net interface. There is a UploadPart (for which I need to supply stream) but no UploadPartCopy. My usecase is that, I already have multiple objects in a source bucket and I want to form a destination bucket by combining all these objects into a single bucket. I want to do this without performing GetObject and then doing UploadPart as this involves a round trip.
I cant seem to find this API in SDK. What am I missing?
CodePudding user response:
My guess is that you are looking for the multipart functions: https://docs.aws.amazon.com/sdkfornet/v3/apidocs/items/S3/MS3InitiateMultipartUploadStringString.html
CodePudding user response:
The API that calls into the REST API UploadPartCopy is CopyPart and CopyPartRequest. You can find a fairly complete demo of using this API and the related APIs from Amazon, here's the salient portion that should demonstrate how to call the API:
// Create a list to store the upload part responses.
List<UploadPartResponse> uploadResponses = new List<UploadPartResponse>();
List<CopyPartResponse> copyResponses = new List<CopyPartResponse>();
// Setup information required to initiate the multipart upload.
InitiateMultipartUploadRequest initiateRequest =
new InitiateMultipartUploadRequest
{
BucketName = targetBucket,
Key = targetObjectKey
};
// Initiate the upload.
InitiateMultipartUploadResponse initResponse = await s3Client.InitiateMultipartUploadAsync(initiateRequest);
// Save the upload ID.
String uploadId = initResponse.UploadId;
// Get the size of the object.
GetObjectMetadataRequest metadataRequest = new GetObjectMetadataRequest
{
BucketName = sourceBucket,
Key = sourceObjectKey
};
GetObjectMetadataResponse metadataResponse = await s3Client.GetObjectMetadataAsync(metadataRequest);
long objectSize = metadataResponse.ContentLength; // Length in bytes.
// Copy the parts.
long partSize = 5242880; // Part size is 5 MiB.
long bytePosition = 0;
for (int i = 1; bytePosition < objectSize; i )
{
CopyPartRequest copyRequest = new CopyPartRequest
{
DestinationBucket = targetBucket,
DestinationKey = targetObjectKey,
SourceBucket = sourceBucket,
SourceKey = sourceObjectKey,
UploadId = uploadId,
FirstByte = bytePosition,
LastByte = bytePosition partSize - 1 >= objectSize ? objectSize - 1 : bytePosition partSize - 1,
PartNumber = i
};
copyResponses.Add(await s3Client.CopyPartAsync(copyRequest));
bytePosition = partSize;
}
// Set up to complete the copy.
CompleteMultipartUploadRequest completeRequest =
new CompleteMultipartUploadRequest
{
BucketName = targetBucket,
Key = targetObjectKey,
UploadId = initResponse.UploadId
};
completeRequest.AddPartETags(copyResponses);
// Complete the copy.
CompleteMultipartUploadResponse completeUploadResponse = await s3Client.CompleteMultipartUploadAsync(completeRequest);