[英]google cloud doesn't work with previous f-string python
I'm trying to connect to google cloud and i've set the python code as follows:我正在尝试连接到谷歌云,我已经将 python 代码设置如下:
import google.cloud import storage
bucket_id='my_bucketname'
//others variables
storage=f'gs://{bucket_id}/'
client = storage.Client()
But, I've had the error:但是,我遇到了错误:
client = storage.Client()
AttributeError: 'str' object has no attribute 'Client'
I've noticed when I comment the variables with f-string(or using .format() as well), it works.我注意到当我用 f-string(或也使用 .format())注释变量时,它起作用了。 These tests I did, there is no variable related with the method storage.Client() and even so occours the error.我做了这些测试,没有与方法 storage.Client() 相关的变量,即使如此也会出现错误。
I tested in 3.6 and 3.8 python versions with same errors.我在 3.6 和 3.8 python 版本中测试了相同的错误。
This line of code imports the Google Cloud SDK into an object named storage .这行代码将 Google Cloud SDK 导入到名为storage的对象中。
import google.cloud import storage
You then declare a string named storage with the contents of the bucket name.然后,您使用存储桶名称的内容声明一个名为storage的字符串。 This overwrites the previous line of code.这会覆盖前一行代码。
storage=f'gs://{bucket_id}/'
You are then trying to create a Cloud Storage Client object from a string object:然后,您尝试从字符串对象创建 Cloud Storage Client 对象:
client = storage.Client()
That results in the error message:这导致错误消息:
AttributeError: 'str' object has no attribute 'Client'
Solution: use different variable names:解决方案:使用不同的变量名:
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.