private void AbortMultipartUpload(MultipartUpload u, AmazonS3Client client) { var request = new AbortMultipartUploadRequest { BucketName = bucket, Key = u.Key, UploadId = u.UploadId, }; client.AbortMultipartUpload(request); }
/// <summary> /// Tell the server to cancel the upload with this filename and uploadId /// </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> /// <returns>Response from S3 server.</returns> public static AbortMultipartUploadResponse AbortUpload( AmazonS3Client s3Client, string uploadTarget, string fileName, string uploadId) { string fileKey = GetFileKey(uploadTarget, fileName); AbortMultipartUploadRequest abortMPURequest = new AbortMultipartUploadRequest { BucketName = Common.UploadBucketName, Key = fileKey, UploadId = uploadId }; return(s3Client.AbortMultipartUpload(abortMPURequest)); }
/// <summary> /// Tell the server to cancel the upload with this filename and uploadId /// </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> /// <returns>Response from S3 server.</returns> public static AbortMultipartUploadResponse AbortUpload( AmazonS3Client s3Client, string uploadTarget, string fileName, string uploadId) { if (s3Client == null || uploadTarget == null || fileName == null || uploadId == null) { throw new InvalidDataException(); } string fileKey = GetFileKey(uploadTarget, fileName); AbortMultipartUploadRequest abortMPURequest = new AbortMultipartUploadRequest { BucketName = Common.UploadBucketName, Key = fileKey, UploadId = uploadId }; return(s3Client.AbortMultipartUpload(abortMPURequest)); }
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 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); } }