Get URL (link) of public object S3 programmatically

I save one public object everywhere in AWS S3 using this java API on my server Now I need to return the public URL of the S3 object to my client

So far, I have not found any API call that can return the public URL (or link field) of an S3 object

Is there any way to get the url?

+14
java amazon-s3 amazon-web-services
source share
8 answers

s3Client.getUrl (AWS_BUCKET, s3RelativeFilePath) .toExternalForm ();

older versions had: s3Client.getResourceUrl (bucket, s3RelativeToBucketPath);

+14
source share

I am using aws-java-sdk-s3 1.11.58

the accepted answer does not work on this version.

below line works for this version

 s3Client.getUrl(AWS_BUCKET, s3RelativeFilePath).toExternalForm(); 
+2
source share
 "https://"+{bucket}.s3.amazonaws.com+"/"+key 

This URL may be different from the link in the object overview in S3. But it works the same way. Note. The link in the object overview usually looks like

  https://s3-ap-southeast-2.amazonaws.com/{bucket}/{key} 
+2
source share

For users who have personal segments and who require a URL.

In my case, I needed to get a S3 Object downloadable link for a specific time, since my bucket is private .

I use Spring Cloud AWS, which under the hood uses the AWS SDK for Java and provides an AmazonS3 interface for interacting with S3, use AmazonS3Client if you use the AWS SDK for JAVA instead of AmazonS3 . I just entered AmazonS3, and now I can get the URL of the object, wherever it is needed, but it can be downloaded within 5 minutes, otherwise it will expire.

  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; } } } 
+1
source share

Think about how to build the url using the bucket name and object key, instead of asking s3 each time to just get the url.

0
source share

For SDK-2 version, Kotlin:

 s3.utilities().getUrl { it.bucket("BUCKET").key("FILEPATH") }.toExternalForm() 
0
source share

The public URL is simply the name of the object (ex my/object.txt ) associated with the endpoint of the bucket ( mybucket.s3.amazonaws.com ), or if you configured it as a website, the endpoint of the site.

When you load an object into s3, you are actually the same calm api your end users interact with; you are a PUT object, they get it.

-3
source share

" https://s3.amazonaws.com/ " + bucketName + "/" + should indicate the URL of the object in s3

-3
source share

All Articles