Nadine Nadine - 6 months ago 398
Python Question

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.

Answer

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

Comments