使用 Cassandra Go 用戶端驅動程式以程式設計方式存取 Amazon Keyspaces - Amazon Keyspaces (適用於 Apache Cassandra)

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

使用 Cassandra Go 用戶端驅動程式以程式設計方式存取 Amazon Keyspaces

本節說明如何使用 Go Cassandra 用戶端驅動程式連線至 Amazon Keyspaces。若要為使用者和應用程式提供 Amazon Keyspaces 資源的程式設計存取憑證,您可以執行下列其中一項操作:

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

  • 為了增強安全性,我們建議為所有 AWS 服務中使用的 IAM 主體建立 IAM 存取金鑰。Cassandra 用戶端驅動程式的 Amazon Keyspaces SigV4 身分驗證外掛程式可讓您使用 IAM 存取金鑰來驗證對 Amazon Keyspaces 的呼叫,而不是使用者名稱和密碼。如需詳細資訊,請參閱建立和設定 Amazon Keyspaces 的 AWS 登入資料

開始之前

您需要先完成下列任務,才能開始。

Amazon Keyspaces 需要使用 Transport Layer Security (TLS) 來協助保護用戶端的連線。若要使用 TLS 連線至 Amazon Keyspaces,您需要下載 Amazon 數位憑證,並將 Go 驅動程式設定為使用 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 Cassandra 的 Gocql 驅動程式和服務特定登入資料連線至 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. 依照 的步驟,確保 ServiceUserNameServicePassword 與您產生服務特定憑證時取得的使用者名稱和密碼相符建立服務特定的登入資料,以程式設計方式存取 Amazon Keyspaces

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

  6. 建置 程式。

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

    ./cqlapp

使用適用於 Apache Cassandra 的 Go 驅動程式和 SigV4 身分驗證外掛程式連線至 Amazon Keyspaces

下列程式碼範例示範如何使用開放原始碼 Go 驅動程式的 SigV4 身分驗證外掛程式來存取 Amazon Keyspaces (適用於 Apache Cassandra)。

如果您尚未這麼做,請依照 中的步驟建立 IAM 主體的登入資料建立和設定 Amazon Keyspaces 的 AWS 登入資料。如果應用程式在 Lambda 或 Amazon EC2 執行個體上執行,您的應用程式會自動使用執行個體的登入資料。若要在本機執行此教學課程,您可以將登入資料儲存為本機環境變數。

GitHub 儲存庫將 Go SigV4 身分驗證外掛程式新增至您的應用程式。外掛程式支援 Cassandra 開放原始碼 Go 驅動程式的 1.2.x 版,且取決於適用於 Go 的 AWS SDK。

$ 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 // the authenticator uses the default credential chain to find AWS credentials cluster.Authenticator = sigv4.NewAwsAuthenticator() 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. 若要在本機執行此範例,您需要將下列變數定義為環境變數:

    • AWS_ACCESS_KEY_ID

    • AWS_SECRET_ACCESS_KEY

    • AWS_DEFAULT_REGION

  3. 若要將存取金鑰存放在程式碼之外,請參閱 的最佳實務存放用於程式設計存取的存取金鑰

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