question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

S3 generate_presigned_url is not working with md5 hash

See original GitHub issue

The pre-signed url is not working with md5 hash. Normally when put

params = {'Bucket': bucket_name, 'Key': key}
url = s3_client.generate_presigned_url('put_object',
                                               Params=params,
                                               ExpiresIn=3600)

it works.

But when put any other put_object parameter like ACL md5 etc it throws

params = {'Bucket': bucket_name, 'Key': key, 
                  'ACL': 'public-read', 'ContentMD5': '1cx12....'}
url = s3_client.generate_presigned_url('put_object',
                                               Params=params,
                                               ExpiresIn=3600)
Error><Code>SignatureDoesNotMatch</Code><Message>
The request signature we calculated does not match the 
signature you provided. Check your key and signing method

If trying put header like Content-MD5 in put request it throws Headers not signed. My S3 client is like

s3 = boto3.client('s3',
                      region_name=app.config['AWS_REGION'],
                      aws_access_key_id=app.config['AWS_ACCESS_KEY_ID'],
                      config=Config(signature_version='s3v4'),
                      aws_secret_access_key=app.config['AWS_SECRET_ACCESS_KEY']
                      )

I am using boto3==1.4.1

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:8 (2 by maintainers)

github_iconTop GitHub Comments

15reactions
subhankarbcommented, Dec 30, 2016

@JordonPhillips thanks a lot, it is working. I am posting the working example for future reference.

import boto3
import requests

parts = s3_client.generate_presigned_post(Bucket=bucket_name,
                                              Key=key,
                                              Fields={
                                                'acl': 'public-read',
                                                'Content-MD5': str(md5),
                                                'Content-Type': 'binary/octet-stream'
                                                },
                                              Conditions=[
                                                  {"acl": "public-read"},
                                                  ["starts-with", "$Content-Type", ""],
                                                  ["starts-with", "$Content-MD5", ""]
                                              ]
                                          )

url = parts['url']
data = parts['fields']
files = {'file': open(local_path, 'rb')} # the key supposed to be file may be
response = requests.post(url, data=data, files=files)

Closing the issue

4reactions
duhaimecommented, Jan 22, 2022

Examples of both POST and PUT methods:

import boto3
import requests

access_key_id = 'AKIA....'
secret_access_key = 'LfNHsQ....'
bucket = 'images-dev'
filename = 'beautiful.png'

s3_client = boto3.client(
  's3',
  aws_access_key_id=access_key_id,
  aws_secret_access_key=secret_access_key
)

PUT:

response = s3_client.generate_presigned_url(
  ClientMethod = 'put_object',
  Params = {
    'Bucket': bucket,
    'Key': filename,
  }
)

# put
r = requests.put(response, data=open(filename, 'rb'))
print(r.status_code)

POST:

# sign url
response = s3_client.generate_presigned_post(
  Bucket = bucket,
  Key = filename,
)

# post
r = requests.post(response['url'], data=response['fields'], files={
  'file': open(filename, 'rb')
})
print(r.status_code)
Read more comments on GitHub >

github_iconTop Results From Across the Web

Java AWS s3: How to set and upload content with Md5 pre ...
I was able to resolve it by adding following piece of code: byte[] resultByte = DigestUtils.md5(/*byte array*/); String streamMD5 = new ...
Read more >
Creating Pre-Signed URLs for Amazon S3 Buckets
Create presigned URLS to Amazon S3 buckets using this AWS SDK for Go code example. ... and generates an MD5 checksum that is...
Read more >
aws/aws-sdk-go - Gitter
I'm getting the following error while creating policy by CreatePolicy method: Error MalformedPolicyDocument: Resource vendor must be fully qualified and cannot ...
Read more >
Securing your Amazon AWS S3 presigned URLs, tips and tricks
This is the result of my experience with S3 buckets, not an absolute truth. ... a presigned URL that checks for the file's...
Read more >
AmazonS3 (AWS Java SDK for Amazon S3 1.11.403 API)
Note: Do not directly implement this interface, new methods are added to ... Callers can use this method to control which AWS region...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found