python s3 using boto, says 'attribute error: 'str' object has no attribute 'connection'

Just replace:

key = Key(mybucket)

with:

mybucket = "foo"
bucketobj = conn.get_bucket(mybucket)
mykey = Key(bucketobj)

Expanding on sth's comment, you can't pass a string, it needs to be a bucket object.


Key expects a bucket object as its first parameter (possibly created by conn.create_bucket()).

It looks like mybucket isn't a bucket, but a string, so the call fails.


Here's how I would do this:

import boto
s3 = boto.connect_s3()
bucket = s3.get_bucket("mybucketname")
key = bucket.new_key("mynewkeyname")
key.set_contents_from_filename('path_to_local_file', policy='public-read')

Mitch