Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
462 views
in Technique[技术] by (71.8m points)

java - amazon s3 upload file time out

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.

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

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));
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.

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.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.

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...