如何修复连接到 cloudsql 时的 KeyError?

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

我正在尝试使用

google.cloud.sql.connector
连接到 cloudsql 服务器,此代码取自文档:

def connect_with_connector(self) -> sqlalchemy.engine.base.Engine:
        """
        Initializes a connection pool for a Cloud SQL instance of Postgres.

        Uses the Cloud SQL Python Connector package.
        """
        # Note: Saving credentials in environment variables is convenient, but not
        # secure - consider a more secure solution such as
        # Cloud Secret Manager (https://cloud.google.com/secret-manager) to help
        # keep secrets safe.

        instance_connection_name = os.environ[self.keys["gProj"]]
          # e.g. 'project:region:instance'
        db_user = os.environ[self.keys["gUser"]]  # e.g. 'my-db-user'
        db_pass = os.environ[self.keys["gPass"]]  # e.g. 'my-db-password'
        db_name = os.environ[self.keys["gDB"]]  # e.g. 'my-database'

        ip_type = IPTypes.PRIVATE if os.environ.get(self.keys["gPrivIP"]) else IPTypes.PUBLIC

        # initialize Cloud SQL Python Connector object
        connector = Connector()

        def getconn() -> pg8000.dbapi.Connection:
            conn: pg8000.dbapi.Connection = connector.connect(
                instance_connection_name,
                "pg8000",
                user=db_user,
                password=db_pass,
                db=db_name,
                ip_type=ip_type,
            )
            return conn

        # The Cloud SQL Python Connector can be used with SQLAlchemy
        # using the 'creator' argument to 'create_engine'
        pool = sqlalchemy.create_engine(
            "postgresql+pg8000://",
            creator=getconn,
            # ...
        )
        return pool

我用

instance_connection_name
给它连接名称,但它随后给了我这个错误:

instance_connection_name = os.environ[self.keys["gProj"]]
  File "C:\Users\--\AppData\Local\Programs\Python\Python310\lib\os.py", line 679, in __getitem__
    raise KeyError(key) from None
KeyError: '--connection_name_here--'

我已经仔细检查过连接名称是否正确,并且我弄乱了 IP,有谁知道问题可能是什么?

python python-3.x sqlalchemy gcloud google-cloud-sql
1个回答
0
投票

我意识到这是

os.environ
的问题,我不知道为什么示例使用它,但我只是将参数直接传递给连接器,并且工作正常。

def connect_with_connector(self) -> sqlalchemy.engine.base.Engine:
        """
        Initializes a connection pool for a Cloud SQL instance of Postgres.

        Uses the Cloud SQL Python Connector package.
        """
        # Note: Saving credentials in environment variables is convenient, but not
        # secure - consider a more secure solution such as
        # Cloud Secret Manager (https://cloud.google.com/secret-manager) to help
        # keep secrets safe.

        ip_type = IPTypes.PRIVATE if os.environ.get(self.keys["gPrivIP"]) else IPTypes.PUBLIC

        # initialize Cloud SQL Python Connector object
        connector = Connector()

        def getconn() -> pg8000.dbapi.Connection:
            conn: pg8000.dbapi.Connection = connector.connect(
                self.keys["gProj"],
                "pg8000",
                user=self.keys["gUser"],
                password=self.keys["gPass"],
                db=self.keys["gDB"],
                ip_type=ip_type,
            )
            return conn

        # The Cloud SQL Python Connector can be used with SQLAlchemy
        # using the 'creator' argument to 'create_engine'
        pool = sqlalchemy.create_engine(
            "postgresql+pg8000://",
            creator=getconn,
            # ...
        )
        return pool
© www.soinside.com 2019 - 2024. All rights reserved.