这是我在redis\u api文档之后编写的源代码我犯了什么错误https://googleapis.dev/python/redis/latest/gapic/v1/api.html
from google.oauth2.service_account import Credentials
from google.cloud import redis_v1
LOGGER = logging.getLogger(__name__)
class GcpMemorystore:
def __init__(self, credentials, project_id: str, zone: str):
self.credentials = credentials
self.project_id = project_id
self.zone = zone
self.redisClient = redis_v1.CloudRedisClient().from_service_account_json(credentials)
"""List all Redis instances"""
def list_all_instances(self, prefix=None):
"""
Delete all Objects from all buckets
followed by deletion of all subsequent buckets
:param prefix:
:return:
"""
#instances = self.redisClient.list_instances().client.from_service_account_json(self.credentials)
parent = self.redisClient.location_path( self.project_id, self.zone )
instances = self.redisClient.list_instances()
print(instances)
然而,每当我运行这段代码时,我总是遇到这个错误
Traceback (most recent call last):
File "main.py", line 17, in <module>
GcpMemorystore.list_all_instances(service_account_file)
File "/Users/shoaib_nasir/PycharmProjects/gcp-cost-saver/GcpResources/gcp_memorystore.py", line 25, in list_all_instances
parent = self.redisClient.location_path( '[eng-node]', '[us-central1-a]' )
AttributeError: 'str' object has no attribute 'redisClient'
(venv) CA-SHOAIBN-M:gcp-cost-saver shoaib_nasir$
2条答案
按热度按时间35g0bw711#
根据错误,您从类构造函数调用方法,将其转换为简单函数。这就是为什么
self
只是你传递给呼叫的字符串。要么将类示例作为第一个参数传递,要么直接从类示例调用方法。
quhf5bfb2#
事实上,我设法让它工作,并注意到一些问题。首先,我的凭据有问题,其次,我将“zone”传递给api调用,相反,我必须传递region,其中region=“us-central1”
调用class方法对我来说很好