java获取s3对象url,Python 3 Boto 3,AWS S3:获取对象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'})

I am not looking for a presigned URL, just the URL that always will be publicly accessable over https.

Any help appreciated.

解决方案

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)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值