9

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.

Dushyant Bangal
  • 6,048
  • 8
  • 48
  • 80

3 Answers3

13

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 
})
John Rotenstein
  • 241,921
  • 22
  • 380
  • 470
1

To avoid installing a package.

const objectUrl = 'https://s3.us-east-2.amazonaws.com/my-s3-bucket/some-prefix/file.json'

const { host, pathname } = new URL(objectUrl);

const [, region] = /s3.(.*).amazon/.exec(host)
const [, bucket, key] = pathname.split('/')

Sebastian Scholl
  • 855
  • 7
  • 11
0

use this module parse-s3-url to set the parameter for the getObject.

 bucket.getObject( parseS3Url('https://s3.amazonaws.com/mybucket/mykey'), (err:any, data:any) =>{
    if (err) {
      // alert("Failed to retrieve an object: " + error);
    } else {
      console.log("Loaded " + data.ContentLength + " bytes");
      // do something with data.Body
    }
  });
hazan kazim
  • 938
  • 9
  • 11