-
Notifications
You must be signed in to change notification settings - Fork 5.7k
/
MPUapiCopyObj.cs
120 lines (104 loc) · 4.49 KB
/
MPUapiCopyObj.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
namespace MPUapiCopyObjectExample
{
// snippet-start:[S3.dotnetv3.MPUapiCopyObjectExample]
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Amazon.S3;
using Amazon.S3.Model;
/// <summary>
/// This example shows how to perform a multi-part copy from one Amazon
/// Simple Storage Service (Amazon S3) bucket to another.
/// </summary>
public class MPUapiCopyObj
{
private const string SourceBucket = "amzn-s3-demo-bucket1";
private const string TargetBucket = "amzn-s3-demo-bucket2";
private const string SourceObjectKey = "example.mov";
private const string TargetObjectKey = "copied_video_file.mov";
/// <summary>
/// This method starts the multi-part upload.
/// </summary>
public static async Task Main()
{
var s3Client = new AmazonS3Client();
Console.WriteLine("Copying object...");
await MPUCopyObjectAsync(s3Client);
}
/// <summary>
/// This method uses the passed client object to perform a multipart
/// copy operation.
/// </summary>
/// <param name="client">An Amazon S3 client object that will be used
/// to perform the copy.</param>
public static async Task MPUCopyObjectAsync(AmazonS3Client client)
{
// Create a list to store the copy part responses.
var copyResponses = new List<CopyPartResponse>();
// Setup information required to initiate the multipart upload.
var initiateRequest = new InitiateMultipartUploadRequest
{
BucketName = TargetBucket,
Key = TargetObjectKey,
};
// Initiate the upload.
InitiateMultipartUploadResponse initResponse =
await client.InitiateMultipartUploadAsync(initiateRequest);
// Save the upload ID.
string uploadId = initResponse.UploadId;
try
{
// Get the size of the object.
var metadataRequest = new GetObjectMetadataRequest
{
BucketName = SourceBucket,
Key = SourceObjectKey,
};
GetObjectMetadataResponse metadataResponse =
await client.GetObjectMetadataAsync(metadataRequest);
var objectSize = metadataResponse.ContentLength; // Length in bytes.
// Copy the parts.
var partSize = 5 * (long)Math.Pow(2, 20); // Part size is 5 MB.
long bytePosition = 0;
for (int i = 1; bytePosition < objectSize; i++)
{
var 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 client.CopyPartAsync(copyRequest));
bytePosition += partSize;
}
// Set up to complete the copy.
var completeRequest = new CompleteMultipartUploadRequest
{
BucketName = TargetBucket,
Key = TargetObjectKey,
UploadId = initResponse.UploadId,
};
completeRequest.AddPartETags(copyResponses);
// Complete the copy.
CompleteMultipartUploadResponse completeUploadResponse =
await client.CompleteMultipartUploadAsync(completeRequest);
}
catch (AmazonS3Exception e)
{
Console.WriteLine($"Error encountered on server. Message:'{e.Message}' when writing an object");
}
catch (Exception e)
{
Console.WriteLine($"Unknown encountered on server. Message:'{e.Message}' when writing an object");
}
}
}
// snippet-end:[S3.dotnetv3.MPUapiCopyObjectExample]
}