Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AWS S3 get object using URL

I have a collection of URLs that may or may not belong to a particular bucket. These are not public.

I'm using the nodejs aws-sdk to get them.

However, the getObject function needs params Bucket and Key separately, which are already in my URL.

Is there any way I can use the URL?

I tried extracting the key by splitting URL with / and getting bucket by splitting with .. But the problem is the bucket name can also have . and I'm not sure if key name can have / as well.

like image 487
Dushyant Bangal Avatar asked May 24 '17 13:05

Dushyant Bangal


People also ask

How do I find my AWS S3 object URL?

In order to get the URL of an S3 Object via the AWS Console:Navigate to the AWS S3 console and click on your bucket's name. Use the search input to find the object if necessary. Click on the checkbox next to the object's name. Click on the Copy URL button.

How do I get S3 objects?

You can download an object from an S3 bucket in any of the following ways: Select the object and choose Download or choose Download as from the Actions menu if you want to download the object to a specific folder. If you want to download a specific version of the object, select the Show versions button.

What is S3 object URL?

An S3 bucket can be accessed through its URL. The URL format of a bucket is either of two options: http://s3.amazonaws.com/[bucket_name]/ http://[bucket_name].s3.amazonaws.com/

Which of the URL formats can be used for accessing S3 objects?

Amazon S3 supports both virtual-hosted–style and path-style URLs to access a bucket. Because buckets can be accessed using path-style and virtual-hosted–style URLs, we recommend that you create buckets with DNS-compliant bucket names.


1 Answers

The amazon-s3-uri library can parse out the Amazon S3 URI:

const AmazonS3URI = require('amazon-s3-uri')

try {
  const uri = 'https://bucket.s3-aws-region.amazonaws.com/key'
  const { region, bucket, key } = AmazonS3URI(uri)
} catch((err) => {
  console.warn(`${uri} is not a valid S3 uri`) // should not happen because `uri` is valid in that example 
})
like image 70
John Rotenstein Avatar answered Oct 12 '22 04:10

John Rotenstein