google 云不适用于以前的 f-string python

google cloud doesn't work with previous f-string python

我正在尝试连接到 google 云,我已将 python 代码设置如下:

import google.cloud import storage

bucket_id='my_bucketname'
//others variables
storage=f'gs://{bucket_id}/'

client = storage.Client()

但是,我遇到了错误:

client = storage.Client()
    AttributeError: 'str' object has no attribute 'Client'

我注意到当我用 f-string(或也使用 .format())注释变量时,它起作用了。 我做的这些测试,没有与方法 storage.Client() 相关的变量,即使这样也会出错。

我在 3.6 和 3.8 python 版本中测试了同样的错误。

这行代码将 Google Cloud SDK 导入到名为 storage.

的对象中
import google.cloud import storage

然后声明一个名为 storage 的字符串,其中包含存储桶名称的内容。这会覆盖上一行代码。

storage=f'gs://{bucket_id}/'

然后您尝试从字符串对象创建云存储客户端对象:

client = storage.Client()

导致错误消息:

AttributeError: 'str' object has no attribute 'Client'

解决方法:使用不同的变量名:

import google.cloud import storage

bucket_name='my_bucketname'
//others variables
bucket_uri=f'gs://{bucket_id}/'

client = storage.Client()