zoukankan      html  css  js  c++  java
  • .net core 3.1 分片上传

     public class xxxHelper
        {
            private readonly AmazonS3Client client;
            public xxxHelper()
            {
      
                string accessKey = "accessKey ";     //自己设置的accessKey
    string secretKey = "secretKey "; //自己设置的secretkey AmazonS3Config config = new AmazonS3Config(); config.ServiceURL = "ServiceURL "; config.SignatureVersion = "SignatureVersion "; client = new AmazonS3Client(accessKey, secretKey, config); } public async void UploadObject(string fileName, string cdnPath, string filePath) { // Create list to store upload part responses. List<UploadPartResponse> uploadResponses = new List<UploadPartResponse>(); // Setup information required to initiate the multipart upload. InitiateMultipartUploadRequest initiateRequest = new InitiateMultipartUploadRequest { BucketName = cdnPath, Key = fileName }; // Initiate the upload. // InitiateMultipartUploadResponse initResponse = client.InitiateMultipartUpload(initiateRequest); .net framework 版本的方法 var initResponse = await client.InitiateMultipartUploadAsync(initiateRequest); //.net core 3.1都是异步方法,需要用await,否则报错,initResponse没有UploadId这个属性
    //
    Upload parts. long contentLength = new FileInfo(filePath).Length; long partSize = 5 * (long)Math.Pow(2, 20); // 5 MB try { long filePosition = 0; for (int i = 1; filePosition < contentLength; i++) { UploadPartRequest uploadRequest = new UploadPartRequest { BucketName = cdnPath, Key = fileName, UploadId = initResponse.UploadId, PartNumber = i, PartSize = partSize, FilePosition = filePosition, FilePath = filePath }; // Track upload progress. uploadRequest.StreamTransferProgress += new EventHandler<StreamTransferProgressArgs>(UploadPartProgressEventCallback); // Upload a part and add the response to our list. var xwaitUpload = await client.UploadPartAsync(uploadRequest); uploadResponses.Add(xwaitUpload); filePosition += partSize; } // Setup to complete the upload. CompleteMultipartUploadRequest completeRequest = new CompleteMultipartUploadRequest { BucketName = cdnPath, Key = fileName, UploadId = initResponse.UploadId.ToString() }; completeRequest.AddPartETags(uploadResponses); // Complete the upload. Task<CompleteMultipartUploadResponse> completeUploadResponse = client.CompleteMultipartUploadAsync(completeRequest); } catch (Exception exception) { AbortMultipartUploadRequest abortMPURequest = new AbortMultipartUploadRequest { BucketName = cdnPath, Key = fileName, UploadId = initResponse.UploadId.ToString() }; await client.AbortMultipartUploadAsync(abortMPURequest); } } public void UploadPartProgressEventCallback(object sender, StreamTransferProgressArgs e) { //Console.WriteLine("{0}/{1}", e.TransferredBytes, e.TotalBytes); System.Diagnostics.Debug.WriteLine("{2} {0}/{1}", e.TransferredBytes, e.TotalBytes, e.IncrementTransferred); }
  • 相关阅读:
    数据库子查询
    数据库简介
    MySql数据类型
    Jquery与js简单对比
    JS-对象
    JS-DOM
    JS-数组
    JS-函数
    JS-json
    JS-类型相关
  • 原文地址:https://www.cnblogs.com/carlpeng/p/13303617.html
Copyright © 2011-2022 走看看