gpt4 book ai didi

python - 非空 gcs 存储桶返回 "can only concatenate str (not "字节") 到 str"

转载 作者:行者123 更新时间:2023-12-01 07:18:50 25 4
gpt4 key购买 nike

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

    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的代码片段,我的代码失败了。在调试时,我得到的存储桶名称为“cats-1567682436”,这是一个字节值。我猜想当使用字符串“/”和字节桶创建路径时会失败。所以串联失败。

最佳答案

更改此行:

path = '/' + bucket

对此:

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

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

关于python - 非空 gcs 存储桶返回 "can only concatenate str (not "字节") 到 str",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57804298/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com