'HTTPSConnection' 对象没有属性 'provider'

'HTTPSConnection' object has no attribute 'provider'

所以我试图在特定的 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'

你的行 k = Key(c) 应该读作 k = Key(b) 因为 Key 需要一个桶作为它的构造函数参数