TypeError:使用Boto客户端库插件创建存储桶时,无法将'bytes'对象隐式转换为str

问题描述 投票:0回答:1

Boto client library plugin

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

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:无法将'bytes'对象隐式转换为str

python google-cloud-platform cloud boto
1个回答
0
投票

您可以在使用它之前尝试对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')
© www.soinside.com 2019 - 2024. All rights reserved.