6

我有一个可以列出存储桶的连接,但在尝试添加对象时出现问题。

conn = S3Connection(awskey, awssecret)

key = Key(mybucket)

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

我得到错误:

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

错误在文件中:

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

5 回答 5

13

只需更换:

key = Key(mybucket)

和:

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

扩展sth的评论,你不能传递一个字符串,它需要是一个桶对象。

于 2012-04-23T21:17:56.190 回答
6

Key期望一个桶对象作为它的第一个参数(可能由创建conn.create_bucket())。

看起来mybucket不是存储桶,而是字符串,因此调用失败。

于 2010-08-03T00:46:28.577 回答
5

这是我将如何做到这一点:

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')

米奇

于 2012-04-24T12:49:49.593 回答
0
import boto3
s3 = boto3.resource('s3')
mybucket = s3.Bucket('mybucketName')

现在您将获得 s3 存储桶对象。你得到了字符串。

享受!

于 2017-05-30T04:35:33.667 回答
0
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()
于 2019-04-22T11:19:04.690 回答