Home > Back-end >  How Do I get Object URL for Amazon S3 object
How Do I get Object URL for Amazon S3 object

Time:02-19

I uploaded a file to my Amazon S3 bucket and I would like to get a url using the aws-SDK not console for the object that I put in. The object has public read access I have tried using

const url = s3Client.getSignedUrl('getObject', {
        Bucket: srcBucket,
        Key: key,
    });

but that generates a signed url that expires. I can't seem to find any other method to fetch just the url. Any help is much appreciated.

CodePudding user response:

You can get the URL of an Amazon S3 object using the AWS SDK for Java V2. Here is the code:

// snippet-start:[s3.java2.getobjecturl.import]
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.s3.S3Client;
import software.amazon.awssdk.services.s3.model.GetUrlRequest;
import software.amazon.awssdk.services.s3.model.S3Exception;
import java.net.URL;
// snippet-end:[s3.java2.getobjecturl.import]

public class GetObjectUrl {


    public static void main(String[] args) {

        final String USAGE = "\n"  
                "Usage:\n"  
                "    <bucketName> <keyName> \n\n"  
                "Where:\n"  
                "    bucketName - the Amazon S3 bucket name.\n\n" 
                "    keyName - a key name that represents the object. \n\n";

        if (args.length != 2) {
            System.out.println(USAGE);
            System.exit(1);
        }

        String bucketName =  args[0];
        String keyName = args[1];
        Region region = Region.US_EAST_1 ;
        S3Client s3 = S3Client.builder()
                .region(region)
                .build();

        getURL(s3,bucketName,keyName);
        s3.close();
    }


    // snippet-start:[s3.java2.getobjecturl.main]
    public static void getURL(S3Client s3, String bucketName, String keyName ) {

        try {

            GetUrlRequest request = GetUrlRequest.builder()
                    .bucket(bucketName)
                    .key(keyName)
                    .build();

            URL url = s3.utilities().getUrl(request);
            System.out.println("The URL for  " keyName  " is " url.toString());

        } catch (S3Exception e) {
            System.err.println(e.awsErrorDetails().errorMessage());
            System.exit(1);
        }
    }
    // snippet-end:[s3.java2.getobjecturl.main]
}

CodePudding user response:

Just in case someone has a similar issue, like someone mentioned in the comment, as of the date this was written there isnt any API to get the s3 url without a signature except you construct it yourself. However, I found the following solution to be quick and easy to impliment.

const signedUrl = s3Client.getSignedUrl('getObject', {
    Bucket: srcBucket,
    Key: key,
});

signedUrl returns in this format:


https://{org}.s3.us-west-2.amazonaws.com/{PATH}/13592c51-d504-4899-960a-04efa0a7f6b7.mp3?AWSAccessKeyId={AWSKEY}&Expires={EPOC DATE}&Signature={RANDOMKEY-amz-security-}token=RANDOM&TOKEN}

To get an unsigned url (s3 Object url) you can split the string by the "?" query separator and return the first element which is identical to the Object url.

const url = signedUrl.split('?')[0];
// https://{org}.s3.us-west-2.amazonaws.com/{PATH}/13592c51-d504-4899-960a-04efa0a7f6b7.mp3

The only consideration is if AWS changes the way s3 object urls are displayed in the future.

  • Related