Python 3 Boto 3,AWS S3:获取对象URL [英] Python 3 Boto 3, AWS S3: Get object URL
本文介绍了Python 3 Boto 3,AWS S3:获取对象URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要在上传文件后直接检索公共对象URL,以便能够将其存储在数据库中. 这是我的上传代码:
I need to retrieve an public object URL directly after uploading a file, this to be able to store it in a database. This is my upload code:
s3 = boto3.resource('s3')
s3bucket.upload_file(filepath, objectname, ExtraArgs={'StorageClass': 'STANDARD_IA'})
我不是在寻找预先签名的URL,而只是在始终可以通过https公开访问的URL.
I am not looking for a presigned URL, just the URL that always will be publicly accessable over https.
任何帮助表示赞赏.
推荐答案
没有简单的方法,但是您可以从存储桶所在的区域(get_bucket_location
),存储桶名称和存储密钥构造URL:
There's no simple way but you can construct the URL from the region where the bucket is located (get_bucket_location
), the bucket name and the storage key:
bucket_name = "my-aws-bucket"
key = "upload-file"
s3 = boto3.resource('s3')
bucket = s3.Bucket(bucket_name)
bucket.upload_file("upload.txt", key)
location = boto3.client('s3').get_bucket_location(Bucket=bucket_name)['LocationConstraint']
url = "https://s3-%s.amazonaws.com/%s/%s" % (location, bucket_name, key)
这篇关于Python 3 Boto 3,AWS S3:获取对象URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文