java 从 S3 存储桶获取文件大小
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35867138/
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
Getting file size from S3 bucket
提问by Ashwani K
I am trying to get the file size (content-length) using Amazon S3 JAVA sdk.
我正在尝试使用 Amazon S3 JAVA sdk 获取文件大小(内容长度)。
public Long getObjectSize(AmazonS3Client amazonS3Client, String bucket, String key)
throws IOException {
Long size = null;
S3Object object = null;
try {
object = amazonS3Client.getObject(bucket, key);
size = object.getObjectMetadata().getContentLength();
} finally {
if (object != null) {
//object.close();
1. This results in 50 calls (connection pool size) post that I start getting connection pool errors.
2. If this line is uncommented it takes hell lot of time to make calls.
}
}
return size;
}
I followed thisand this. But not sure what I am doing wrong here. Any help on this?
回答by Mark B
I'm guessing what your actual question is asking, but I think you can reduce your code and eliminate the need to create an s3Object at all by doing something like:
我在猜测您的实际问题是什么,但我认为您可以通过执行以下操作来减少代码并完全消除创建 s3Object 的需要:
public Long getObjectSize(AmazonS3Client amazonS3Client, String bucket, String key)
throws IOException {
return amazonS3Client.getObjectMetadata(bucket, key).getContentLength();
}
That should remove the need to call object.close()
which you appear to be having issues with.
这应该消除了object.close()
您似乎遇到问题的呼叫需求。