Blankman
Blankman

Reputation: 267320

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

I have a connection that works as I can list buckets, but having issues when trying to add a object.

conn = S3Connection(awskey, awssecret)

key = Key(mybucket)

key.key = p.sku
key.set_contents_from_filename(fullpathtofile)

I get the error:

'attribute error: 'str' object has no attribute 'connection'

the error is in the file:

/usr/local/lib/python2.6/dist-package/boto-2.obl-py2.6.egg/boto/s3/key.py' line # 539

Upvotes: 6

Views: 23109

Answers (5)

Jatinder Singh
Jatinder Singh

Reputation: 327

import boto3
s3 = boto3.resource('s3')
mybucket = s3.Bucket('mybucketName')

Now you will get the s3 bucket object. You were getting the string.

Enjoy!

Upvotes: 0

Aakash Handa
Aakash Handa

Reputation: 1307

import os
import boto.s3.connection

accessKeyId = 'YOUR_AWS_ACCESS_KEY_ID'
secretKey = 'YOUR_AWS_SECERT_KEY_ID'
host = 'HOST'

S3 = boto.connect_s3(
  aws_access_key_id = accessKeyId,
  aws_secret_access_key = secretKey,
  host = host,
  port = PORT,
  calling_format = boto.s3.connection.OrdinaryCallingFormat(),
)


def upload_objects():

    try:
        bucket_name = "bucket name" #s3 bucket name
        root_path = 'model/' # local folder for upload
        my_bucket = S3.get_bucket(bucket_name)
        for path, subdirs, files in os.walk(root_path):
            path = path.replace("\\","/")
            directory_name = path.replace(root_path,"")
            for file in files:
                if(file != ".DS_Store"):
                    full_key_name = os.path.join(path, file)
                    k = my_bucket.new_key(full_key_name)
                    k.set_contents_from_filename('/model/'+directory_name+'/'+file)

    except Exception as err:
        print(err)

upload_objects()

Upvotes: 0

garnaat
garnaat

Reputation: 45916

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

Upvotes: 5

Brian
Brian

Reputation: 869

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.

Upvotes: 13

sth
sth

Reputation: 229914

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.

Upvotes: 6

Related Questions