【问题标题】:Not null gcs bucket returned "can only concatenate str (not "bytes") to str"非空 gcs 存储桶返回“只能将 str(不是“字节”)连接到 str”
【发布时间】:2020-01-08 06:52:08
【问题描述】:

我对谷歌云存储非常陌生。我正在使用教程https://cloud.google.com/storage/docs/boto-plugin 使用boto 创建一个存储桶到谷歌云存储。请在下面找到代码:

    import boto
    import gcs_oauth2_boto_plugin
    import time

    GOOGLE_STORAGE = 'gs'
    LOCAL_FILE = 'file'

    CLIENT_ID = "hnsdndsjsksoasjmoadsj"
    CLIENT_SECRET = "jdijeroerierper-er0erjfdkdf"

    gcs_oauth2_boto_plugin.SetFallbackClientIdAndSecret(CLIENT_ID, CLIENT_SECRET)

    now = time.time()

    # Your project ID can be found at https://console.cloud.google.com/
    # If there is no domain for your project, then project_id = 'YOUR_PROJECT'
    project_id = 'my-project-4749485'


    now = time.time()
    CATS_BUCKET = 'cats-%d' % now
    DOGS_BUCKET = 'dogs-%d' % now

    for name in (CATS_BUCKET, DOGS_BUCKET):
      uri = boto.storage_uri(name, GOOGLE_STORAGE)
      try:
        header_values = {"x-goog-project-id": project_id}
        uri.create_bucket(headers=header_values)

        print('Successfully created bucket "%s"' % name)
      except boto.exception.StorageCreateError as e:
        print('Failed to create bucket:', e)

当我运行它时,我得到:

/Users/dilipyadav/githome/elrond/venv/bin/python /Users/dilipyadav/githome/elrond/elrond/deploy3.py -t test -v 0.0.1
Traceback (most recent call last):
  File "/Users/dilipyadav/githome/elrond/elrond/deploy3.py", line 28, in <module>
    uri.create_bucket(headers=header_values)
  File "/Users/dilipyadav/githome/elrond/venv/lib/python3.7/site-packages/boto/storage_uri.py", line 574, in create_bucket
    storage_class)
  File "/Users/dilipyadav/githome/elrond/venv/lib/python3.7/site-packages/boto/gs/connection.py", line 95, in create_bucket
    data=get_utf8_value(data))
  File "/Users/dilipyadav/githome/elrond/venv/lib/python3.7/site-packages/boto/s3/connection.py", line 659, in make_request
    auth_path = self.calling_format.build_auth_path(bucket, key)
  File "/Users/dilipyadav/githome/elrond/venv/lib/python3.7/site-packages/boto/s3/connection.py", line 94, in build_auth_path
    path = '/' + bucket
TypeError: can only concatenate str (not "bytes") to str

Process finished with exit code 1

我在这里发现了类似的问题 - Can't collectstatic to s3 via Heroku using boto - s3 bucket returns a NoneType,但没有帮助。

注意:CLIENT_ID、CLIENT_SECRET 和 project_id 被替换为随机字符。

编辑:

    def build_auth_path(self, bucket, key=''):
        key = boto.utils.get_utf8_value(key)
        path = ''
        if bucket != '':
            path = '/' + bucket
        return path + '/%s' % urllib.parse.quote(key)

上面是来自 connection.py 的代码 sn-p,我的代码失败了。在调试时,我得到存储桶名称为“cats-1567682436”,这是一个字节值。我猜当使用字符串'/'和字节桶创建路径时它会失败。所以连接失败。

【问题讨论】:

  • 是的,你有很好的假设,所以只需在 bucket 上执行 decode 方法,并使用所需的编码,例如 UTF-8
  • 我们无法对 connection.py 进行更改,因为它是一个 gs 库。
  • 我认为您可能不得不强制将存储桶的名称以 utf-8 编码,因为它们似乎是以字节类型创建的字符串。你可以尝试添加.decode("utf-8") ,所以这条线看起来像:uri = boto.storage_uri(name.decode("utf-8"), GOOGLE_STORAGE)

标签: python python-3.x google-cloud-storage boto3 boto


【解决方案1】:

我的方式:

  1. virtualenv --python=/usr/bin/python3 venv/
  2. source venv/bin/activate
  3. git clone git://github.com/boto/boto.git -b develop
  4. 在 boto/utils.py:871 将行修补到value = str(value.encode('utf-8'))
  5. 来自博托/python setup.py install
  6. git clone https://github.com/henrysher/duplicity
  7. cd duplicity && python setup.py install

【讨论】:

  • 正如目前所写,您的答案尚不清楚。请edit 添加其他详细信息,以帮助其他人了解这如何解决所提出的问题。你可以找到更多关于如何写好答案的信息in the help center
【解决方案2】:

boto/utils.py:871 修补线路到

value = str(value.encode('utf-8'))

这是get_utf8_value() 函数的一个错误,它将字符串转换为字节数组。图书馆很旧,不太可能解决这个问题。

【讨论】:

    【解决方案3】:

    改变这一行:

    path = '/' + bucket
    

    到这里:

    path = '/' + bucket.decode('utf-8')
    

    但是,我会更改调用build_auth_path() 的代码,并先将存储桶bytearray 转换为string,然后再调用build_auth_path()

    【讨论】:

      猜你喜欢
      • 2021-11-07
      • 1970-01-01
      • 2021-03-11
      • 2021-06-22
      • 1970-01-01
      • 1970-01-01
      • 2020-10-20
      • 2021-12-15
      相关资源
      最近更新 更多