0
conn = tinys3.Connection(S3_ACCESS_KEY,S3_SECRET_KEY)
f = open('sample.zip','rb')
conn.upload('sample.zip',f,bucketname)

I can upload the file to my bucket (test) via the code above, but I want to upload it directly to test/images/example. I am open to moving over to boto, but I can't seem to import boto.s3 in my environment.

I have looked through How to upload a file to directory in S3 bucket using boto but none of the tinys3 examples show this.

mootmoot
  • 10,339
  • 3
  • 38
  • 41
Testing
  • 1
  • 1
  • 1
  • First, start using boto3 , because AWS deprecate boto and not support any third party module like tinys3, you will not get any support if you face any bug. Second, take a look http://boto3.readthedocs.io/en/latest/guide/s3-example-creating-buckets.html – mootmoot Jan 19 '18 at 11:42

2 Answers2

3
import boto3

client = boto3.client('s3', region_name='ap-southeast-2')

client.upload_file('/tmp/foo.txt', 'my-bucket', 'test/images/example/foo.txt')
John Rotenstein
  • 165,783
  • 13
  • 223
  • 298
  • Which line hangs? Hanging is normally an indication that the network connection is not working. Try doing it on a different network (eg home vs work). – John Rotenstein Jan 12 '18 at 05:33
  • It's not a network issue because I'm able to upload the file to the bucket but not the directory. client.upload_file('/tmp/foo.txt', 'my-bucket', 'test/images/example/foo.txt') is the line that hangs. – Testing Jan 13 '18 at 18:04
  • It works fine for me on a personal laptop and an EC2 instance. Check that you have the latest boto3 installed (sudo pip install boto3 --upgrade) and confirm the file paths and the AWS region (in the 2nd line). – John Rotenstein Jan 14 '18 at 09:42
0

The following worked for me

from boto3.s3.transfer import S3Transfer
from boto3 import client


client_obj = client('s3',
                   aws_access_key_id='my_aws_access_key_id',
                aws_secret_access_key='my_aws_secret_access_key')
transfer = S3Transfer(client_obj)
transfer.upload_file(src_file,
                     'my_s3_bucket_name',
                     dst_file,
                     extra_args={'ContentType': "application/zip"})
Hafizur Rahman
  • 1,704
  • 16
  • 24