3

I've got this URL that was generated using the generate_url(300, 'PUT', ...) method and I'm wanting to use the requests library to upload a file into it.

This is the code I've been using: requests.put(url, data=content, headers={'Content-Type': content_type}), I've also tried some variations on this but the error I get is always the same.

I get a 403 - SignatureDoesNotMatch error from S3 every time, what am I doing wrong?

Noah McIlraith
  • 14,386
  • 7
  • 26
  • 35
  • 1
    http://stackoverflow.com/questions/10044151/how-to-generate-a-temporary-url-to-upload-file-to-amazon-s3-with-boto-library – varela Jul 20 '12 at 14:09

2 Answers2

1

Using boto3, this is how to generate an upload url and to PUT some data in it:

session = boto3.Session(aws_access_key_id="XXX", aws_secret_access_key="XXX")
s3client = session.client('s3')
url = s3client.generate_presigned_url('put_object', Params={'Bucket': 'mybucket', 'Key': 'mykey'})

requests.put(url, data=content)
Régis B.
  • 9,423
  • 6
  • 51
  • 87
0

S3 requires authentication token if your bucket is not public write. Please check token.

I would suggest you to use boto directly.

bucket.new_key()
key.name = keyname
key.set_contents_from_filename(filename, {"Content-Type": content_type})
# optional if file public to read
bucket.set_acl('public-read', key.name)

Also please check did you add Content-Length header. It's required and take part in auth hash calculation.

varela
  • 1,250
  • 1
  • 10
  • 16