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


C# AmazonS3Client.ListMultipartUploads方法代码示例

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


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

示例1: 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


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