Python 如何在亚马逊网络服务中从 boto3 生成 url
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33549254/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to generate url from boto3 in amazon web services
提问by Prabhakar Shanmugam
I have a Bucket in s3 and I am trying to pull the url of the image that is in there.
我在 s3 中有一个 Bucket,我正在尝试提取其中的图像的 url。
I am using boto3 and boto3 doesn't seem to have an implemented generate url method.
我正在使用 boto3,而 boto3 似乎没有实现的 generate url 方法。
They have a core method, that generates url like this,
他们有一个核心方法,可以像这样生成 url,
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)
我被迫做的一件事是,我必须使用会话对象将参数与每个请求一起发送。并且上述方法不允许我设置会话变量(即 .. aws 凭据)
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
这让我得到了 amazon s3 对象,这是一个名为
botocore.response.StreamingBody object at 0x7ffaff8cef50
Can I get a url of the image this way?
我可以通过这种方式获取图像的 url 吗?
回答by omuthu
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
能够获得结果并且在获得签名 URL 时没有遇到任何问题。我使用了默认会话,因为我的 aws 凭据本地存储在“~/.aws/credentials”文件中,并且我的默认区域是根据需要设置的 ~/.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
如果您需要为 Session 传递参数,请导入 boto3.session 并创建自定义会话
import boto3.session
session = boto3.session.Session(region_name='eu-central-1')
s3Client = session.client('s3')