繁体   English   中英

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

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

我正在尝试连接到谷歌云,我已经将 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}/'

然后,您尝试从字符串对象创建 Cloud Storage Client 对象:

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()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM