在C#中多部分上传错误 [英] multipart upload error in c#

查看:262
本文介绍了在C#中多部分上传错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用AWS时,我设置了零件尺寸为5MB的code正常工作,要上传的文件,但是当我试图改变零件尺寸为1MB它给了我一个例外: 在code是

I am trying to upload a file using AWS when i set the part size as 5MB the code works fine, but when i try to change the part size to 1MB it gives me an exception: The code is

            string strusername = "user1";       
            strlocalpath = "C:\\file1.zip";    
            string BUCKET_NAME = "bucket1";
            string filename = "file1.zip"
            string keypath = strusername + "/" + filename;
            string keyName = "123";
            string filePath = strlocalpath;
            // List to store upload part responses.
            List<UploadPartResponse> uploadResponses = new List<UploadPartResponse>();

            // 1. Initialize.
            InitiateMultipartUploadRequest initRequest =
                new InitiateMultipartUploadRequest()
                .WithBucketName(BUCKET_NAME)
                .WithKey(keyName);

            InitiateMultipartUploadResponse initResponse =
                s3Client.InitiateMultipartUpload(initRequest);

            // 2. Upload Parts.
            long contentLength = new FileInfo(filePath).Length;
            //Set Part size 
            long partSize = 1*1024*1024; // 5 MB

            try
            {
                long filePosition = 0;
                for (int i = 1; filePosition < contentLength; i++)
                {

                    if (filePosition + partSize > contentLength)
                    {
                        partSize = contentLength - filePosition;
                    }
                    // Create request to upload a part.
                    UploadPartRequest uploadRequest = new UploadPartRequest()
                        .WithBucketName(BUCKET_NAME)
                        .WithKey(keyName)
                        .WithUploadId(initResponse.UploadId)
                        .WithPartNumber(i)
                        .WithPartSize(partSize)
                        .WithFilePosition(filePosition)
                        .WithFilePath(filePath)
                        .WithTimeout(60*60*60);

                    // Upload part and add response to our list.
                    uploadResponses.Add(s3Client.UploadPart(uploadRequest));

                    filePosition += partSize;



                    Console.WriteLine("\nTotal uploaded size = " + filePosition.ToString());
                }

                // Step 3: complete.
                CompleteMultipartUploadRequest compRequest =
                    new CompleteMultipartUploadRequest()
                    .WithBucketName(BUCKET_NAME)
                    .WithKey(keyName)
                    .WithUploadId(initResponse.UploadId)
                    .WithPartETags(uploadResponses);

                CompleteMultipartUploadResponse completeUploadResponse =
                    s3Client.CompleteMultipartUpload(compRequest);

            }
            catch (Exception exception)
            {
                Console.WriteLine("Exception occurred: {0}", exception.Message);
                s3Client.AbortMultipartUpload(new AbortMultipartUploadRequest()
                    .WithBucketName(BUCKET_NAME)
                    .WithKey(keyName)
                    .WithUploadId(initResponse.UploadId));
            }

唯一的例外是:

The exception is:

<Error>
<Code>EntityTooSmall</Code>
<Message>Your proposed upload is smaller than the minimum allowed size</Message>   
<ETag>d9c00192bcf6bf7412814a8fe0422b0c</ETag>
<MinSizeAllowed>5242880</MinSizeAllowed>
<ProposedSize>1048576</ProposedSize>
<RequestId>PBG04E031C012F34</RequestId>
<HostId>VEjvpkjuk89yS4xW6Bl/+NPpb3yxvbbe7ijjPmTrlXc7hnjj89kjkm</HostId> 
<PartNumber>1</PartNumber></Error>

我要上传的文件中的1 MB块或2MB是有可能做到这一点?

I want to upload the file in chunks of 1 MB or 2MB is it possible to do that?

感谢

推荐答案

在最小的部件尺寸为S3上传为5 MB。

The minimum part size for S3 uploads is 5 MB.

http://docs.aws.amazon.com/AmazonS3/最新的/ dev / qfacts.html

显然,最后部分通常是较小的,这是好的。

Obviously, the last part will usually be smaller, which is fine.

的事实,即最后部分可以更小的副作用是,如果你有,例如,一个500 KB的文件,你把它作为一个多部分上传,第一部分也是最后部分,这仍然有效,因为它满足了规则最后部分可以比5MB小,但是你还是要EX pressly使用部分尺寸不小于5 MB。

A side effect of the fact that the "last part" can be smaller is that if you have, for example, a 500 KB file and you send it as a multipart upload, the "first part" is also the "last part" and this still works, because it satisfies the rule that the "last part" can be smaller than 5MB, but you still have to expressly use a part-size not smaller than 5 MB.

我发现了这个在测试一个多上传,默认为64MB。它仍然在小文件,它得到的多部分上传作为一个单一的一部分,这是小于5MB正常工作。

I discovered this while testing a multipart-uploaded that defaults to 64MB. It still works fine on smaller files, which get multipart-uploaded as a single "part" which is less than 5MB.

这篇关于在C#中多部分上传错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆