public AbortMultipartUpload ( |
||
request | Container for the necessary parameters to execute the AbortMultipartUpload service method. | |
Résultat |
private void AbortMultipartUpload(MultipartUpload u, AmazonS3Client client) { var request = new AbortMultipartUploadRequest { BucketName = _bucket, Key = u.Key, UploadId = u.UploadId, }; client.AbortMultipartUpload(request); }
public System.Tuple<IEnumerable<IObservable<PartUploadProgressChangedArgs>>, IObservable<bool>> UploadFile(string filePath, string bucket, string keyName) { var s3Client = new AmazonS3Client(GetCredentials()); List<UploadPartResponse> uploadResponses = new List<UploadPartResponse>(); // 1. Initialize. InitiateMultipartUploadRequest initRequest = new InitiateMultipartUploadRequest() .WithBucketName(bucket) .WithKey(keyName); InitiateMultipartUploadResponse initResponse = s3Client.InitiateMultipartUpload(initRequest); // 2. Upload Parts. long contentLength = new FileInfo(filePath).Length; long partSize = 5242880; // 5 MB var list = new List<UploadPartRequest>(); long filePosition = 0; int i = 1; while (filePosition < contentLength) { // Create request to upload a part. var uploadRequest = new UploadPartRequest() .WithBucketName(bucket) .WithKey(keyName) .WithUploadId(initResponse.UploadId) .WithPartNumber(i) .WithPartSize(partSize) .WithFilePosition(filePosition) .WithFilePath(filePath) .WithTimeout(3600000); list.Add(uploadRequest); filePosition += partSize; ++i; } var q = list //.Select(req => s3Client.UploadPart(req)) .Select(res => Observable.FromEvent<EventHandler<UploadPartProgressArgs>, PartUploadProgressChangedArgs>( h => (s, e) => h(new PartUploadProgressChangedArgs(res, e)), h => res.UploadPartProgressEvent += h, h => res.UploadPartProgressEvent -= h)); var q1 = Observable.Start<bool>(() => { try { var prs = Parallel.ForEach(list, res => uploadResponses.Add(s3Client.UploadPart(res))); if (prs.IsCompleted) { CompleteMultipartUploadRequest compRequest = new CompleteMultipartUploadRequest() .WithBucketName(bucket) .WithKey(keyName) .WithUploadId(initResponse.UploadId) .WithPartETags(uploadResponses); CompleteMultipartUploadResponse completeUploadResponse = s3Client.CompleteMultipartUpload(compRequest); // MessageBox.Show(completeUploadResponse.ResponseXml); return true; } s3Client.AbortMultipartUpload(new AbortMultipartUploadRequest() .WithBucketName(bucket) .WithKey(keyName) .WithUploadId(initResponse.UploadId)); return false; } catch(Exception e) { Console.WriteLine(e.StackTrace); s3Client.AbortMultipartUpload(new AbortMultipartUploadRequest() .WithBucketName(bucket) .WithKey(keyName) .WithUploadId(initResponse.UploadId)); return false; } }); //list.ToObservable(Scheduler.ThreadPool) // .Do(res => { uploadResponses.Add(s3Client.UploadPart(res)); }) // .Finally(() => // { // CompleteMultipartUploadRequest compRequest = // new CompleteMultipartUploadRequest() // .WithBucketName(bucket) // .WithKey(keyName) // .WithUploadId(initResponse.UploadId) // .WithPartETags(uploadResponses); // CompleteMultipartUploadResponse completeUploadResponse = // s3Client.CompleteMultipartUpload(compRequest); // }) // .Subscribe(); return Tuple.Create(q, q1); // return list; //return Observable.While(() => filePosition < contentLength, // Observable.Return(new UploadPartRequest() // .WithBucketName(bucket) // .WithKey(keyName) // .WithUploadId(initResponse.UploadId) // // .WithPartNumber(i) // // .WithPartSize(partSize) // // .WithFilePosition(filePosition) // .WithFilePath(filePath), Scheduler.ThreadPool) // .Do(req => // { // req.PartNumber = i; // req.PartSize = partSize; // req.FilePosition = filePosition; // uploadResponses.Add(s3Client.UploadPart(req)); // filePosition += partSize; // ++i; // })) // //.Do(req => // // { // // uploadResponses.Add(s3Client.UploadPart(req)); // // }) // .Finally(() => // { // CompleteMultipartUploadRequest compRequest = // new CompleteMultipartUploadRequest() // .WithBucketName(bucket) // .WithKey(keyName) // .WithUploadId(initResponse.UploadId) // .WithPartETags(uploadResponses); // CompleteMultipartUploadResponse completeUploadResponse = // s3Client.CompleteMultipartUpload(compRequest); // }); //while (filePosition < contentLength) //{ // // Create request to upload a part. // var uploadRequest = new UploadPartRequest() // .WithBucketName(bucket) // .WithKey(keyName) // .WithUploadId(initResponse.UploadId) // .WithPartNumber(i) // .WithPartSize(partSize) // .WithFilePosition(filePosition) // .WithFilePath(filePath); // yield return uploadRequest; // uploadResponses.Add(s3Client.UploadPart(uploadRequest)); // filePosition += partSize; // ++i; //} ////Observable.FromEvent<EventHandler<UploadPartProgressArgs>, PartUploadProgressChangedArgs>( //// h => (s, e) => h(new PartUploadProgressChangedArgs(uploadRequest, e)), //// h => uploadRequest.UploadPartProgressEvent += h, //// h => uploadRequest.UploadPartProgressEvent -= h); //// Upload part and add response to our list. //// Step 3: complete. //CompleteMultipartUploadRequest compRequest = // new CompleteMultipartUploadRequest() // .WithBucketName(bucket) // .WithKey(keyName) // .WithUploadId(initResponse.UploadId) // .WithPartETags(uploadResponses); //CompleteMultipartUploadResponse completeUploadResponse = // s3Client.CompleteMultipartUpload(compRequest); //try //{ //} //catch (Exception exception) //{ // Console.WriteLine("Exception occurred: {0}", exception.Message); // s3Client.AbortMultipartUpload(new AbortMultipartUploadRequest() // .WithBucketName(bucket) // .WithKey(keyName) // .WithUploadId(initResponse.UploadId)); //} //return list.ToObservable(); }
static void Main() { _accessKeyId = Utilities.AwsAccessKey; _secretAccessKey = Utilities.AwsSecretKey; AmazonS3 s3Client = new AmazonS3Client(_accessKeyId, _secretAccessKey); ListMultipartUploadsRequest allMultipartUploadsRequest = new ListMultipartUploadsRequest().WithBucketName(ExistingBucketName); ListMultipartUploadsResponse mpUploadsResponse = s3Client.ListMultipartUploads(allMultipartUploadsRequest); var objects = new List<Object>(); foreach (MultipartUpload multipartUpload in mpUploadsResponse.MultipartUploads) { bool isObjectdFound = false; foreach (Object o in objects) { if (o.UploadId == multipartUpload.UploadId) { o.Parts.Add(new Part { PartId = o.Parts.Count, Etag = "" }); isObjectdFound = true; } } if (!isObjectdFound) { objects.Add(new Object { Parts = new List<Part> { new Part() { Etag = "", PartId = 1 } }, UploadId = multipartUpload.UploadId }); } } var result = JsonConvert.SerializeObject(objects); var objs = JsonConvert.DeserializeObject<List<Object>>(result); //return; // List to store upload part responses. var uploadResponses = new List<UploadPartResponse>(); byte[] bytes; long contentLength = 0; using (var fileStream = new FileStream(FilePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) { contentLength = fileStream.Length; bytes = new byte[contentLength]; fileStream.Read(bytes, 0, Convert.ToInt32(contentLength)); } // 1. Initialize. InitiateMultipartUploadRequest initiateRequest = new InitiateMultipartUploadRequest().WithBucketName(ExistingBucketName).WithKey(KeyName); InitiateMultipartUploadResponse initResponse = s3Client.InitiateMultipartUpload(initiateRequest); try { // 2. Upload Parts. long partSize = 5 * (long)Math.Pow(2, 20); // 5 MB long filePosition = 0; for (int i = 1; filePosition < contentLength; i++) { byte[] bytesToStream; if (filePosition + partSize < contentLength) { bytesToStream = new byte[partSize]; Array.Copy(bytes, filePosition, bytesToStream, 0, partSize); } else { bytesToStream = new byte[contentLength - filePosition]; Array.Copy(bytes, filePosition, bytesToStream, 0, contentLength - filePosition); } Stream stream = new MemoryStream(bytesToStream); // Create request to upload a part. UploadPartRequest uploadRequest = new UploadPartRequest() .WithBucketName(ExistingBucketName) .WithKey(KeyName) .WithUploadId(initResponse.UploadId) .WithPartNumber(i) .WithPartSize(partSize) .WithFilePosition(filePosition) .WithTimeout(1000000000) .WithMD5Digest(Convert.ToBase64String(MD5.Create().ComputeHash(bytesToStream))); uploadRequest.WithInputStream(stream); // Upload part and add response to our list. uploadResponses.Add(s3Client.UploadPart(uploadRequest)); filePosition += partSize; } // Step 3: complete. CompleteMultipartUploadRequest completeRequest = new CompleteMultipartUploadRequest() .WithBucketName(ExistingBucketName) .WithKey(KeyName) .WithUploadId(initResponse.UploadId) .WithPartETags(uploadResponses); CompleteMultipartUploadResponse completeUploadResponse = s3Client.CompleteMultipartUpload(completeRequest); Console.WriteLine(completeUploadResponse.ETag); } catch (Exception exception) { Console.WriteLine("Exception occurred: {0}", exception.Message); s3Client.AbortMultipartUpload(new AbortMultipartUploadRequest() .WithBucketName(ExistingBucketName) .WithKey(KeyName) .WithUploadId(initResponse.UploadId)); } }