本文介绍了如何使用python 3.8获取AWS S3对象的位置/URL?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用带有以下代码的AWS Lambda函数(Python3.8)将文件上传到AWS S3.
I am uploading a file to AWS S3 using AWS Lambda function (Python3.8) with the following code.
file_obj = open(filename, 'rb')
s3_upload = s3.put_object( Bucket="aaa", Key="aaa.png", Body=file_obj)
return {
'statusCode': 200,
'body': json.dumps("Executed Successfully")
}
我想在 return
中获取S3对象的位置/URL.在 Node.js
中,我们使用 .location
参数来获取对象的位置/URL.
I want to get the location/url of the S3 object in return
. In Node.js
we use the .location
parameter for getting the object location/url.
有什么想法如何使用python 3.8做到这一点?
Any idea how to do this using python 3.8?
推荐答案
S3对象的网址具有已知格式,并遵循虚拟托管样式访问:
The url of S3 objects has known format and follows Virtual hosted style access:
https://bucket-name.s3.Region.amazonaws.com/keyname
因此,您可以自己构建网址:
Thus, you can construct the url yourself:
bucket_name = 'aaa'
aws_region = boto3.session.Session().region_name
object_key = 'aaa.png'
s3_url = f"https://{bucket_name}.s3.{aws_region}.amazonaws.com/{object_key}"
return {
'statusCode': 200,
'body': json.dumps({'s3_url': s3_url})
}
这篇关于如何使用python 3.8获取AWS S3对象的位置/URL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!