首页 > 解决方案 > TypeError:使用 boto 客户端库插件创建存储桶时,无法将“字节”对象隐式转换为 str

问题描述

Boto 客户端库插件

此代码用于创建存储桶,但我不能

import boto  
import time

# URI scheme for Cloud Storage. 
GOOGLE_STORAGE = 'gs'
# URI scheme for accessing local files. 
LOCAL_FILE = 'file'

now = time.time()

CATS_BUCKET = 'cats-%d' % now 
DOGS_BUCKET = 'dogs-%d' % now

print(CATS_BUCKET) 
print(DOGS_BUCKET)

for name in (CATS_BUCKET, DOGS_BUCKET):
    # Instantiate a BucketStorageUri object.
    uri = boto.storage_uri(name, GOOGLE_STORAGE)
    # create bucket
    try:
        uri.create_bucket()
        print("Successfully created bucket {}".format(name))
    except boto.exception.StorageCreateError as e:
        print("Failed to create bucket: ", e)

我已经设置了 .boto 文件,当我运行它时,我收到了类似的错误

path = '/' + bucket

TypeError:无法将“字节”对象隐式转换为 str

标签: pythongoogle-cloud-platformcloudboto

解决方案


您可以在使用之前尝试解码 uri:

uri.decode('utf-8')

或者你可以从一开始就以 utf-8 的形式阅读它:

with open(file_path, encoding='utf-8') as file:
  uri = file.read()

老实说,我认为这个问题与编码有关。如果我的建议不起作用,请告诉我,我会进一步研究这个方向。

编辑:

您也可以尝试像这样获取“uri”对象:

 uri = boto.storage_uri("%s/%s" % (BUCKET_NAME, DOWNLOAD_PATH), 'gs')

推荐阅读