Not null gcs bucket returned “can only concatenate str (not ”bytes“) to str”

让人想犯罪 __ 提交于 2020-01-24 20:09:20

问题


I am very new to google clould storage. I am using tutorial https://cloud.google.com/storage/docs/boto-plugin to create a bucket to google cloud storage using boto. Please find code below:

    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)

When I run it I get:

/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

I found similar issue here - Can't collectstatic to s3 via Heroku using boto - s3 bucket returns a NoneType, but it did not help.

Note: CLIENT_ID, CLIENT_SECRET and project_id are replaced with random characters.

Edit:

    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)

The above is a code snippet from connection.py where my code fails. On debugging I get bucket name as "cats-1567682436" which is a byte value. I guess it's failing when the path is created with string '/' and byte bucket. So concatenation fails.


回答1:


Change this line:

path = '/' + bucket

To this:

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

However, I would change the code that calls build_auth_path() and convert the bucket bytearray to a string first and then call build_auth_path().



来源:https://stackoverflow.com/questions/57804298/not-null-gcs-bucket-returned-can-only-concatenate-str-not-bytes-to-str

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!