Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get URL(link) of a public S3 object programmatically

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

Till now i have'nt found any API call that can return the public URL(or link field) of a S3 object

Is there any way to get the URL??

like image 653
Neel Choudhury Avatar asked Jan 31 '14 18:01

Neel Choudhury


People also ask

Is S3 URL public?

Amazon Web Services (AWS) S3 objects are private by default. Only the object owner has permission to access these objects. Optionally we can set bucket policy to whitelist some accounts or URLs to access the objects of our S3 bucket.

How do I get a Presigned URL?

To create a valid pre-signed URL for your object, you must provide your security credentials, specify a bucket name, an object key, specify the HTTP method (for instance the method is "GET" to download the object) and expiration date and time. Anyone who receives the pre-signed URL can then access the object.


3 Answers

For Java SDK 2

s3Client.utilities().getUrl(builder -> builder.bucket(AWS_BUCKET).key(s3RelativeFilePath)).toExternalForm(); 

Reference - https://sdk.amazonaws.com/java/api/latest/software/amazon/awssdk/services/s3/S3Utilities.html#getUrl-java.util.function.Consumer-

older versions had:

s3Client.getResourceUrl(bucket, s3RelativeToBucketPath); 
like image 182
Strinder Avatar answered Sep 16 '22 17:09

Strinder


For SDK-2 version, Kotlin:

s3.utilities().getUrl { it.bucket("BUCKET").key("FILEPATH") }.toExternalForm()
like image 33
Pavel Shorokhov Avatar answered Sep 16 '22 17:09

Pavel Shorokhov


i am using 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();
like image 21
Govind Singh Avatar answered Sep 19 '22 17:09

Govind Singh