Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Upload to Amazon S3 using tinys3

I'm using Python and tinys3 to write files to S3, but it's not working. Here's my code:

import tinys3
conn = tinys3.Connection('xxxxxxx','xxxxxxxx',tls=True)

f = open('testing_s3.txt','rb')
print conn.upload('testing_data/testing_s3.txt',f,'testing-bucket')
print conn.get('testing_data/testing_s3.txt','testing-bucket')

That gives the output:

<Response [301]>
<Response [301]>

When I try specifying the endpoint, I get:

requests.exceptions.HTTPError: 403 Client Error: Forbidden

Any idea what I'm doing wrong?

Edit: When I try using boto, it works, so the problem isn't in the access key or secret key.

like image 930
Nadine Avatar asked Sep 01 '14 09:09

Nadine


2 Answers

I finally figured this out. Here is the correct code:

import tinys3
conn = tinys3.Connection('xxxxxxx','xxxxxxxx',tls=True,endpoint='s3-us-west-1.amazonaws.com')

f = open('testing_s3.txt','rb')
print conn.upload('testing_data/testing_s3.txt',f,'testing-bucket')
print conn.get('testing_data/testing_s3.txt','testing-bucket')

You have to use the region endpoint, not s3.amazonaws.com. You can look up the region endpoint from here: http://docs.aws.amazon.com/general/latest/gr/rande.html. Look under the heading "Amazon Simple Storage Service (S3)."

I got the idea from this thread: https://github.com/smore-inc/tinys3/issues/5

like image 151
Nadine Avatar answered Sep 28 '22 04:09

Nadine


If using an IAM user it is necessary to allow the "s3:PutObjectAcl" action.

like image 38
nicodjimenez Avatar answered Sep 28 '22 03:09

nicodjimenez