开发者社区> 问答> 正文

uploadPart遇到CannotaccessaclosedStream

代码如下,亲有何建议?


static void UploadBig(Bucket bucket, OssClient client)
        {
            InitiateMultipartUploadRequest initRequest = new InitiateMultipartUploadRequest (bucket.Name, Guid.NewGuid().ToString());
            var initResult = client.InitiateMultipartUpload(initRequest);
            string uploadId = initResult.UploadId;


            List<PartETag> tags = new List<PartETag> ();
            using (FileStream fs = File.OpenRead(@"E:\work\Database\surveyMPSDB_Demo-Full Database Backup.bak"))
            {                
                long partCount = 6*1024*1024;                
                long count = fs.Length % partCount == 0 ? fs.Length / partCount : fs.Length / partCount   1;
                List<Task> tasks = new List<Task>();                
                for (long i = 1; i <= count;   i)
                {
                    UploadPartRequest request = new UploadPartRequest(bucket.Name, Guid.NewGuid().ToString(), uploadId);
                    Byte[] content = new Byte[partCount];
                    int readCount = fs.Read(content, 0, (int)partCount);
                    MemoryStream stream = new MemoryStream (content, 0, readCount);
                    stream.Seek(0, SeekOrigin.Begin);
                    request.InputStream = stream;
                    request.PartNumber = (int)i;
                    request.PartSize = readCount;


                    var upResult = client.UploadPart(request);
                    tags.Add(upResult.PartETag);
                    //var t = Task.Factory.StartNew(() =>
                    //{

                    //}
                    //);
                    //tasks.Add(t);
                }
                //Task.WaitAll(tasks.ToArray());
            }
            CompleteMultipartUploadRequest comRequest = new CompleteMultipartUploadRequest (bucket.Name, Guid.NewGuid().ToString(), uploadId);



            var comResult = client.CompleteMultipartUpload(comRequest);
        }

展开
收起
bestlimao 2013-08-08 17:32:49 8889 0
3 条回答
写回答
取消 提交回答
  • 这个 有点难度 。。。
    2013-08-09 17:08:36
    赞同 展开评论 打赏
  • 此贴,悬赏求解

    我会对楼主选出的最佳答案给20-50云币奖励

    (云币可用于论坛活动的抽奖等)

    请大家多多支持哦~~
    2013-08-09 11:07:59
    赞同 展开评论 打赏
  • ReuploadPart遇到CannotaccessaclosedStream
    没人用过吗?补充一些http请求的数据
    ---------------------------------------InitiateMultipartUpload的时候返回的http:-----------------------
    HTTP/1.1 200 OK
    Date: Fri, 09 Aug 2013 02:21:53 GMT
    Content-Type: application/xml
    Connection: close
    Content-Length: 245
    Server: AliyunOSS
    x-oss-request-id: 52045241493FCD675E644BCB

    <?xml version="1.0" encoding="UTF-8"?>
    <InitiateMultipartUploadResult>
      <Bucket>bucket-mao-sms1</Bucket>
      <Key>7c34b682-98dc-4f50-bb40-f16c2ea04aa3</Key>
      <UploadId>A07DE2D002D64BAF82C2AE1B37F32F0E</UploadId>
    </InitiateMultipartUploadResult>

    --------------------uploadpart 返回的http:-----------------------------------
    HTTP/1.1 404 Not Found
    Content-Type: application/xml
    Connection: close
    Content-Length: 380
    Date: Fri, 09 Aug 2013 02:22:06 GMT
    Server: AliyunOSS
    x-oss-request-id: 5204524E49EDCC20189CE806

    <?xml version="1.0" encoding="UTF-8"?>
    <Error>
      <Code>NoSuchUpload</Code>
      <Message>The specified upload does not exist. The upload ID may be invalid, or the upload may have been aborted or completed.</Message>
      <UploadId>A07DE2D002D64BAF82C2AE1B37F32F0E</UploadId>
      <RequestId>5204524E49EDCC20189CE806</RequestId>
      <HostId>bucket-mao-sms1.oss.aliyuncs.com</HostId>
    </Error>

    -------------------------

    ReuploadPart遇到CannotaccessaclosedStream
    接到阿里开发的电话,解决了部分问题。在Multipleupload过程中key需要保持不变。
    谢谢阿里的及时处理,也希望API文档中能够说明一下。

    -------------------------

    ReuploadPart遇到CannotaccessaclosedStream
    这个是没有问题的代码,希望对以后有需要的人有所帮助。谢谢ali的开发的支持。

    static void UploadBig(Bucket bucket, OssClient client)
            {
                string uploadKey = Guid.NewGuid().ToString();
                InitiateMultipartUploadRequest initRequest = new InitiateMultipartUploadRequest(bucket.Name, uploadKey);
                var initResult = client.InitiateMultipartUpload(initRequest);
                string uploadId = initResult.UploadId;

                ConcurrentBag<PartETag> tags = new ConcurrentBag<PartETag>();
                using (FileStream fs = File.OpenRead(@"E:\work\Database\surveyMPSDB_Demo-Full Database Backup.bak"))
                {                
                    long partCount = 6*1024*1024;                
                    long count = fs.Length % partCount == 0 ? fs.Length / partCount : fs.Length / partCount   1;
                    List<Task> tasks = new List<Task>();
                    for (long i = 1; i <= count;   i)
                    {
                        UploadPartRequest request = new UploadPartRequest(bucket.Name, uploadKey, uploadId);
                        Byte[] content = new Byte[partCount];
                        int readCount = fs.Read(content, 0, (int)partCount);
                        MemoryStream stream = new MemoryStream (content, 0, readCount);
                        stream.Seek(0, SeekOrigin.Begin);
                        request.InputStream = stream;
                        request.PartNumber = (int)i;
                        request.PartSize = readCount;
                      
                        var t = Task.Factory.StartNew(() =>
                        {
                            var upResult = client.UploadPart(request);
                            tags.Add(upResult.PartETag);
                        }
                        );
                        tasks.Add(t);
                    }
                    Task.WaitAll(tasks.ToArray());
                }
                CompleteMultipartUploadRequest comRequest = new CompleteMultipartUploadRequest (bucket.Name, uploadKey, uploadId);
                foreach (var x in tags)
                {
                    comRequest.PartETags.Add(x);
                }
                var comResult = client.CompleteMultipartUpload(comRequest);
            }
    2013-08-09 10:32:56
    赞同 展开评论 打赏
问答分类:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
Sketching Data With T-Digest in Apache Spark 立即下载
Sparksheet - Transforming Spreadsheets into Spark Data Frames 立即下载
Speeding up Spark with Data Co 立即下载