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

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

使用 Cassandra Go 客戶端驅動程序以編程方式訪問 Amazon Keyspaces

本節介紹如何使用 Go 客戶端驅動程序連接到 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

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

  1. 為您的應用程式建立目錄。

    mkdir ./gocqlexample
  2. 導覽至新目錄。

    cd gocqlexample
  3. 為您的應用程式建立檔案。

    touch cqlapp.go
  4. 下載 Go 驅動程式。

    go get github.com/gocql/gocql
  5. 將下列範例程式碼新增至 cqlapp.go 檔案。

    package main import ( "fmt" "github.com/gocql/gocql" "log" ) func main() { // add the Amazon Keyspaces service endpoint cluster := gocql.NewCluster("cassandra.us-east-2.amazonaws.com") cluster.Port=9142 // add your service specific credentials cluster.Authenticator = gocql.PasswordAuthenticator{ Username: "ServiceUserName", Password: "ServicePassword"} // provide the path to the sf-class2-root.crt cluster.SslOpts = &gocql.SslOptions{ CaPath: "path_to_file/sf-class2-root.crt", EnableHostVerification: false, } // Override default Consistency to LocalQuorum cluster.Consistency = gocql.LocalQuorum cluster.DisableInitialHostLookup = false session, err := cluster.CreateSession() if err != nil { fmt.Println("err>", err) } defer session.Close() // run a sample query from the system keyspace var text string iter := session.Query("SELECT keyspace_name FROM system_schema.tables;").Iter() for iter.Scan(&text) { fmt.Println("keyspace_name:", text) } if err := iter.Close(); err != nil { log.Fatal(err) } session.Close() }

    使用注意事項:

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

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

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

  6. 建置程式。

    go build cqlapp.go
  7. 執行程式。

    ./cqlapp

使用 Go 驅動程序的 Apache 卡桑德拉和 SIGv4 身份驗證插件 Connect 到 Amazon Keyspaces

下面的代碼示例演示了如何使用 Sigv4 身份驗證插件的開源 Go 驅動程序訪問 Amazon Keyspaces(適用於 Apache 卡桑德拉)。

如果您尚未這麼做,請依照中的步驟為您的 IAM 使用者或角色建立登入資料如何創建和配置 Amazon Keyspaces 的 AWS 憑據

GitHub存儲庫將 Go Sigv4 身份驗證插件添加到您的應用程序。該插件支持開源 Go 驅動程序卡桑德拉的 1.2.x 版本,並取決 SDK for Go。 AWS

$ go mod init $ go get github.com/aws/aws-sigv4-auth-cassandra-gocql-driver-plugin

在此程式碼範例中,Amazon Keyspaces 端點由Cluster類別表示。它會使AwsAuthenticator用叢集的驗證器屬性來取得認證。

package main import ( "fmt" "github.com/aws/aws-sigv4-auth-cassandra-gocql-driver-plugin/sigv4" "github.com/gocql/gocql" "log" ) func main() { // configuring the cluster options cluster := gocql.NewCluster("cassandra.us-west-2.amazonaws.com") cluster.Port=9142 var auth sigv4.AwsAuthenticator = sigv4.NewAwsAuthenticator() auth.Region = "us-west-2" auth.AccessKeyId = "AKIAIOSFODNN7EXAMPLE" auth.SecretAccessKey = "wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY" cluster.Authenticator = auth cluster.SslOpts = &gocql.SslOptions{ CaPath: "path_to_file/sf-class2-root.crt", EnableHostVerification: false, } cluster.Consistency = gocql.LocalQuorum cluster.DisableInitialHostLookup = false session, err := cluster.CreateSession() if err != nil { fmt.Println("err>", err) return } defer session.Close() // doing the query var text string iter := session.Query("SELECT keyspace_name FROM system_schema.tables;").Iter() for iter.Scan(&text) { fmt.Println("keyspace_name:", text) } if err := iter.Close(); err != nil { log.Fatal(err) } }

使用注意事項:

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

  2. 確保 AccessKeyID 和密SecretAccess鑰與您使用獲得的訪問密鑰和秘密訪問密鑰匹配AwsAuthenticator如需詳細資訊,請參閱在 AWS SDK for Go. AWS

  3. 若要將存取金鑰儲存在程式碼之外,請參閱的最佳做法如何管理 IAM 使用者的存取金鑰

  4. 如需可用端點的清單,請參閱亞馬遜 Keyspaces 的服務端點