Jimmy
Jimmy

Reputation: 12497

Two Amazon S3 Python Boto Upload Questions

I am working with this code currently:

#!/usr/bin/env python

import boto
import boto.s3
from boto.s3.key import Key

AWS_ACCESS_KEY_ID = ''
AWS_SECRET_ACCESS_KEY = ''

filename = 'test.zip'    
bucket_name = AWS_ACCESS_KEY_ID.lower() + '-mah-bucket'
conn = boto.connect_s3(AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY)

bucket = conn.create_bucket(bucket_name, location=boto.s3.connection.Location.DEFAULT)

k = Key(bucket)
k.key = 'my test file'
k.set_contents_from_filename(filename)

I have two questions. Firstly, I believe this code creates a bucket as well as doing an upload. Thing is I don't want a bucket created, since I already have one in place. In order to do this do I just change it to this:

k = Key(bucket_name)

and get rid of this:

bucket = conn.create_bucket(bucket_name, location=boto.s3.connection.Location.DEFAULT)

Upvotes: 4

Views: 962

Answers (1)

Hexar
Hexar

Reputation: 583

Actually, instead of conn.create_bucket(...), just do:

bucket = conn.get_bucket(bucket_name)
k = Key(bucket)

Also, I should probably point out that it seems silly to me to name your buckets based on the AWS_ACCESS_KEY_ID, as this is not really needed.

Upvotes: 4

Related Questions