本文介绍了亚马逊 s3 上传文件超时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 800KB 的 JPG 文件.我尝试上传到 S3 并不断收到超时错误.你能弄清楚出了什么问题吗?800KB 的上传很小.

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.

错误消息:您与服务器的套接字连接在超时期限内未被读取或写入.空闲连接将被关闭.

HTTP 状态码:400

HTTP Status Code: 400

AWS 错误代码:RequestTimeout

AWS Error Code: RequestTimeout

Long contentLength = null;
System.out.println("Uploading a new object to S3 from a file
");
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));

推荐答案

是否有可能 IOUtils.toByteArray 正在耗尽您的输入流,以便在进行服务调用时没有更多数据可供读取?在这种情况下,stream.reset() 可以解决这个问题.

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.

但是如果你只是上传一个文件(而不是任意的 InputStream),你可以使用更简单的 AmazonS3.putObject() 形式,它接受一个文件,然后你就不需要计算内容长度完全没有.

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.

http://docs.amazonwebservices.com/AWSJavaSDK/latest/javadoc/com/amazonaws/services/s3/AmazonS3.html#putObject(java.lang.String, java.lang.String, java.io.文件)

http://docs.amazonwebservices.com/AWSJavaSDK/latest/javadoc/com/amazonaws/services/s3/AmazonS3.html#putObject(java.lang.String, java.lang.String, java.io.File)

这将多次自动重试任何此类网络错误.您可以通过使用 ClientConfiguration 对象实例化客户端来调整客户端使用的重试次数.

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.

http://docs.amazonwebservices.com/AWSJavaSDK/latest/javadoc/com/amazonaws/ClientConfiguration.html#setMaxErrorRetry(int)

这篇关于亚马逊 s3 上传文件超时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 03:11