断点续传:(存在问题)
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(); } }