• 阿里云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();
            }
        }


  • 相关阅读:
    谈谈MVC项目中的缓存功能设计的相关问题
    MVC 3.0 在各个版本IIS中的部署
    从零开始学习jQuery (十一) 实战表单验证与自动完成提示插件
    从零开始学习jQuery (十) jQueryUI常用功能实战
    从零开始学习jQuery (九) jQuery工具函数
    从零开始学习jQuery (八) 插播:jQuery实施方案
    从零开始学习jQuery (七) jQuery动画-让页面动起来!
    从零开始学习jQuery (六) AJAX快餐
    从零开始学习jQuery (五) 事件与事件对象
    从零开始学习jQuery (四) 使用jQuery操作元素的属性与样式
  • 原文地址:https://www.cnblogs.com/AI001/p/3996843.html
Copyright © 2020-2023  润新知