Boto3: Using headers in presigned urls

Created on 4 Apr 2016  路  5Comments  路  Source: boto/boto3

Hi guys!

I'm changing from boto2 and I've stopped in generation of presigned urls. With boto2 we can give headers to generate presigned urls, but with boto3 I didn't found this option. I need to give a Content-Type header to upload a video with correct content type.

My actual code with boto2:

s3 = boto.connect_s3()
headers = [{'Content-Type': 'image/jpeg'}]
return s3.generate_url(7200, 'PUT', 'my-bucket', 'my-key', headers)

Am I missing something? There is a way to give headers to generate_presigned_url?

Thanks in advance!

closing-soon question s3

All 5 comments

So there is no parameter for custom headers in boto3's generate_presigned_url. The headers get added based on the parameters that you would normally pass to the client's method. So something like this:

import boto3

s3 = boto3.client('s3')

# Generate the URL to get 'key-name' from 'bucket-name'
url = s3.generate_presigned_url(
    ClientMethod='put_object',
    Params={
        'Bucket': 'bucket-name',
        'Key': 'key-name',
        'ContentType': 'image/jpeg'
    }
)
print(url)

Hopes this helps.

Closing due to inactivity. Please update if you have further information or questions.

Problem is not solved. "get_object" has not "ContentType" in params. But presigned url is needed for this headers.

@random1st I think when we make a GET request we don't need to use ContentType. I'm not sure but doesn't make sense to me to use it with a GET method.

I'm using generate_presigned_url with GET method in this way:

client = boto3.client('s3')

url = client.generate_presigned_url(
    'get_object', Params = {'Bucket': 'MYBUCKET', 'Key': 'MYKEY'},
    ExpiresIn=MY_LIFE_TIME_EXPIRATION_IN_SECONDS)

I'm using PUT method before because the client needs to inform me what kind of content he is sending to my bucket and I'm restricting the content type too.

I hope that this helps you!
[]'s

Full situation. I create request to service with Content-Type header. After I redirect request to AWS S3 service(302 Redirect). And S3 check request and raise "SignatureDoesNotMatch" exception. With url, generated with old boto, all work correct. I created new issue https://github.com/boto/boto3/issues/610

Was this page helpful?
0 / 5 - 0 ratings