Nadine
Nadine

Reputation: 1638

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.

Upvotes: 6

Views: 7976

Answers (3)

Don't know why but this code never worked for me. I've switched to boto, and it just uploaded file from 1 time.

  AWS_ACCESS_KEY_ID = 'XXXXXXXXXXXXXXXXXXXXX'
  AWS_SECRET_ACCESS_KEY = 'XXXXXXXXXXXXXXXXXXXXX/XXXXXXXXXXXXXXXXXXXXXXXXXXX'

  bucket_name = 'my-bucket'
  conn = boto.connect_s3(AWS_ACCESS_KEY_ID,
          AWS_SECRET_ACCESS_KEY)

  bucket = conn.get_bucket('my-bucket')

  print 'Uploading %s to Amazon S3 bucket %s' % \
     (filename, bucket_name)

  k = Key(bucket)
  k.key = filename
  k.set_contents_from_filename(filename,
      cb=percent_cb, num_cb=10)

Upvotes: 1

nicodjimenez
nicodjimenez

Reputation: 1208

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

Upvotes: 3

Nadine
Nadine

Reputation: 1638

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

Upvotes: 14

Related Questions