使用 IAM 身分驗證搭配 Gremlin Python 連線至 Amazon Neptune 資料庫 - Amazon Neptune

本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。

使用 IAM 身分驗證搭配 Gremlin Python 連線至 Amazon Neptune 資料庫

概觀

本指南示範如何使用 Gremlin Python 驅動程式、Signature Version 4 身分驗證和 AWS SDK for Python (Boto3) 來使用啟用的 IAM 身分驗證連線至 Amazon Neptune 資料庫。

建立基本連線

使用下列程式碼範例做為如何使用 Gremlin Python 驅動程式與 IAM 身分驗證建立基本連線的指引。

from boto3 import Session from botocore.auth import SigV4Auth from botocore.awsrequest import AWSRequest from gremlin_python.process.anonymous_traversal import traversal from gremlin_python.driver.driver_remote_connection import DriverRemoteConnection def main(): endpoint = 'your.cluster.endpoint.neptune.amazonaws.com' conn_string = 'wss://' + endpoint + ':8182/gremlin' default_region = 'us-east-1' service = 'neptune-db' credentials = Session().get_credentials() if credentials is None: raise Exception("No AWS credentials found") creds = credentials.get_frozen_credentials() # region set inside config profile or via AWS_DEFAULT_REGION environment variable will be loaded region = Session().region_name if Session().region_name else default_region request = AWSRequest(method='GET', url=conn_string, data=None) SigV4Auth(creds, service, region).add_auth(request) rc = DriverRemoteConnection(conn_string, 'g', headers=request.headers.items()) g = traversal().with_remote(rc) # simple query to verify connection count = g.V().count().next() print('Vertex count: ' + str(count)) # cleanup rc.close() if __name__ == "__main__": main()