阿里云oss的net断点续传demo

断点续传:(存在问题)

  class Program
    {
        static void Main(string[] args)
        {
            String bucketName = "自己的bucket名";
            String key = "阿里云的key";
            String accessId = "阿里云的id";
            String localPath = @"本地文件路径";

            // 初始化OSSClient
            OssClient client = new OssClient(accessId, key);

            // 开始Multipart Upload
            InitiateMultipartUploadRequest initiateMultipartUploadRequest =
                                                new InitiateMultipartUploadRequest(bucketName, key);           
            InitiateMultipartUploadResult initiateMultipartUploadResult =
                                                client.InitiateMultipartUpload(initiateMultipartUploadRequest);

            // 打印UploadId
            Console.WriteLine("UploadId: " + initiateMultipartUploadResult.UploadId);
            // 设置每块为 5M
            int partSize = 1024 * 1024 * 5;

            Stream partFile = File.OpenRead(localPath);

            // 计算分块数目
            int partCount = (int)(partFile.Length / partSize);
            if (partFile.Length % partSize != 0)
            {
                partCount++;
            }

            // 新建一个List保存每个分块上传后的ETag和PartNumber
            List<PartETag> partETags = new List<PartETag>();

            for (int i = 0; i < partCount; i++)
            {

                // 跳到每个分块的开头
                long skipBytes = partSize * i;

                // 计算每个分块的大小
                long size = partSize < partFile.Length - skipBytes ?
                        partSize : partFile.Length - skipBytes;

                // 创建UploadPartRequest,上传分块
                UploadPartRequest uploadPartRequest = new UploadPartRequest(bucketName, key, initiateMultipartUploadResult.UploadId);
                uploadPartRequest.InputStream = partFile;
                uploadPartRequest.PartSize = size;
                uploadPartRequest.PartNumber = i + 1;
                UploadPartResult uploadPartResult = client.UploadPart(uploadPartRequest);

                // 将返回的PartETag保存到List中。
                partETags.Add(uploadPartResult.PartETag);
            }
            partFile.Close();
            CompleteMultipartUploadRequest completeMultipartUploadRequest =
        new CompleteMultipartUploadRequest(bucketName, key, initiateMultipartUploadResult.UploadId);

            // 完成分块上传
            CompleteMultipartUploadResult completeMultipartUploadResult =
                    client.CompleteMultipartUpload(completeMultipartUploadRequest);

            // 打印Object的ETag
            Console.WriteLine(completeMultipartUploadResult.ETag);
            ListPartsRequest listPartsRequest = new ListPartsRequest(bucketName, key, initiateMultipartUploadResult.UploadId);
            Console.WriteLine(listPartsRequest.Key);
            Console.ReadKey();
        }
    }


评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值