当前位置: 首页>>代码示例>>C#>>正文


C# AmazonS3Client.AbortMultipartUpload方法代码示例

本文整理汇总了C#中Amazon.S3.AmazonS3Client.AbortMultipartUpload方法的典型用法代码示例。如果您正苦于以下问题:C# AmazonS3Client.AbortMultipartUpload方法的具体用法?C# AmazonS3Client.AbortMultipartUpload怎么用?C# AmazonS3Client.AbortMultipartUpload使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Amazon.S3.AmazonS3Client的用法示例。


在下文中一共展示了AmazonS3Client.AbortMultipartUpload方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: AbortMultipartUpload

        private void AbortMultipartUpload(MultipartUpload u, AmazonS3Client client)
        {
            var request = new AbortMultipartUploadRequest
            {
                BucketName = _bucket,
                Key = u.Key,
                UploadId = u.UploadId,
            };

            client.AbortMultipartUpload(request);
        }
开发者ID:Inzaghi2012,项目名称:teamlab.v7.5,代码行数:11,代码来源:S3UploadGuard.cs

示例2: Main

        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));
            }
        }
开发者ID:Santhoshonet,项目名称:VersaVaultSyncTool,代码行数:96,代码来源:Program.cs

示例3: UploadFile

        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,
//.........这里部分代码省略.........
开发者ID:tanaka-takayoshi,项目名称:CloudStorageExplorer,代码行数:101,代码来源:S3Model.cs


注:本文中的Amazon.S3.AmazonS3Client.AbortMultipartUpload方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。