Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the URL of a file on AWS S3 using aws-sdk?

I'm using an AWS Lambda function to create a file and save it to my bucket on S3, it is working fine. After executing the putObject method, I get a data object, but it only contains an Etag of the recently added object.

s3.putObject(params, function(err, data) {     // data only contains Etag }); 

I need to know the exact URL that I can use in a browser so the client can see the file. The folder has been already made public and I can see the file if I copy the Link from the S3 console.

I tried using getSignedUrl but the URL it returns is used for other purposes, I believe.

Thanks!

like image 648
Pablo Quemé Avatar asked Jun 06 '17 21:06

Pablo Quemé


People also ask

How do I get my S3 upload URL?

You can get the resource URL either by calling getResourceUrl or getUrl . AmazonS3Client s3Client = (AmazonS3Client)AmazonS3ClientBuilder. defaultClient(); s3Client. putObject(new PutObjectRequest("your-bucket", "some-path/some-key.

How do I access files on my S3 AWS?

In the Amazon S3 console, choose your S3 bucket, choose the file that you want to open or download, choose Actions, and then choose Open or Download. If you are downloading an object, specify where you want to save it. The procedure for saving the object depends on the browser and operating system that you are using.


Video Answer


2 Answers

The SDKs do not generally contain a convenience method to create a URL for publicly-readable objects. However, when you called PutObject, you provided the bucket and the object's key and that's all you need. You can simply combine those to make the URL of the object, for example:

  • https://bucket.s3.amazonaws.com/key

So, for example, if your bucket is pablo and the object key is dogs/toto.png, use:

  • https://pablo.s3.amazonaws.com/dogs/toto.png

Note that S3 keys do not begin with a / prefix. A key is of the form dogs/toto.png, and not /dogs/toto.png.

For region-specific buckets, see Working with Amazon S3 Buckets and AWS S3 URL Styles. Replace s3 with s3.<region>.amazonaws.com or s3-<region>.amazonaws.com in the above URLs, for example:

  • https://seamus.s3.eu-west-1.amazonaws.com/dogs/setter.png (with dot)
  • https://seamus.s3-eu-west-1.amazonaws.com/dogs/setter.png (with dash)

If you are using IPv6, then the general URL form will be:

  • https://BUCKET.s3.dualstack.REGION.amazonaws.com

For some buckets, you may use the older path-style URLs. Path-style URLs are deprecated and only work with buckets created on or before September 30, 2020. They are used like this:

  • https://s3.amazonaws.com/bucket/key
  • https://s3.amazonaws.com/pablo/dogs/toto.png
  • https://s3.eu-west-1.amazonaws.com/seamus/dogs/setter.png
  • https://s3.dualstack.REGION.amazonaws.com/BUCKET

Currently there are TLS and SSL certificate issues that may require some buckets with dots (.) in their name to be accessed via path-style URLs. AWS plans to address this. See the AWS announcement.

Note: General guidance on object keys where certain characters need special handling. For example space is encoded to + (plus sign) and plus sign is encoded to %2B. Also here.

like image 107
jarmod Avatar answered Sep 22 '22 08:09

jarmod


in case you got the s3bucket and filename objects and want to extract the url, here is an option:

function getUrlFromBucket(s3Bucket,fileName){     const {config :{params,region}} = s3Bucket;     const regionString = region.includes('us-east-1') ?'':('-' + region)     return `https://${params.Bucket}.s3${regionString}.amazonaws.com/${fileName}` }; 
like image 25
adir abargil Avatar answered Sep 18 '22 08:09

adir abargil