How to get the URL of a file on AWS S3, return path without AWS Key

1.5k Views Asked by At

I need to use Lambda function ( Python), I need to get S3 path. It should look like this:

s3://some-path-to-the-location-of-generated-text/generated-text.txt

That URL client need to see in browser, without AWS Key, because presigned urls i don't need.

Thank you so much!

2

There are 2 best solutions below

0
Tejaskumar On BEST ANSWER

You need to use AWS S3 bucket policy which should allow read to all for given file.

Your bucket policy should look like as follows:

 {

  "Statement":[
    {
      "Sid":"PublicRead",
      "Effect":"Allow",
      "Principal": "*",
      "Action":["s3:GetObject"],
      "Resource":["arn:aws:s3:::<name of bucket>/<file that you want everyone can read>"]
    }
}

Example - https://docs.aws.amazon.com/AmazonS3/latest/dev/example-bucket-policies.html#example-bucket-policies-use-case-2

0
Marcin On

S3 service and website url/paths have well described fixed format:

Working with Amazon S3 Buckets

They depend on whether you want to use virtual style, path style, website endpoints. They also depend on region. These are the things to consider when designing your lambda function.

Thus, you can construct them programmatically in your function if you know bucket and object names. But unless the objects are public, no one will be able to download them without AWS credentials.