我能够使用python使用这里的代码生成预先指定的AWS S3对象的url。当我使用生成的URL时,一些对象在浏览器中打开,一些对象被下载。我可以迫使URL下载对象吗?我读到过Content-Disposition头文件可以用来完成这个任务,但是我似乎找不到一个使用python的例子。
这可以是一个标准做法
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的回答不适合我。相反,这对我来说很有效:
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
我正在使用Python 3.10.4和AWS boto3 1.21.24.