java 亚马逊 s3 上传文件超时
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12774909/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
amazon s3 upload file time out
提问by user1688346
I have a JPG file with 800KB. I try to upload to S3 and keep getting timeout error. Can you please figure what is wrong? 800KB is rather small for upload.
我有一个 800KB 的 JPG 文件。我尝试上传到 S3 并不断收到超时错误。你能弄清楚出了什么问题吗?800KB 对于上传来说是相当小的。
Error Message: Your socket connection to the server was not read from or written to within the timeout period. Idle connections will be closed.
HTTP Status Code: 400
AWS Error Code: RequestTimeout
错误消息:您与服务器的套接字连接在超时期限内未被读取或写入。空闲连接将被关闭。
HTTP 状态代码:400
AWS 错误代码:RequestTimeout
Long contentLength = null;
System.out.println("Uploading a new object to S3 from a file\n");
try {
byte[] contentBytes = IOUtils.toByteArray(is);
contentLength = Long.valueOf(contentBytes.length);
} catch (IOException e) {
System.err.printf("Failed while reading bytes from %s", e.getMessage());
}
ObjectMetadata metadata = new ObjectMetadata();
metadata.setContentLength(contentLength);
s3.putObject(new PutObjectRequest(bucketName, key, is, metadata));
采纳答案by Zach Musgrave
Is it possible that IOUtils.toByteArray is draining your input stream so that there is no more data to be read from it when the service call is made? In that case a stream.reset() would fix the issue.
IOUtils.toByteArray 是否可能正在耗尽您的输入流,以便在进行服务调用时没有更多数据可供读取?在这种情况下,stream.reset() 可以解决这个问题。
But if you're just uploading a file (as opposed to an arbitrary InputStream), you can use the simpler form of AmazonS3.putObject() that takes a File, and then you won't need to compute the content length at all.
但是,如果您只是上传一个文件(而不是任意 InputStream),您可以使用更简单的 AmazonS3.putObject() 形式,它接受一个文件,然后您根本不需要计算内容长度。
http://docs.amazonwebservices.com/AWSJavaSDK/latest/javadoc/com/amazonaws/services/s3/AmazonS3.html#putObject(java.lang.String, java.lang.String, java.io.File)
http://docs.amazonwebservices.com/AWSJavaSDK/latest/javadoc/com/amazonaws/services/s3/AmazonS3.html#putObject(java.lang.String, java.lang.String, java.io.File)
This will automatically retry any such network errors several times. You can tweak how many retries the client uses by instantiating it with a ClientConfiguration object.
这将多次自动重试任何此类网络错误。您可以通过使用 ClientConfiguration 对象实例化客户端来调整客户端使用的重试次数。
回答by Gabriel Doty
If your endpoint is behind a VPC it will also silently error out. You can add a new VPC endpoint here for s3
如果您的终端节点位于 VPC 后面,它也会以静默方式出错。您可以在此处为 s3 添加新的 VPC 端点
https://aws.amazon.com/blogs/aws/new-vpc-endpoint-for-amazon-s3/
https://aws.amazon.com/blogs/aws/new-vpc-endpoint-for-amazon-s3/