所以我试图在特定的s3存储桶中创建一个新的密钥作为字符串文件,但是我遇到了这个特殊的错误。我需要能够使用k.set_contents_from_string方法,因为稍后我打算从指定的URL直接将图像下载到s3存储桶(本地不这样做)。 P.S:连接到s3的凭据存储在.aws凭证文件中,以便正确进行身份验证。
代码:
import boto3
from boto.s3.key import Key
import urllib3
import boto
#s3 = boto3.resource('s3')
#mybucket = s3.Bucket("bucket_name")
#mybucket
c = boto.connect_s3()
b = c.get_bucket("bucket_name")
k = Key(c)
k.key = 'foobar'
k.set_contents_from_string('This is a test of S3')
错误:
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-29-3bd192af4319> in <module>()
1 k = Key(c)
2 k.key = 'foobar'
----> 3 k.set_contents_from_string('This is a test of S3')
/anaconda3/lib/python3.6/site-packages/boto/s3/key.py in set_contents_from_string(self, string_data, headers, replace, cb, num_cb, policy, md5, reduced_redundancy, encrypt_key)
1436 r = self.set_contents_from_file(fp, headers, replace, cb, num_cb,
1437 policy, md5, reduced_redundancy,
-> 1438 encrypt_key=encrypt_key)
1439 fp.close()
1440 return r
/anaconda3/lib/python3.6/site-packages/boto/s3/key.py in set_contents_from_file(self, fp, headers, replace, cb, num_cb, policy, md5, reduced_redundancy, query_args, encrypt_key, size, rewind)
1208 :return: The number of bytes written to the key.
1209 """
-> 1210 provider = self.bucket.connection.provider
1211 headers = headers or {}
1212 if policy:
AttributeError: 'HTTPSConnection' object has no attribute 'provider'
答案 0 :(得分:1)
您的第k = Key(c)
行应为k = Key(b)
,因为Key需要一个存储桶作为其构造函数参数