Java 以编程方式获取公共 S3 对象的 URL(链接)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21487066/
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
Get URL(link) of a public S3 object programmatically
提问by Neel Choudhury
I am storing one public object in AWS S3 bucket using given java API in my server Now i need to return back the public URL of the S3 object to my client
我在服务器中使用给定的 java API 在 AWS S3 存储桶中存储一个公共对象 现在我需要将 S3 对象的公共 URL 返回给我的客户端
Till now i have'nt found any API call that can return the public URL(or link field) of a S3 object
直到现在我还没有找到任何可以返回 S3 对象的公共 URL(或链接字段)的 API 调用
Is there any way to get the URL??
有没有办法获取网址??
采纳答案by Strinder
s3Client.getUrl(AWS_BUCKET, s3RelativeFilePath).toExternalForm();
older versions had:
旧版本有:
s3Client.getResourceUrl(bucket, s3RelativeToBucketPath);
回答by user1819998
"https://s3.amazonaws.com/"+bucketName+"/"+key should give you the url to the object in s3
" https://s3.amazonaws.com/"+bucketName+"/"+key 应该为您提供 s3 中对象的 URL
回答by Govind Singh
i am using aws-java-sdk-s3 1.11.58
我在用 aws-java-sdk-s3 1.11.58
accepted answer not working on this version.
接受的答案不适用于此版本。
below line works for this version
以下行适用于此版本
s3Client.getUrl(AWS_BUCKET, s3RelativeFilePath).toExternalForm();
回答by karthik
Consider constructing the url using bucket name and object key instead of making a request to s3 everytime to just fetch the url.
考虑使用存储桶名称和对象键构建 url,而不是每次都向 s3 发出请求以获取 url。
回答by AmyG
"https://"+{bucket}.s3.amazonaws.com+"/"+key
This URL may look different from the link in object overview in S3. But it works just as the same. Note: The link in object overview usually looks like
此 URL 可能与 S3 中对象概述中的链接不同。但它的工作原理是一样的。注意:对象概述中的链接通常看起来像
https://s3-ap-southeast-2.amazonaws.com/{bucket}/{key}
回答by Muhammad Waqas Dilawar
For users, who have private buckets and requires the URL.
对于拥有私有存储桶并需要 URL 的用户。
In my case, I had to get downloadable link of S3 Object for a specific time as my bucket is private.
就我而言,我必须在特定时间获取 S3 对象的可下载链接,因为我的存储桶是private。
I'm using Spring Cloud AWS, which under the hood uses AWS SDK For Java and which provides AmazonS3interface for interacting with S3, use AmazonS3Clientif you're using AWS SDK For JAVA instead of AmazonS3. I simply injected AmazonS3and now I can get URL of an object where-ever required, but it can be downloaded within 5 mins else it will be expired.
我正在使用 Spring Cloud AWS,它在底层使用 AWS SDK For Java 并提供AmazonS3接口用于与 S3 交互,如果您使用的是 AWS SDK For JAVA 而不是AmazonS3,请使用AmazonS3Client。我只是注入了AmazonS3,现在我可以在任何需要的地方获取对象的 URL,但它可以在 5 分钟内下载,否则它将过期。
public String getURL(String key) throws FileNotFoundException
{
try {
return amazonS3.generatePresignedUrl(BUCKET_NAME, key, new DateTime().plusMinutes(5).toDate()).toString();
}
catch (AmazonS3Exception exception){
if(exception.getStatusCode() == 404){
throw new FileNotFoundException(key);
}
else{
throw exception;
}
}
}
回答by Pavel Shorokhov
For SDK-2 version, Kotlin:
对于 SDK-2 版本,Kotlin:
s3.utilities().getUrl { it.bucket("BUCKET").key("FILEPATH") }.toExternalForm()