无法使用redis python API在我的gcp项目中列出Redis实例

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

这是我根据Redis_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$ 

python-3.x redis google-api-python-client google-cloud-python
1个回答
0
投票

根据错误,您正在从类构造函数调用该方法,将其转换为简单函数。这就是self只是您传递给呼叫的字符串的原因。

或者将类实例作为第一个参数传递,或者最好直接从类实例中调用该方法。

© www.soinside.com 2019 - 2024. All rights reserved.