当我将零件大小设置为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));
}
例外是:
<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的规则,但是您仍然必须明确使用不小于5 MB的部分大小。
我在测试默认为64MB的分段上传时发现了这一点。它在较小的文件上仍然可以正常工作,这些文件可以作为单个“部分”(小于5MB)进行多部分上传。
关于c# - C#中的分段上传错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/19634555/