当我将部分大小设置为 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 或 2 MB 的块上传文件,可以这样做吗?
谢谢