Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
215 views
in Technique[技术] by (71.8m points)

python - 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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')

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...