[英]Generate AWS S3 presigned URL using python that forces objects to be downloaded
I am able to use python to generate presigned URLs to AWS S3 objects using the code here .我可以使用 python 使用此处的代码生成 AWS S3 对象的预签名 URL。 When I use the generated URL, some objects open in the browser and some are downloaded.
当我使用生成的 URL 时,一些对象在浏览器中打开,一些对象被下载。 Can I force the URL to download the object?
我可以强制 URL 下载 object 吗? I have read that the Content-Disposition header can be used to accomplish this, but I cant seem to find an example using python.
我已经读过 Content-Disposition header 可用于完成此操作,但我似乎无法找到使用 python 的示例。
This is can be a standard practice这可以是标准做法
def create_presigned_url(bucket_name, object_name, expiration=3600):
try:
response = s3_client.generate_presigned_url(
"get_object",
Params={"Bucket": bucket_name, "Key": object_name},
ExpiresIn=expiration,
)
except ClientError as e:
print(e)
return None
return response
Mobasshir's answer didn't do it for me.莫巴希尔的回答对我没有用。 Instead, this worked for me:
相反,这对我有用:
def create_presigned_url(bucket_name, object_name, expiration=10):
# Generate a presigned URL for the S3 object
s3_client = boto3.client('s3')
try:
response = s3_client.generate_presigned_url(
'get_object',
Params={
'Bucket': bucket_name,
'Key': object_name,
'ResponseContentType': 'binary/octet-stream' # this is the crucial line
},
ExpiresIn=expiration)
except ClientError as e:
logging.error(e)
return None
return response
I am using Python 3.10.4 with AWS boto3 1.21.24.我正在使用 Python 3.10.4 和 AWS boto3 1.21.24。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.