Boto3: generate_presigned_url how to pass response_headers

Created on 9 Nov 2015  路  4Comments  路  Source: boto/boto3

Trying to migrate from boto2 and previously was using the boto.s3.generate_url method which took a parameter response_headers.

Am now attempting to use S3.Client.generate_presigned_url which no longer has this parameter.

How would I go about passing these in boto3?

question

Most helpful comment

Thanks for the response managed to get it working with your advice :)

For anyone else who comes across this in when doing a boto2 -> boto3 port you would translate as follows:

boto2

signed_url = connection.generate_url(
    expires_in = 60,
    method = "GET",
    bucket = "mybucket",
    key = s3_key,
    response_headers = {
        "response-content-disposition": "attachment; filename=report.pdf",
        "response-content-type": "application/pdf"
    }
)

boto3

signed_url = client.generate_presigned_url(
    ClientMethod = "get_object",
    ExpiresIn = 60,
    Params = {
        "Bucket": "mybucket",
        "Key": s3_key,
        "ResponseContentDisposition": "attachment; filename=report.pdf",
        "ResponseContentType" : "application/pdf"
    }
)

All 4 comments

The Params parameter in S3.Client.generate_presigned_url (as well as its cousin in botocore) are the droids you are looking for. :-)

Thanks for the response managed to get it working with your advice :)

For anyone else who comes across this in when doing a boto2 -> boto3 port you would translate as follows:

boto2

signed_url = connection.generate_url(
    expires_in = 60,
    method = "GET",
    bucket = "mybucket",
    key = s3_key,
    response_headers = {
        "response-content-disposition": "attachment; filename=report.pdf",
        "response-content-type": "application/pdf"
    }
)

boto3

signed_url = client.generate_presigned_url(
    ClientMethod = "get_object",
    ExpiresIn = 60,
    Params = {
        "Bucket": "mybucket",
        "Key": s3_key,
        "ResponseContentDisposition": "attachment; filename=report.pdf",
        "ResponseContentType" : "application/pdf"
    }
)

:+1: The force is strong with this one. :)

Hi, I'm using boto3 and trying to use params ResponseContentType, ResponseContentDisposition but it cause SignatureDoesNotMatch error. This error not appeared when I don't specify these params.

Was this page helpful?
0 / 5 - 0 ratings