文档中心 > 分片上传
分片上传

最近更新时间:2021-10-29

分片上传的每个part不得小于5MB。

以下代码用于分片上传文件。

using System;

using System.Collections.Generic;

using Amazon.S3;

using Amazon.S3.Model;

using System.IO;

using System.Diagnostics;

 

namespace PutLifecycleConfiguration

{

    class Program

    {

        static void Main(string[] args)

        {  

            var Ak = "xxx";

            var Sk = "xxx";

            var endpoint = "http://s3.test.com";

            var uploadFile = "xxx"; //The full path and name to a file to be uploaded

            var bucketName = "xxx";

            var objctName = "xxx";

            AmazonS3Client serviceClient = new AmazonS3Client(Ak,Sk,

            new AmazonS3Config{ ServiceURL = endpoint });

 

            try

            {  

                int MB = (int)Math.Pow(2, 20);

 

                // Define input stream

                FileStream inputStream = new FileStream(uploadFile,FileMode.Open);

                // Stream inputStream = Create13MBDataStream();

               

                // Initiate multipart upload

                InitiateMultipartUploadRequest initRequest = new InitiateMultipartUploadRequest

                {

                    BucketName = bucketName,

                    Key = objctName

                };

                InitiateMultipartUploadResponse initResponse = serviceClient.InitiateMultipartUploadAsync(initRequest).GetAwaiter().GetResult();

 

                Console.WriteLine("开始上传part1");

               

                // Upload part 1

                UploadPartRequest uploadRequest = new UploadPartRequest

                {

                    BucketName = bucketName,

                    Key = objctName,

                    UploadId = initResponse.UploadId,

                    PartNumber = 1,

                    PartSize = 5 * MB,

                    InputStream = inputStream

                };

                UploadPartResponse up1Response = serviceClient.UploadPartAsync(uploadRequest).GetAwaiter().GetResult();

               

                Console.WriteLine(up1Response.ETag);

                Console.WriteLine("开始上传part2");

               

                // Upload part 2

                uploadRequest = new UploadPartRequest

                {

                    BucketName = bucketName,

                    Key = objctName,

                    UploadId = initResponse.UploadId,

                    PartNumber = 2,

                    PartSize = 5 * MB,

                    InputStream = inputStream

                };

                UploadPartResponse up2Response = serviceClient.UploadPartAsync(uploadRequest).GetAwaiter().GetResult();

 

                Console.WriteLine(up2Response.ETag);

               

                Console.WriteLine("开始上传part3");

 

                // Upload part 3

                uploadRequest = new UploadPartRequest

                {

                    BucketName = bucketName,

                    Key = objctName,

                    UploadId = initResponse.UploadId,

                    PartNumber = 3,

                    InputStream = inputStream

                };

                UploadPartResponse up3Response = serviceClient.UploadPartAsync(uploadRequest).GetAwaiter().GetResult();

 

                Console.WriteLine(up3Response.ETag);

 

                Console.WriteLine("检查上传的分片");

               

                // List parts for current upload

                ListPartsRequest listPartRequest = new ListPartsRequest

                {

                    BucketName = bucketName,

                    Key = objctName,

                    UploadId = initResponse.UploadId

                };

                ListPartsResponse listPartResponse = serviceClient.ListPartsAsync(listPartRequest).GetAwaiter().GetResult();

                Debug.Assert(listPartResponse.Parts.Count == 3);

 

                Console.WriteLine("结束本次分片上传");

               

                // Complete the multipart upload

                CompleteMultipartUploadRequest compRequest = new CompleteMultipartUploadRequest

                {

                    BucketName = bucketName,

                    Key = objctName,

                    UploadId = initResponse.UploadId,

                    PartETags = new List<PartETag>

                    {

                        new PartETag { ETag = up1Response.ETag, PartNumber = 1 },

                        new PartETag { ETag = up2Response.ETag, PartNumber = 2 },

                        new PartETag { ETag = up3Response.ETag, PartNumber = 3 }

                    }

                };

                CompleteMultipartUploadResponse compResponse = serviceClient.CompleteMultipartUploadAsync(compRequest).GetAwaiter().GetResult();

                           

                            } catch (AmazonS3Exception e) {

                                Console.WriteLine(e.Message);

                                throw;

                            } catch (Exception e) {

                                Console.WriteLine(e);

                                throw;

            }

 

        }

 

    }

}