Search code examples
javascriptamazon-web-servicesamazon-s3urlaws-sdk-js

How Do I get Object URL for Amazon S3 object


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.


Solution

  • Just in case someone has a similar issue, like someone mentioned in the comment, as of the date this was written there isn't 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 implement.

    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.