使用卡桑德拉 Python 客戶端驅動程序以編程方式訪問 Amazon Keyspaces - Amazon Keyspaces (適用於 Apache Cassandra)

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

使用卡桑德拉 Python 客戶端驅動程序以編程方式訪問 Amazon Keyspaces

在本節中,我們將向您展示如何使用 Python 客戶端驅動程序連接到 Amazon Keyspaces。若要提供使用者和應用程式以程式設計方式存取 Amazon Keyspace 資源的登入資料,您可以執行下列其中一項作業:

  • 建立與特定 AWS Identity and Access Management (IAM) 使用者相關聯的服務特定登入資料。

  • 為了增強安全性,我們建議您為 IAM 使用者或跨所有 AWS 服務使用的角色建立 IAM 存取金鑰。適用於 Cassandra 客戶端驅動程序的 Amazon 密 Keyspaces Sigv4 身份驗證插件使您可以使用 IAM 訪問密鑰而不是用戶名和密碼對 Amazon 密鑰 Keyspaces 進行身份驗證呼叫。如需詳細資訊,請參閱 如何創建和配置 Amazon Keyspaces 的 AWS 憑據

開始之前

您必須先完成下列工作,才能開始。

Amazon Keyspaces 需要使用傳輸層安全性 (TLS) 來協助保護與用戶端的連線安全。要使用 TLS 連接到 Amazon Keyspaces,您需要下載 Amazon 數字證書並配置 Python 驅動程序以使用 TLS。

使用下列指令下載 Starfield 數位憑證,並儲存在sf-class2-root.crt本機或主目錄中。

curl https://certs.secureserver.net/repository/sf-class2-root.crt -O
注意

您也可以使用 Amazon 數位憑證連線到 Amazon Keyspaces,如果您的用戶端成功連線至 Amazon Keyspaces,則可以繼續這麼做。Starfield 憑證為使用舊版憑證授權單位的用戶端提供額外的向後相容性。

curl https://certs.secureserver.net/repository/sf-class2-root.crt -O

使用 Python 驅動程序的 Apache 卡桑德拉和特定於服務的憑據 Connect 到 Amazon Keyspaces

下列程式碼範例說明如何使用 Python 用戶端驅動程式和服務特定登入資料連線至 Amazon Keyspaces。

from cassandra.cluster import Cluster from ssl import SSLContext, PROTOCOL_TLSv1_2 , CERT_REQUIRED from cassandra.auth import PlainTextAuthProvider ssl_context = SSLContext(PROTOCOL_TLSv1_2 ) ssl_context.load_verify_locations('path_to_file/sf-class2-root.crt') ssl_context.verify_mode = CERT_REQUIRED auth_provider = PlainTextAuthProvider(username='ServiceUserName', password='ServicePassword') cluster = Cluster(['cassandra.us-east-2.amazonaws.com'], ssl_context=ssl_context, auth_provider=auth_provider, port=9142) session = cluster.connect() r = session.execute('select * from system_schema.keyspaces') print(r.current_rows)

使用注意事項:

  1. 取代"path_to_file/sf-class2-root.crt"為第一個步驟中儲存的憑證路徑。

  2. 請遵循下ServiceUser列步驟,確定名稱並ServicePassword符合您在產生服務特定認證時取得的使用者名稱和密碼。 產生服務特定認證

  3. 如需可用端點的清單,請參閱Amazon Keyspaces 的服務端點

Connect 到 Amazon Keyspaces 使用 DataStax Python 驅動程序阿帕奇卡桑德拉和 SIGv4 身份驗證插件

下面的部分演示了如何使用 Sigv4 身份驗證插件的開源 DataStax Python 驅動程序阿帕奇卡桑德拉訪問 Amazon Keyspaces(阿帕奇卡桑德拉)。

如果您尚未這麼做,請依照中的步驟開始為您的 IAM 角色建立登入資料如何創建和配置 Amazon Keyspaces 的 AWS 憑據。本教學課程使用需要 IAM 角色的臨時登入資料。如需暫時登入資料的詳細資訊,請參閱使用臨時登入資料,透過 IAM 角色和 Sigv4 外掛程式連線到 Amazon Keyspaces

然後,將 Python Sigv4 身份驗證插件從GitHub 存儲庫添加到您的環境中。

pip install cassandra-sigv4

下面的代碼示例演示了如何通過使用開源 DataStax Python 驅動程序卡桑德拉和 SIGv4 身份驗證插件連接到 Amazon Keyspaces。該插件取決 AWS SDK for Python (Boto3)。它用boto3.session來獲取臨時憑據。

from cassandra.cluster import Cluster from ssl import SSLContext, PROTOCOL_TLSv1_2 , CERT_REQUIRED from cassandra.auth import PlainTextAuthProvider import boto3 from cassandra_sigv4.auth import SigV4AuthProvider ssl_context = SSLContext(PROTOCOL_TLSv1_2) ssl_context.load_verify_locations('path_to_file/sf-class2-root.crt') ssl_context.verify_mode = CERT_REQUIRED # use this if you want to use Boto to set the session parameters. boto_session = boto3.Session(aws_access_key_id="AKIAIOSFODNN7EXAMPLE", aws_secret_access_key="wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY", aws_session_token="AQoDYXdzEJr...<remainder of token>", region_name="us-east-2") auth_provider = SigV4AuthProvider(boto_session) # Use this instead of the above line if you want to use the Default Credentials and not bother with a session. # auth_provider = SigV4AuthProvider() cluster = Cluster(['cassandra.us-east-2.amazonaws.com'], ssl_context=ssl_context, auth_provider=auth_provider, port=9142) session = cluster.connect() r = session.execute('select * from system_schema.keyspaces') print(r.current_rows)

使用注意事項:

  1. 取代"path_to_file/sf-class2-root.crt"為第一個步驟中儲存的憑證路徑。

  2. 請確定是否符合、aws_秘密碼和aws_session_ token 相符,而且您使用取得. Access Key Secret Access Key Session Token boto3.session 如需詳細資訊,請參閱中的認證AWS SDK for Python (Boto3)

  3. 如需可用端點的清單,請參閱Amazon Keyspaces 的服務端點