Unable to execute HTTP request: Read timed out
why i use this api when i upload parts file like:
UploadPartRequest uploadRequest = new UploadPartRequest()
.withBucketName(bucketName)
.withKey(fileName)
.withUploadId(uploadId)
.withPartNumber(currentPart + 1)
.withFileOffset(offset)
.withInputStream(new ByteArrayInputStream(dataMap.get("file")))
.withPartSize(partSize);
s3 throw read timed out exception;
but i used without withFileOffset(offset) ,service is ok. like
UploadPartRequest uploadRequest = new UploadPartRequest()
.withBucketName(bucketName)
.withKey(fileName)
.withUploadId(uploadId)
.withPartNumber(currentPart + 1)
.withInputStream(new ByteArrayInputStream(dataMap.get("file")))
.withPartSize(partSize);
It's possible you're skipping a lot of content in the InputStream, which means that the connection to S3 sits idle while waiting for the content to get skipped.
You could try skipping the content in the stream on your own before making the request or increasing the read timeout. If you have a stand-alone repro case we can investigate further.
Did you try the above suggestion?
Closing the issue due to inactivity. Feel free to reopen if you still have the issue.
I am sure with that i didn't skip content of InputStream. then i just use the api whthout it. and i met a new issue.i used the sdk to upload file was so slower than i uploaded file with s3cmd. the speed i used the s3cmd can reach 12m/s,but i used java-sdk just can reach 2m/s.
@dntbeSris It's possible s3cmd is doing some optimizations like parallel multipart uploads of your file to S3 which would increase the throughput. Can you share the full code snippet you used to upload your file?
I don't know how s3cmd woked!
At first.I accepted the http request from front-end with all parameters that s3 needed. Code like this:
private static boolean dispatch(final FileUploadParams params, final String uploadId) {
logger.info("upload piece order|number|" + params.getPartNumber() + "|upload|" + uploadId);
UploadPartRequest uploadRequest = new UploadPartRequest()
.withBucketName(awsConfig.getBucket())
.withKey(params.getFileName())
.withUploadId(uploadId)
.withPartNumber(params.getPartNumber())
.withInputStream(params.getFileBlob())
.withPartSize(params.getPartSize());
UploadPartResult uploadResult = null;
try {
uploadResult = newS3Client().uploadPart(uploadRequest);
} catch (Exception e) {
logger.error("uploadPiece exception:", e);
return false;
}
if (PART_MAP.get(uploadId) == null) {
List<PartETag> partETags = new ArrayList<>();
partETags.add(uploadResult.getPartETag());
PART_MAP.putIfAbsent(uploadId, partETags);
} else {
List<PartETag> partETags = PART_MAP.get(uploadId);
partETags.add(uploadResult.getPartETag());
}
return true;
}
the speed was so slow.
then i got an idea that the front-end thransported a larger part of file to server. The back-end server splite the larger part to some smaller part and parallel upload to s3 server. the speed more faster than before. Code like this:
public static boolean schedule(final String uploadId) {
final int[] successCount = {0};
List<CompletableFuture<Boolean>> futures = EVERY_COMPLETE_UPLOAD_MAP.get(uploadId).stream()
.map(params -> CompletableFuture.completedFuture(params)
.thenApplyAsync(s -> dispatch(s, uploadId), newS3FileExexcutorService()))
.collect(Collectors.toList());
CompletableFuture.allOf(futures.toArray(new CompletableFuture[futures.size()]))
.whenComplete((v, th) -> {
futures.forEach(ft -> {
try {
if (ft.get(30L, TimeUnit.SECONDS)) {
successCount[0]++;
}
} catch (Exception e) {
logger.error("uploadPiece exception:", e);
}
});
}).join();
return successCount[0] == EVERY_COMPLETE_UPLOAD_MAP.get(uploadId).size();
}
private static boolean dispatch(final FileUploadParams params, final String uploadId) {
logger.info("upload piece order|number|" + params.getPartNumber() + "|upload|" + uploadId);
UploadPartRequest uploadRequest = new UploadPartRequest()
.withBucketName(awsConfig.getBucket())
.withKey(params.getFileName())
.withUploadId(uploadId)
.withPartNumber(params.getPartNumber())
.withInputStream(params.getFileBlob())
.withPartSize(params.getPartSize());
UploadPartResult uploadResult = null;
try {
uploadResult = newS3Client().uploadPart(uploadRequest);
} catch (Exception e) {
logger.error("uploadPiece exception:", e);
return false;
}
if (PART_MAP.get(uploadId) == null) {
List<PartETag> partETags = new ArrayList<>();
partETags.add(uploadResult.getPartETag());
PART_MAP.putIfAbsent(uploadId, partETags);
} else {
List<PartETag> partETags = PART_MAP.get(uploadId);
partETags.add(uploadResult.getPartETag());
}
return true;
}
Now. one question confused me. I used the AmazonS3 as a singleton class. Is a safe-thread class? Should I instantiation a AmazonS3 for every thread?
Is a safe-thread class?
Yes, all AWS clients are threadsafe.