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


C# AmazonS3Client.InitiateMultipartUpload方法代码示例

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


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

示例2: ObjectSamples


//.........这里部分代码省略.........
                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
开发者ID:rajdotnet,项目名称:aws-sdk-net,代码行数:67,代码来源:BaseS3Samples.cs

示例3: CreateMultiPartS3Blob

        internal static void CreateMultiPartS3Blob(AmazonS3Client client, string key, S3CopyMemoryStream stream)
        {
            if (stream.InitiatingPart) {

                InitiateMultipartUploadRequest initiateMultipartUploadRequest =
                    new InitiateMultipartUploadRequest()
                        .WithBucketName("static.getbrickpile.com")
                        .WithCannedACL(S3CannedACL.PublicRead)
                        .WithKey(key);

                InitiateMultipartUploadResponse initiateResponse =
                    client.InitiateMultipartUpload(initiateMultipartUploadRequest);
                stream.UploadPartId = initiateResponse.UploadId;

            }

            stream.Position = 0;

            UploadPartRequest uploadPartRequest =
                new UploadPartRequest()
                    .WithBucketName("static.getbrickpile.com")
                    .WithKey(key)
                    .WithPartNumber(stream.WriteCount)
                    .WithPartSize(stream.Position)
                    .WithUploadId(stream.UploadPartId)
                    .WithInputStream(stream) as UploadPartRequest;

            UploadPartResponse response = client.UploadPart(uploadPartRequest);
            PartETag etag = new PartETag(response.PartNumber, response.ETag);
            stream.PartETagCollection.Add(etag);

            if (stream.EndOfPart) {

                CompleteMultipartUploadRequest completeMultipartUploadRequest =
                    new CompleteMultipartUploadRequest()
                        .WithBucketName("static.getbrickpile.com")
                        .WithKey(key)
                        .WithPartETags(stream.PartETagCollection)
                        .WithUploadId(stream.UploadPartId);

                CompleteMultipartUploadResponse completeMultipartUploadResponse = client.CompleteMultipartUpload(completeMultipartUploadRequest);
                string loc = completeMultipartUploadResponse.Location;

            }
        }
开发者ID:andersgranberg,项目名称:BrickPile,代码行数:45,代码来源:AmazonS3VirtualFile.cs

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