python s3使用boto,说'属性错误:'str'对象没有属性'connection'

Bla*_*man 5 python amazon-s3 boto amazon-web-services

我有一个连接,因为我可以列出存储桶,但在尝试添加对象时遇到问题.

conn = S3Connection(awskey, awssecret)

key = Key(mybucket)

key.key = p.sku
key.set_contents_from_filename(fullpathtofile)
Run Code Online (Sandbox Code Playgroud)

我收到错误:

'attribute error: 'str' object has no attribute 'connection'
Run Code Online (Sandbox Code Playgroud)

错误在文件中:

/usr/local/lib/python2.6/dist-package/boto-2.obl-py2.6.egg/boto/s3/key.py' line # 539
Run Code Online (Sandbox Code Playgroud)

Bri*_*ian 13

只需更换:

key = Key(mybucket)
Run Code Online (Sandbox Code Playgroud)

有:

mybucket = "foo"
bucketobj = conn.get_bucket(mybucket)
mykey = Key(bucketobj)
Run Code Online (Sandbox Code Playgroud)

扩展sth的注释,你不能传递一个字符串,它需要是一个桶对象.


sth*_*sth 6

Key期望将bucket对象作为其第一个参数(可能由其创建conn.create_bucket()).

它看起来mybucket不是一个桶,而是一个字符串,因此调用失败.


gar*_*aat 6

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

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')
Run Code Online (Sandbox Code Playgroud)

米奇