开发者社区> 问答> 正文

OSS分段上传连接超时

#region 分块上传方法
        /// <summary>
        /// 分块上传方法
        /// </summary>
        /// <param name="fullFilePath">要上传的文件路径</param>
        /// <param name="endPoint"></param>
        /// <param name="accessKeyID"></param>
        /// <param name="accessKeySecret"></param>
        /// <param name="bucketName"></param>
        /// <param name="key"></param>
        public static void MutiPartUpload(string fullFilePath, string endPoint, string accessKeyID , string accessKeySecret, string bucketName, string key)
        {

            OssClient ossClient = new OssClient(endPoint, accessKeyID, accessKeySecret);
            InitiateMultipartUploadRequest initRequest =
                            new InitiateMultipartUploadRequest(bucketName, key);
           InitiateMultipartUploadResult initResult = ossClient.InitiateMultipartUpload(initRequest);   //总是在这句 连接超时返回错误
            // 设置每块为 5M  
            int partSize = 1024 * 1024 * 5;
            FileInfo partFile = new FileInfo(fullFilePath);
            // 计算分块数目  
            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++)
            {
                // 获取文件流  
                using (FileStream fis = new FileStream(partFile.FullName, FileMode.Open))
                {

                    // 跳到每个分块的开头  
                    long skipBytes = partSize * i;
                    fis.Position = skipBytes;
                    // fis.skip(skipBytes);
                    // 计算每个分块的大小  
                    long size = partSize < partFile.Length - skipBytes ?
                            partSize : partFile.Length - skipBytes;
                    // 创建UploadPartRequest,上传分块  
                    UploadPartRequest uploadPartRequest = new UploadPartRequest(bucketName, key
                        , initResult.UploadId);
                    uploadPartRequest.InputStream = fis;
                    uploadPartRequest.PartSize = size;
                    uploadPartRequest.PartNumber = (i + 1);
                    UploadPartResult uploadPartResult = ossClient.UploadPart(uploadPartRequest);
                    // 将返回的PartETag保存到List中。  
                    partETags.Add(uploadPartResult.PartETag);
                }
            }

            CompleteMultipartUploadRequest completeReq = new CompleteMultipartUploadRequest(bucketName, key, initResult.UploadId);
            foreach (PartETag partETag in partETags)
            {
                completeReq.PartETags.Add(partETag);
            }

            //完成分块上传  
            CompleteMultipartUploadResult completeResult = ossClient.CompleteMultipartUpload(completeReq);
            // 返回最终文件的MD5,用于用户进行校验  
            //  Console.WriteLine(completeResult.ETag);
        }
        #endregion






展开
收起
lw2016 2014-11-03 14:14:49 11212 0
2 条回答
写回答
取消 提交回答
  • Connecttomediagzzd.oss-cn-shenzhen.aliyuncs.com80timedout
    连接的时候出现80 time out
    2015-07-17 11:01:32
    赞同 展开评论 打赏
  • LT是个伪程序员
    检查一下你的endPoint, accessKeyID, accessKeySecret参数,还有你的bucketName, key参数
    2014-11-03 20:16:01
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
OSS运维进阶实战手册 立即下载
《OSS运维基础实战手册》 立即下载
OSS运维基础实战手册 立即下载