Skip to main content
 首页 » 编程设计

c#之c#中的分段上传错误

2024年02月13日23zhoujg

当我将部分大小设置为 5MB 时,我尝试使用 AWS 上传文件,代码工作正常,但是当我尝试将部分大小更改为 1MB 时,出现异常: 代码是

            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)); 
            } 

异常(exception)情况是:

<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 的文件,可以吗?

谢谢

请您参考如下方法:

S3 上传的最小部分大小为 5 MB。

http://docs.aws.amazon.com/AmazonS3/latest/dev/qfacts.html

显然,最后一部分通常较小,这很好。

“最后一部分”可以更小这一事实的一个副作用是,例如,如果您有一个 500 KB 的文件并且您将其作为分段上传发送,则“第一部分”也是“最后一部分”部分”,这仍然有效,因为它满足“最后一部分”可以小于 5MB 的规则,但您仍然必须明确使用不小于 5MB 的部分大小。

我在测试默认为 64MB 的分段上传时发现了这一点。它仍然适用于较小的文件,这些文件作为小于 5MB 的单个“部分”进行分段上传。