Reputation: 146
import boto
import boto.s3
import sys
from boto.s3.key import Key
AWS_ACCESS_KEY_ID = 'XXXXXXXXXXXXXXXXX'
AWS_SECRET_ACCESS_KEY = 'XXXXXXXXXXXXXXXXXXXXXXXXX'
bucket_name="s3 bucket_name"
conn = boto.connect_s3(AWS_ACCESS_KEY_ID,AWS_SECRET_ACCESS_KEY)
bucket=conn.get_bucket(bucket_name,validate=True,headers=None)s
testfile = "file_path"
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 = 'mytestfile.csv'
k.set_contents_from_filename(testfile,
cb=percent_cb, num_cb=10)
Upvotes: 1
Views: 4774
Reputation: 146
import boto
import sys
from boto.s3.key import Key
import boto.s3.connection
AWS_ACCESS_KEY_ID = '<access_key>'
AWS_SECRET_ACCESS_KEY = '<secert_key>'
Bucketname = 'bucket_name'
conn = boto.s3.connect_to_region('us-east-2',
aws_access_key_id=AWS_ACCESS_KEY_ID,
aws_secret_access_key=AWS_SECRET_ACCESS_KEY,
is_secure=True, # uncomment if you are not using ssl
calling_format = boto.s3.connection.OrdinaryCallingFormat(),
)
bucket = conn.get_bucket(Bucketname)
testfile = "filename"
print ('Uploading %s to Amazon S3 bucket %s' %
(testfile, Bucketname))
def percent_cb(complete, total):
sys.stdout.write('.')
sys.stdout.flush()
k = Key(bucket)
k.key = 'fileName
k.set_contents_from_filename(testfile,
cb=percent_cb, num_cb=10)
Upvotes: 2
Reputation: 269320
It worked for me without having to specify a region:
import boto3
client = boto3.client('s3')
client.upload_file('foo.txt', 'my-bucket', 'foo.txt')
However, you can also specify a region when connecting to S3:
import boto3
client = boto3.client('s3', region_name = 'ap-southeast-2')
client.upload_file('foo.txt', 'my-bucket', 'foo.txt')
By the way, you should never have a need to put credentials in a source (.py) file. Instead, store credentials in a configuration file. The SDK will automatically retrieve them. The easiest way to create the file is with the AWS Command-Line Interface (CLI) aws configure
command.
Then, you can just run code like mine above, without having to worry about passing credentials.
Upvotes: -1