问题描述
我现在用的是官方的Amazon S3的SDK(1.0.14.1)的最新版本,以创建一个备份工具。到目前为止,一切正常,如果正常的文件,我上传的尺寸小于5 MB,但是当任何一个文件高于5 MB上传失败,出现以下异常:
注意:5 MB大致是失效的边界,它可以略低或任何更高
我假设的连接超时的流文件上传完成之前被自动关闭。
我试图找到一种方法来设置一个长超时(但我无法找到在任何 AmazonS3
或 AmazonS3Config )。
如何增加超时(如应用程序范围的设置,我可以使用),或者是任何想法是无关的超时问题?
code:的
VAR s3Client = AWSClientFactory.CreateAmazonS3Client(AwsAccessKey,AwsSecretKey);
VAR putObjectRequest =新PutObjectRequest {
BucketName =桶,
文件路径= sourceFileName,
重点=目的地文件,
MD5Digest = md5Base64,
GenerateMD5Digest =真
};
使用(VAR上传= s3Client.PutObject(putObjectRequest)){}
解决方案
更新答案:
我最近更新了使用亚马逊AWS .NET SDK我的项目之一(为版本的 1.4.1.0 ),并在这个版本那里时,我写了原来的答复是根本不存在有两个方面的改进在这里。
- 您现在可以设置
超时
到 1
有无限的时间限制put操作。 - 在上有
PutObjectRequest
现在是一个额外的属性名为 ReadWriteTimeout
可以设置(毫秒)超时的而非整个put操作水平流的读/写电平。
所以,我的code现在看起来是这样的:
VAR putObjectRequest =新PutObjectRequest {
BucketName =桶,
文件路径= sourceFileName,
重点=目的地文件,
MD5Digest = md5Base64,
GenerateMD5Digest = TRUE,
超时= -1,
ReadWriteTimeout = 300000 // 5分钟以毫秒
};
原来的答复:
我设法找出答案......
在发布我曾探讨这个问题 AmazonS3
和 AmazonS3Config
而不是 PutObjectRequest
。
在 PutObjectRequest
有一个超时
财产(以毫秒为单位设置)。我已经成功地使用这种上传的文件。(注:如果设置为0不清除超时,需要指定毫秒的正数......我已经走了1小时)
这工作得很好:
VAR putObjectRequest =新PutObjectRequest {
BucketName =桶,
文件路径= sourceFileName,
重点=目的地文件,
MD5Digest = md5Base64,
GenerateMD5Digest = TRUE,
超时= 3600000
};
I am using the latest version of the official Amazon S3 SDK (1.0.14.1) to create a backup tool. So far everything works correctly if the size of the file I'm uploading is below 5 MB, but when any of the files is above 5 MB the upload fails with the following exception:
Note: 5 MB is roughly the boundary of failure, it can be slightly lower or anything higher
I am assuming that the connection is timing out and the stream is being automatically closed before the file upload completes.
I've tried to find a way to set a long timeout (but I can't find the option in either AmazonS3
or AmazonS3Config
).
Any ideas on how to increase the time-out (like an application wide setting I can use) or is it unrelated to a timeout issue?
Code:
var s3Client = AWSClientFactory.CreateAmazonS3Client(AwsAccessKey, AwsSecretKey);
var putObjectRequest = new PutObjectRequest {
BucketName = Bucket,
FilePath = sourceFileName,
Key = destinationFileName,
MD5Digest = md5Base64,
GenerateMD5Digest = true
};
using (var upload = s3Client.PutObject(putObjectRequest)) { }
解决方案 Updated answer:
I recently updated one of my projects that uses the Amazon AWS .NET SDK (to version 1.4.1.0) and in this version there are two improvements that did not exist when I wrote the original answer here.
- You can now set
Timeout
to -1
to have an infinite time limit for the put operation. - There is now an extra property on
PutObjectRequest
called ReadWriteTimeout
which can be set (in milliseconds) to timeout on the stream read/write level opposed to the entire put operation level.
So my code now looks like this:
var putObjectRequest = new PutObjectRequest {
BucketName = Bucket,
FilePath = sourceFileName,
Key = destinationFileName,
MD5Digest = md5Base64,
GenerateMD5Digest = true,
Timeout = -1,
ReadWriteTimeout = 300000 // 5 minutes in milliseconds
};
Original answer:
I managed to figure out the answer...
Before posting the question I had explored AmazonS3
and AmazonS3Config
but not PutObjectRequest
.
Inside PutObjectRequest
there is a Timeout
property (set in milliseconds). I have successfully used this to upload the larger files (note: setting it to 0 doesn't remove the timeout, you need to specify a positive number of miliseconds... I've gone for 1 hour).
This works fine:
var putObjectRequest = new PutObjectRequest {
BucketName = Bucket,
FilePath = sourceFileName,
Key = destinationFileName,
MD5Digest = md5Base64,
GenerateMD5Digest = true,
Timeout = 3600000
};
这篇关于如何上传文件到Amazon S3(官方SDK),比5 MB(近似值)大?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!