Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Amazon s3 boto library, how can I get the URL of a saved key?

I am saving a key to a bucket with:

    key = bucket.new_key(fileName)
    key.set_contents_from_string(base64.b64decode(data))
    key.set_metadata('Content-Type', 'image/jpeg')
    key.set_acl('public-read')

After the save is successful, how can I access the URL of the newly created file?

like image 401
S-K' Avatar asked Apr 22 '13 20:04

S-K'


People also ask

How do I find my Amazon S3 URL?

Get an S3 Object's URL #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 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 get a pre signed URL?

To generate a presigned URL using the AWS Management ConsoleIn the Buckets list, choose the name of the bucket that contains the object that you want a presigned URL for. In the Objects list, select the object that you want to create a presigned URL for. On the Actions menu, choose Share with a presigned URL.

How can I access S3 files in Python using URLs?

http://s3tools.org/s3cmd works pretty well and support the s3:// form of the URL structure you want. It does the business on Linux and Windows. If you need a native API to call from within a python program then http://code.google.com/p/boto/ is a better choice.


2 Answers

If the key is publicly readable (as shown above) you can use Key.generate_url:

url = key.generate_url(expires_in=0, query_auth=False)

If the key is private and you want to generate an expiring URL to share the content with someone who does not have direct access you could do:

url = key.generate_url(expires_in=300)

where expires is the number of seconds before the URL expires. These will produce HTTPS url's. If you prefer an HTTP url, use this:

url = key.generate_url(expires_in=0, query_auth=False, force_http=True)
like image 115
garnaat Avatar answered Oct 24 '22 04:10

garnaat


For Boto3, you need to do it the following way...

import boto3

s3 = boto3.client('s3')
url = '{}/{}/{}'.format(s3.meta.endpoint_url, bucket, key)
like image 31
treecoder Avatar answered Oct 24 '22 05:10

treecoder