static void Main(string[] args) { IAmazonS3 s3Client = new AmazonS3Client(Amazon.RegionEndpoint.USEast1); // List to store upload part responses. List<UploadPartResponse> uploadResponses = new List<UploadPartResponse>(); List<CopyPartResponse> copyResponses = new List<CopyPartResponse>(); InitiateMultipartUploadRequest initiateRequest = new InitiateMultipartUploadRequest { BucketName = targetBucket, Key = targetObjectKey }; InitiateMultipartUploadResponse initResponse = s3Client.InitiateMultipartUpload(initiateRequest); String uploadId = initResponse.UploadId; try { // Get object size. GetObjectMetadataRequest metadataRequest = new GetObjectMetadataRequest { BucketName = sourceBucket, Key = sourceObjectKey }; GetObjectMetadataResponse metadataResponse = s3Client.GetObjectMetadata(metadataRequest); long objectSize = metadataResponse.ContentLength; // in bytes // Copy parts. long partSize = 5 * (long)Math.Pow(2, 20); // 5 MB 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(s3Client.CopyPart(copyRequest)); bytePosition += partSize; } CompleteMultipartUploadRequest completeRequest = new CompleteMultipartUploadRequest { BucketName = targetBucket, Key = targetObjectKey, UploadId = initResponse.UploadId }; completeRequest.AddPartETags(copyResponses); CompleteMultipartUploadResponse completeUploadResponse = s3Client.CompleteMultipartUpload(completeRequest); } catch (Exception e) { Console.WriteLine(e.Message); } }
/// <summary> /// Signal the upload for this uploadId and fileName is complete /// </summary> /// <param name="s3Client">S3 client object.</param> /// <param name="uploadTarget">Upload target returned from the Upload REST API.</param> /// <param name="fileName">Name of the file to be uploaded.</param> /// <param name="uploadId">Upload ID returned from the S3 server.</param> /// <param name="partResponses">List of upload part responses from the server.</param> /// <returns>Response from S3 server.</returns> public static CompleteMultipartUploadResponse CloseUpload( AmazonS3Client s3Client, string uploadTarget, string fileName, string uploadId, List <UploadPartResponse> partResponses) { string fileKey = GetFileKey(uploadTarget, fileName); CompleteMultipartUploadRequest completeRequest = new CompleteMultipartUploadRequest { BucketName = Common.UploadBucketName, Key = fileKey, UploadId = uploadId, }; completeRequest.AddPartETags(partResponses); return(s3Client.CompleteMultipartUpload(completeRequest)); }
public string CompletePartialAndSaveResource(ReferenceRepository referenceRepository, ResourceModel.User owner, string uploadIdentifier, String originalName, String md5OfResource, List <string> etags, long totalSize) { IAmazonS3 s3Client = new AmazonS3Client(); ResourceModel.DigitalResource resource = null; ResourceType imageRT = referenceRepository.AllResourceTypes().Where(x => x.Type == "Image").FirstOrDefault(); ResourceType otherRT = referenceRepository.AllResourceTypes().Where(x => x.Type == "Other").FirstOrDefault(); List <PartETag> partTags = new List <PartETag>(); int partNumber = 1; foreach (string tag in etags) { partTags.Add(new PartETag { ETag = tag, PartNumber = partNumber }); partNumber++; } // complete the upload. CompleteMultipartUploadRequest completeRequest = new CompleteMultipartUploadRequest { BucketName = "piccoli", Key = md5OfResource, UploadId = uploadIdentifier, PartETags = partTags }; // Complete the upload. CompleteMultipartUploadResponse completeUploadResponse = s3Client.CompleteMultipartUpload(completeRequest); //create the resource object resource = CreateNewResourceObject(owner, totalSize, originalName, imageRT, otherRT, md5OfResource); SaveResource(resource);//update the database return(completeUploadResponse.ETag); }
/// <summary> /// Signal the upload for this uploadId and fileName is complete /// </summary> /// <param name="s3Client">S3 client object.</param> /// <param name="uploadTarget">Upload target returned from the Upload REST API.</param> /// <param name="fileName">Name of the file to be uploaded.</param> /// <param name="uploadId">Upload ID returned from the S3 server.</param> /// <param name="partResponses">List of upload part responses from the server.</param> /// <returns>Response from S3 server.</returns> public static CompleteMultipartUploadResponse CloseUpload( AmazonS3Client s3Client, string uploadTarget, string fileName, string uploadId, List <UploadPartResponse> partResponses) { if (s3Client == null || uploadTarget == null || fileName == null || uploadId == null || partResponses == null || partResponses.Count <= 0) { throw new InvalidDataException(); } string fileKey = GetFileKey(uploadTarget, fileName); CompleteMultipartUploadRequest completeRequest = new CompleteMultipartUploadRequest { BucketName = Common.UploadBucketName, Key = fileKey, UploadId = uploadId, }; completeRequest.AddPartETags(partResponses); return(s3Client.CompleteMultipartUpload(completeRequest)); }
public static void Main(string[] args) { // create the AWS S3 client AmazonS3Client s3 = AWSS3Factory.getS3Client(); // retrieve the object key/value from user Console.Write("Enter the object key: "); string key = Console.ReadLine(); Console.Write("Enter the file location: "); string filePath = Console.ReadLine(); // grab the start time of upload DateTime startDate = DateTime.Now; // part size for chunking in multi-part long partSize = 1024 * 1024 * 2; // 2 MB // list of upload part response objects for each part that is uploaded List <PartETag> partETags = new List <PartETag>(); // Step 1: Initialize InitiateMultipartUploadRequest initRequest = new InitiateMultipartUploadRequest() { BucketName = AWSS3Factory.S3_BUCKET, Key = key, }; InitiateMultipartUploadResponse initResponse = s3.InitiateMultipartUpload(initRequest); // get the file and file length long contentLength = new FileInfo(filePath).Length; Console.WriteLine(string.Format("Starting multi-part upload for object {0}/{1} with file path {2} and size {3} in {4} MB size chunks", AWSS3Factory.S3_BUCKET, key, filePath, Convert.ToString(contentLength), partSize / 1024 / 1024)); try { // Step 2: Upload parts long filePosition = 0; for (int i = 1; filePosition < contentLength; i++) { // get the size of the chunk. Note - the last part can be less than the chunk size partSize = Math.Min(partSize, (contentLength - filePosition)); Console.WriteLine(string.Format("Sending chunk {0} starting at position {1}", i, filePosition)); // create request to upload a part UploadPartRequest uploadRequest = new UploadPartRequest() { BucketName = AWSS3Factory.S3_BUCKET, Key = key, UploadId = initResponse.UploadId, PartNumber = i, FilePosition = filePosition, FilePath = filePath, PartSize = partSize }; UploadPartResponse partResponse = s3.UploadPart(uploadRequest); PartETag eTagPart = new PartETag(partResponse.PartNumber, partResponse.ETag); partETags.Add(eTagPart); filePosition = filePosition += partSize; } // Step 3: complete Console.WriteLine("Waiting for completion of multi-part upload"); CompleteMultipartUploadRequest compRequest = new CompleteMultipartUploadRequest() { BucketName = AWSS3Factory.S3_BUCKET, Key = key, UploadId = initResponse.UploadId, PartETags = partETags }; s3.CompleteMultipartUpload(compRequest); } catch (Exception e) { s3.AbortMultipartUpload(new AbortMultipartUploadRequest() { BucketName = AWSS3Factory.S3_BUCKET, Key = key, UploadId = initResponse.UploadId }); } // grab the end time of upload DateTime endDate = DateTime.Now; Console.WriteLine(string.Format("Completed multi-part upload for object {0}/{1} with file path: {2}", AWSS3Factory.S3_BUCKET, key, filePath)); Console.WriteLine(string.Format("Process took: {0} seconds.", (endDate - startDate).TotalSeconds.ToString())); Console.ReadLine(); }
public static async Task MainSync(string key, string filePath) { // create the AWS S3 client AmazonS3Client s3 = AWSS3Factory.getS3Client(); // part size for chunking in multi-part long partSize = 1024 * 1024 * 2; // 2 MB // list of upload part response objects for each part that is uploaded IEnumerable <PartETag> partETags = new List <PartETag>(); // Step 1: Initialize InitiateMultipartUploadRequest initRequest = new InitiateMultipartUploadRequest() { BucketName = AWSS3Factory.S3_BUCKET, Key = key, }; // call initialize method -obtain upload id to be used for subsequent parts. InitiateMultipartUploadResponse initResponse = s3.InitiateMultipartUpload(initRequest); // get the file and file length fileSize = new FileInfo(filePath).Length; Console.WriteLine(string.Format("Starting multi-part upload for object {0}/{1} with file path {2} and size {3} in {4} MB size chunks", AWSS3Factory.S3_BUCKET, key, filePath, Convert.ToString(fileSize), partSize / 1024 / 1024)); try { // STEP 2: generate list of parts to be uploaded long filePosition = 0; // the parts list representing each chunk to be uploaded List <UploadPartRequest> parts = new List <UploadPartRequest>(); for (int i = 1; filePosition < fileSize; i++) { // get the size of the chunk. Note - the last part can be less than the chunk size partSize = Math.Min(partSize, (fileSize - filePosition)); // create request to upload a part UploadPartRequest uploadRequest = new UploadPartRequest() { BucketName = AWSS3Factory.S3_BUCKET, Key = key, UploadId = initResponse.UploadId, PartNumber = i, FilePosition = filePosition, FilePath = filePath, PartSize = partSize }; parts.Add(uploadRequest); filePosition = filePosition += partSize; } // generate query to simultaneously upload chunks IEnumerable <Task <PartETag> > uploadTasksQuery = from part in parts select ProcessChunk(part); // execute the upload query List <Task <PartETag> > uploadTasks = uploadTasksQuery.ToList(); // // Can do other work here while waiting ... Console.WriteLine("Waiting for completion of multi-part upload"); // // // wait here for the query to complete partETags = await Task.WhenAll(uploadTasks); // STEP 3: complete the mpu CompleteMultipartUploadRequest compRequest = new CompleteMultipartUploadRequest() { BucketName = AWSS3Factory.S3_BUCKET, Key = key, UploadId = initResponse.UploadId, PartETags = partETags.ToList() }; s3.CompleteMultipartUpload(compRequest); } catch (Exception e) { s3.AbortMultipartUpload(new AbortMultipartUploadRequest() { BucketName = AWSS3Factory.S3_BUCKET, Key = key, UploadId = initResponse.UploadId }); } }
public void ObjectSamples() { { #region ListObjects Sample // Create a client AmazonS3Client client = new AmazonS3Client(); // List all objects ListObjectsRequest listRequest = new ListObjectsRequest { BucketName = "SampleBucket", }; ListObjectsResponse listResponse; do { // Get a list of objects listResponse = client.ListObjects(listRequest); foreach (S3Object obj in listResponse.S3Objects) { Console.WriteLine("Object - " + obj.Key); Console.WriteLine(" Size - " + obj.Size); Console.WriteLine(" LastModified - " + obj.LastModified); Console.WriteLine(" Storage class - " + obj.StorageClass); } // Set the marker property listRequest.Marker = listResponse.NextMarker; } while (listResponse.IsTruncated); #endregion } { #region GetObject Sample // Create a client AmazonS3Client client = new AmazonS3Client(); // Create a GetObject request GetObjectRequest request = new GetObjectRequest { BucketName = "SampleBucket", Key = "Item1" }; // Issue request and remember to dispose of the response using (GetObjectResponse response = client.GetObject(request)) { using (StreamReader reader = new StreamReader(response.ResponseStream)) { string contents = reader.ReadToEnd(); Console.WriteLine("Object - " + response.Key); Console.WriteLine(" Version Id - " + response.VersionId); Console.WriteLine(" Contents - " + contents); } } #endregion } { #region GetObjectMetadata Sample // Create a client AmazonS3Client client = new AmazonS3Client(); // Create a GetObjectMetadata request GetObjectMetadataRequest request = new GetObjectMetadataRequest { BucketName = "SampleBucket", Key = "Item1" }; // Issue request and view the response GetObjectMetadataResponse response = client.GetObjectMetadata(request); Console.WriteLine("Content Length - " + response.ContentLength); Console.WriteLine("Content Type - " + response.Headers.ContentType); if (response.Expiration != null) { Console.WriteLine("Expiration Date - " + response.Expiration.ExpiryDate); Console.WriteLine("Expiration Rule Id - " + response.Expiration.RuleId); } #endregion } { #region PutObject Sample 1 // Create a client AmazonS3Client client = new AmazonS3Client(); // Create a PutObject request PutObjectRequest request = new PutObjectRequest { BucketName = "SampleBucket", Key = "Item1", ContentBody = "This is sample content..." }; // Put object PutObjectResponse response = client.PutObject(request); #endregion } { #region PutObject Sample 2 // Create a client AmazonS3Client client = new AmazonS3Client(); // Create a PutObject request PutObjectRequest request = new PutObjectRequest { BucketName = "SampleBucket", Key = "Item1", FilePath = "contents.txt" }; // Put object PutObjectResponse response = client.PutObject(request); #endregion } { #region PutObject Sample 3 // Create a client AmazonS3Client client = new AmazonS3Client(); // Create a PutObject request PutObjectRequest request = new PutObjectRequest { BucketName = "SampleBucket", Key = "Item1", }; using (FileStream stream = new FileStream("contents.txt", FileMode.Open)) { request.InputStream = stream; // Put object PutObjectResponse response = client.PutObject(request); } #endregion } { #region DeleteObject Sample // Create a client AmazonS3Client client = new AmazonS3Client(); // Create a DeleteObject request DeleteObjectRequest request = new DeleteObjectRequest { BucketName = "SampleBucket", Key = "Item1" }; // Issue request client.DeleteObject(request); #endregion } { #region DeleteObjects Sample // Create a client AmazonS3Client client = new AmazonS3Client(); // Create a DeleteObject request DeleteObjectsRequest request = new DeleteObjectsRequest { BucketName = "SampleBucket", Objects = new List <KeyVersion> { new KeyVersion() { Key = "Item1" }, // Versioned item new KeyVersion() { Key = "Item2", VersionId = "Rej8CiBxcZKVK81cLr39j27Y5FVXghDK", }, // Item in subdirectory new KeyVersion() { Key = "Logs/error.txt" } } }; try { // Issue request DeleteObjectsResponse response = client.DeleteObjects(request); } catch (DeleteObjectsException doe) { // Catch error and list error details DeleteObjectsResponse errorResponse = doe.Response; foreach (DeletedObject deletedObject in errorResponse.DeletedObjects) { Console.WriteLine("Deleted item " + deletedObject.Key); } foreach (DeleteError deleteError in errorResponse.DeleteErrors) { Console.WriteLine("Error deleting item " + deleteError.Key); Console.WriteLine(" Code - " + deleteError.Code); Console.WriteLine(" Message - " + deleteError.Message); } } #endregion } { #region CopyObject Sample // Create a client AmazonS3Client client = new AmazonS3Client(); // Create a CopyObject request CopyObjectRequest request = new CopyObjectRequest { SourceBucket = "SampleBucket", SourceKey = "Item1", DestinationBucket = "AnotherBucket", DestinationKey = "Copy1", CannedACL = S3CannedACL.PublicRead }; // Issue request client.CopyObject(request); #endregion } { #region CopyObject Sample // Create a client AmazonS3Client client = new AmazonS3Client(); // Create a CopyObject request CopyObjectRequest request = new CopyObjectRequest { SourceBucket = "SampleBucket", SourceKey = "Item1", DestinationBucket = "AnotherBucket", DestinationKey = "Copy1", CannedACL = S3CannedACL.PublicRead }; // Issue request client.CopyObject(request); #endregion } { #region ListVersions Sample // Create a client AmazonS3Client client = new AmazonS3Client(); // Turn versioning on for a bucket client.PutBucketVersioning(new PutBucketVersioningRequest { BucketName = "SampleBucket", VersioningConfig = new S3BucketVersioningConfig { Status = "Enable" } }); // Populate bucket with multiple items, each with multiple versions PopulateBucket(client, "SampleBucket"); // Get versions ListVersionsRequest request = new ListVersionsRequest { BucketName = "SampleBucket" }; // Make paged ListVersions calls ListVersionsResponse response; do { response = client.ListVersions(request); // View information about versions foreach (var version in response.Versions) { Console.WriteLine("Key = {0}, Version = {1}, IsLatest = {2}, LastModified = {3}, Size = {4}", version.Key, version.VersionId, version.IsLatest, version.LastModified, version.Size); } request.KeyMarker = response.NextKeyMarker; request.VersionIdMarker = response.NextVersionIdMarker; } while (response.IsTruncated); #endregion } { #region Multipart Upload Sample int MB = (int)Math.Pow(2, 20); // Create a client AmazonS3Client client = new AmazonS3Client(); // Define input stream Stream inputStream = Create13MBDataStream(); // Initiate multipart upload InitiateMultipartUploadRequest initRequest = new InitiateMultipartUploadRequest { BucketName = "SampleBucket", Key = "Item1" }; InitiateMultipartUploadResponse initResponse = client.InitiateMultipartUpload(initRequest); // Upload part 1 UploadPartRequest uploadRequest = new UploadPartRequest { BucketName = "SampleBucket", Key = "Item1", UploadId = initResponse.UploadId, PartNumber = 1, PartSize = 5 * MB, InputStream = inputStream }; UploadPartResponse up1Response = client.UploadPart(uploadRequest); // Upload part 2 uploadRequest = new UploadPartRequest { BucketName = "SampleBucket", Key = "Item1", UploadId = initResponse.UploadId, PartNumber = 2, PartSize = 5 * MB, InputStream = inputStream }; UploadPartResponse up2Response = client.UploadPart(uploadRequest); // Upload part 3 uploadRequest = new UploadPartRequest { BucketName = "SampleBucket", Key = "Item1", UploadId = initResponse.UploadId, PartNumber = 3, InputStream = inputStream }; UploadPartResponse up3Response = client.UploadPart(uploadRequest); // List parts for current upload ListPartsRequest listPartRequest = new ListPartsRequest { BucketName = "SampleBucket", Key = "Item1", UploadId = initResponse.UploadId }; ListPartsResponse listPartResponse = client.ListParts(listPartRequest); Debug.Assert(listPartResponse.Parts.Count == 3); // Complete the multipart upload CompleteMultipartUploadRequest compRequest = new CompleteMultipartUploadRequest { BucketName = "SampleBucket", Key = "Item1", UploadId = initResponse.UploadId, PartETags = new List <PartETag> { new PartETag { ETag = up1Response.ETag, PartNumber = 1 }, new PartETag { ETag = up2Response.ETag, PartNumber = 2 }, new PartETag { ETag = up3Response.ETag, PartNumber = 3 } } }; CompleteMultipartUploadResponse compResponse = client.CompleteMultipartUpload(compRequest); #endregion } }
CancellationTokenSource IBackupService.Upload(BackupEntry entry, Stream stream, Action <int> progress) { if (entry == null) { throw new ArgumentNullException(nameof(entry)); } if (stream == null) { throw new ArgumentNullException(nameof(stream)); } if (progress == null) { throw new ArgumentNullException(nameof(progress)); } var key = GetKey(entry); var initResponse = _client.InitiateMultipartUpload(new InitiateMultipartUploadRequest { BucketName = _bucket, Key = key, }); var filePosition = 0L; var nextProgress = 1; var etags = new List <PartETag>(); var partNum = 1; while (filePosition < stream.Length) { var response = _client.UploadPart(new UploadPartRequest { BucketName = _bucket, UploadId = initResponse.UploadId, PartNumber = partNum, PartSize = _bufferSize, //FilePosition = filePosition, InputStream = stream, Key = key }); etags.Add(new PartETag(partNum, response.ETag)); filePosition += _bufferSize; var currProgress = (int)(filePosition.Min(stream.Length) * 100 / stream.Length); if (currProgress >= nextProgress) { progress(currProgress); nextProgress = currProgress + 1; } partNum++; } _client.CompleteMultipartUpload(new CompleteMultipartUploadRequest { BucketName = _bucket, UploadId = initResponse.UploadId, Key = key, PartETags = etags }); var source = new CancellationTokenSource(); return(source); }
public static void MultipartEncryptionTest(AmazonS3Client s3EncryptionClient, IAmazonS3 s3DecryptionClient, string bucketName) { var guid = Guid.NewGuid(); var filePath = Path.Combine(Path.GetTempPath(), $"multi-{guid}.txt"); var retrievedFilepath = Path.Combine(Path.GetTempPath(), $"retrieved-{guid}.txt"); var totalSize = MegaByteSize * 15; UtilityMethods.GenerateFile(filePath, totalSize); var key = $"key-{guid}"; Stream inputStream = File.OpenRead(filePath); try { var initRequest = new InitiateMultipartUploadRequest() { BucketName = bucketName, Key = key, StorageClass = S3StorageClass.OneZoneInfrequentAccess, ContentType = "text/html", }; var initResponse = s3EncryptionClient.InitiateMultipartUpload(initRequest); // Upload part 1 var uploadRequest = new UploadPartRequest() { BucketName = bucketName, Key = key, UploadId = initResponse.UploadId, PartNumber = 1, PartSize = 5 * MegaByteSize, InputStream = inputStream }; var up1Response = s3EncryptionClient.UploadPart(uploadRequest); // Upload part 2 uploadRequest = new UploadPartRequest() { BucketName = bucketName, Key = key, UploadId = initResponse.UploadId, PartNumber = 2, PartSize = 5 * MegaByteSize, InputStream = inputStream }; var up2Response = s3EncryptionClient.UploadPart(uploadRequest); // Upload part 3 uploadRequest = new UploadPartRequest() { BucketName = bucketName, Key = key, UploadId = initResponse.UploadId, PartNumber = 3, InputStream = inputStream, IsLastPart = true }; var up3Response = s3EncryptionClient.UploadPart(uploadRequest); var listPartRequest = new ListPartsRequest() { BucketName = bucketName, Key = key, UploadId = initResponse.UploadId }; var listPartResponse = s3EncryptionClient.ListParts(listPartRequest); Assert.Equal(3, listPartResponse.Parts.Count); Assert.Equal(up1Response.PartNumber, listPartResponse.Parts[0].PartNumber); Assert.Equal(up1Response.ETag, listPartResponse.Parts[0].ETag); Assert.Equal(up2Response.PartNumber, listPartResponse.Parts[1].PartNumber); Assert.Equal(up2Response.ETag, listPartResponse.Parts[1].ETag); Assert.Equal(up3Response.PartNumber, listPartResponse.Parts[2].PartNumber); Assert.Equal(up3Response.ETag, listPartResponse.Parts[2].ETag); listPartRequest.MaxParts = 1; listPartResponse = s3EncryptionClient.ListParts(listPartRequest); Assert.Equal(1, listPartResponse.Parts.Count); // Complete the response var compRequest = new CompleteMultipartUploadRequest() { BucketName = bucketName, Key = key, UploadId = initResponse.UploadId }; compRequest.AddPartETags(up1Response, up2Response, up3Response); var compResponse = s3EncryptionClient.CompleteMultipartUpload(compRequest); Assert.Equal(bucketName, compResponse.BucketName); Assert.NotNull(compResponse.ETag); Assert.Equal(key, compResponse.Key); Assert.NotNull(compResponse.Location); // Get the file back from S3 and make sure it is still the same. var getRequest = new GetObjectRequest() { BucketName = bucketName, Key = key }; var getResponse = s3DecryptionClient.GetObject(getRequest); getResponse.WriteResponseStreamToFile(retrievedFilepath); UtilityMethods.CompareFiles(filePath, retrievedFilepath); var metaDataRequest = new GetObjectMetadataRequest() { BucketName = bucketName, Key = key }; var metaDataResponse = s3DecryptionClient.GetObjectMetadata(metaDataRequest); Assert.Equal("text/html", metaDataResponse.Headers.ContentType); } finally { inputStream.Close(); if (File.Exists(filePath)) { File.Delete(filePath); } if (File.Exists(retrievedFilepath)) { File.Delete(retrievedFilepath); } } #if ASYNC_AWAIT // run the async version of the same test WaitForAsyncTask(MultipartEncryptionTestAsync(s3EncryptionClient, s3DecryptionClient, bucketName)); #elif AWS_APM_API // run the APM version of the same test MultipartEncryptionTestAPM(s3EncryptionClient, s3DecryptionClient, bucketName); #endif }
public static void putObjectAsMultiPart() { Stopwatch sw = Stopwatch.StartNew(); var s3Client = new AmazonS3Client( "AKIA6PYYJMASLJEFTI6E", "IJPo9Ys58iAb35dKw4kcW/SkOU2J+iI9IOA5Wpl6", Amazon.RegionEndpoint.APSoutheast1 ); // List to store upload part responses. var uploadResponses = new List <UploadPartResponse>(); // 1. Initialize. var initiateRequest = new InitiateMultipartUploadRequest { BucketName = bucketName, Key = keyName }; var initResponse = s3Client.InitiateMultipartUpload(initiateRequest); // 2. Upload Parts. var contentLength = new FileInfo(filePath).Length; var partSize = 5242880; // 5 MB try { long filePosition = 0; for (var i = 1; filePosition < contentLength; ++i) { // Create request to upload a part. var uploadRequest = new UploadPartRequest { BucketName = bucketName, Key = keyName, UploadId = initResponse.UploadId, PartNumber = i, PartSize = partSize, FilePosition = filePosition, FilePath = filePath }; // Upload part and add response to our list. uploadResponses.Add(s3Client.UploadPart(uploadRequest)); filePosition += partSize; } // Step 3: complete. var completeRequest = new CompleteMultipartUploadRequest { BucketName = bucketName, Key = keyName, UploadId = initResponse.UploadId, }; // add ETags for uploaded files completeRequest.AddPartETags(uploadResponses); var completeUploadResponse = s3Client.CompleteMultipartUpload(completeRequest); sw.Stop(); Console.WriteLine("Upload completed : {0}", sw.Elapsed.TotalMilliseconds); } catch (Exception exception) { Console.WriteLine("Exception occurred: {0}", exception.ToString()); var abortMPURequest = new AbortMultipartUploadRequest { BucketName = bucketName, Key = keyName, UploadId = initResponse.UploadId }; s3Client.AbortMultipartUpload(abortMPURequest); } }