Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to generate url from boto3 in amazon web services

I have a Bucket in s3 and I am trying to pull the url of the image that is in there.

I am using boto3 and boto3 doesn't seem to have an implemented generate url method.

They have a core method, that generates url like this,

import botocore.session  session = botocore.session.get_session() client = session.create_client('s3')  presigned_url = client.generate_presigned_url(     'get_object', Params={'Bucket': self.bucket_name, 'Key': self.key}) 

One thing I am forced to do is, I have to send the parameters along with each request using session object. And the above method does not allow me to set the session variables (ie .. aws credentials)

The closest I can get is this

session = Session(aws_access_key_id='342342342342', aws_secret_access_key='3434234322', region_name='us-east-1')     s3 = session.resource('s3')     object = s3.Object('my-dev-bucket', 'amazonKeyString')     print object.get()["Body"] 

This gets me amazon s3 object which is an object called

botocore.response.StreamingBody object at 0x7ffaff8cef50 

Can I get a url of the image this way?

like image 601
Prabhakar Shanmugam Avatar asked Nov 05 '15 15:11

Prabhakar Shanmugam


People also ask

How do I create a URL for Amazon S3?

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 a Presigned 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.

What is AWS Presigned URL?

A presigned URL is generated by an AWS user who has access to the object. The generated URL is then given to the unauthorized user. The presigned URL can be entered in a browser or used by a program or HTML webpage. The credentials used by the presigned URL are those of the AWS user who generated the URL.


1 Answers

Able to get results and did not face any issues in getting the signed URL. I used the default session since my aws creds were stored locally in "~/.aws/credentials" file and my default region is set as needed ~/.aws/config

import boto3 s3Client = boto3.client('s3') s3Client.generate_presigned_url('get_object', Params = {'Bucket': 'www.mybucket.com', 'Key': 'hello.txt'}, ExpiresIn = 100) 

If you need to pass params for Session, import boto3.session and create custom session

import boto3.session session = boto3.session.Session(region_name='eu-central-1') s3Client = session.client('s3') 
like image 79
omuthu Avatar answered Oct 12 '22 11:10

omuthu