如何在亚马逊网络服务中从boto3生成网址

时间:2022-09-05 23:10:00

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似乎没有实现生成url方法。

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)

我不得不做的一件事是,我必须使用会话对象发送参数以及每个请求。并且上面的方法不允许我设置会话变量(即.. 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

这让我获得了亚马逊s3对象,这是一个被称为的对象

botocore.response.StreamingBody object at 0x7ffaff8cef50

Can I get a url of the image this way.

我可以通过这种方式获得图像的网址吗?

Kindly help Prabhakar S

请帮助Prabhakar S.

1 个解决方案

#1


46  

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

如果您需要为会话传递参数,请导入boto3.session并创建自定义会话

import boto3.session
session = boto3.session.Session(region_name='eu-central-1')
s3Client = session.client('s3')

#1


46  

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

如果您需要为会话传递参数,请导入boto3.session并创建自定义会话

import boto3.session
session = boto3.session.Session(region_name='eu-central-1')
s3Client = session.client('s3')