26

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??

8 Answers 8

34

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);
3
  • 2
    It seems it should be AmazonS3.getUrl(bucket, path)
    – simon
    Commented Dec 23, 2017 at 16:26
  • You cannot use these URLs to use sometime later, since these URLs has a expiration time.
    – CLOUGH
    Commented Jun 23, 2020 at 9:59
  • How do you create the s3Client?, what is builder? what should contain s3RelativeFilePath?
    – jdurango
    Commented Feb 21, 2021 at 2:15
9

For SDK-2 version, Kotlin:

s3.utilities().getUrl { it.bucket("BUCKET").key("FILEPATH") }.toExternalForm()
8

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();
4
"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

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

For users, who have private buckets and requires the URL.

In my case, I had to get downloadable link of S3 Object for a specific time as my bucket is private.

I'm using Spring Cloud AWS, which under the hood uses AWS SDK For Java and which provides AmazonS3 interface for interacting with S3, use AmazonS3Client if you're using AWS SDK For JAVA instead of AmazonS3. I simply injected AmazonS3 and now I can get URL of an object where-ever required, but it can be downloaded within 5 mins else it will be expired.

 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;
        }
    }
}
0

I am using Kotlin sdk implementation("aws.sdk.kotlin:s3:1.0.58")

here, how I am generating url from aws s3 bucket.

fun generatePublicUrl(region: String, bucketName: String, objectKey: String): String {
    val endpoint = "https://$bucketName.s3.$region.amazonaws.com/$objectKey"
    return URL(endpoint).toString()
}
-1

Consider constructing the url using bucket name and object key instead of making a request to s3 everytime to just fetch the url.

1
-5

"https://s3.amazonaws.com/"+bucketName+"/"+key should give you the url to the object in s3

4

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.