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); }
  • 相关阅读:
    Ubuntu系统中安装Macaca过程记录
    Nightwatch——自动化测试(端对端e2e)
    junit 5 官方用户手册
    junit 5 与 testNG 使用对比
    认知体系——从“知道自己不知道”到“知道自己知道”的进化
    测试入门教程
    web UI 自动化变革龙测问世
    [黑苹果硬件] 实用黑苹果配置推荐
    windows使用vbs打开谷歌浏览器登录网页
    Note++ 的快捷
  • 原文地址:https://www.cnblogs.com/carlpeng/p/13303617.html
Copyright © 2011-2022 走看看