How to upload a file to directory in S3 bucket using boto How to upload a file to directory in S3 bucket using boto python python

How to upload a file to directory in S3 bucket using boto


Try this...

import botoimport boto.s3import sysfrom boto.s3.key import KeyAWS_ACCESS_KEY_ID = ''AWS_SECRET_ACCESS_KEY = ''bucket_name = AWS_ACCESS_KEY_ID.lower() + '-dump'conn = boto.connect_s3(AWS_ACCESS_KEY_ID,        AWS_SECRET_ACCESS_KEY)bucket = conn.create_bucket(bucket_name,    location=boto.s3.connection.Location.DEFAULT)testfile = "replace this with an actual filename"print 'Uploading %s to Amazon S3 bucket %s' % \   (testfile, bucket_name)def percent_cb(complete, total):    sys.stdout.write('.')    sys.stdout.flush()k = Key(bucket)k.key = 'my test file'k.set_contents_from_filename(testfile,    cb=percent_cb, num_cb=10)

[UPDATE]I am not a pythonist, so thanks for the heads up about the import statements.Also, I'd not recommend placing credentials inside your own source code. If you are running this inside AWS use IAM Credentials with Instance Profiles (http://docs.aws.amazon.com/IAM/latest/UserGuide/id_roles_use_switch-role-ec2_instance-profiles.html), and to keep the same behaviour in your Dev/Test environment, use something like Hologram from AdRoll (https://github.com/AdRoll/hologram)


import boto3s3 = boto3.resource('s3')BUCKET = "test"s3.Bucket(BUCKET).upload_file("your/local/file", "dump/file")


No need to make it that complicated:

s3_connection = boto.connect_s3()bucket = s3_connection.get_bucket('your bucket name')key = boto.s3.key.Key(bucket, 'some_file.zip')with open('some_file.zip') as f:    key.send_file(f)