

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

# 使用 Cassandra Node.js 用戶端驅動程式以程式設計方式存取 Amazon Keyspaces
<a name="using_nodejs_driver"></a>

 本節說明如何使用 Node.js 用戶端驅動程式連線至 Amazon Keyspaces。若要為使用者和應用程式提供以程式設計方式存取 Amazon Keyspaces 資源的登入資料，您可以執行下列其中一項操作：
+ 建立與特定 AWS Identity and Access Management (IAM) 使用者相關聯的服務特定登入資料。
+ 為了增強安全性，我們建議為所有 AWS 服務中使用的 IAM 使用者或角色建立 IAM 存取金鑰。Cassandra 用戶端驅動程式的 Amazon Keyspaces SigV4 身分驗證外掛程式可讓您使用 IAM 存取金鑰來驗證對 Amazon Keyspaces 的呼叫，而不是使用者名稱和密碼。如需詳細資訊，請參閱[建立和設定 Amazon Keyspaces 的 AWS 登入資料](access.credentials.md)。

**Topics**
+ [開始之前](#using_nodejs_driver.BeforeYouBegin)
+ [使用適用於 Apache Cassandra 的 Node.js DataStax 驅動程式和服務特定登入資料連線至 Amazon Keyspaces](#nodejs_ssc)
+ [使用適用於 Apache Cassandra 的 DataStax Node.js 驅動程式和 SigV4 身分驗證外掛程式連線至 Amazon Keyspaces](#nodejs_SigV4)

## 開始之前
<a name="using_nodejs_driver.BeforeYouBegin"></a>

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

Amazon Keyspaces 需要使用 Transport Layer Security (TLS) 來協助保護與用戶端的連線。若要使用 TLS 連線至 Amazon Keyspaces，您需要下載 Amazon 數位憑證，並將 Python 驅動程式設定為使用 TLS。

 下載下列數位憑證，並將檔案儲存在本機或主目錄中。

1. AmazonRootCA1

1. AmazonRootCA2

1. AmazonRootCA3

1. AmazonRootCA4

1. Starfield Class 2 根目錄 （選用 – 用於回溯相容性）

若要下載憑證，您可以使用下列命令。

```
curl -O https://www.amazontrust.com/repository/AmazonRootCA1.pem
curl -O https://www.amazontrust.com/repository/AmazonRootCA2.pem
curl -O https://www.amazontrust.com/repository/AmazonRootCA3.pem
curl -O https://www.amazontrust.com/repository/AmazonRootCA4.pem
curl -O https://certs.secureserver.net/repository/sf-class2-root.crt
```

**注意**  
Amazon Keyspaces 先前使用錨定至 Starfield 類別 2 CA 的 TLS 憑證。 AWS 正在將所有 遷移 AWS 區域 至根據 Amazon Trust Services (Amazon 根 CAs 發行的憑證。在此轉換期間，請將用戶端設定為信任 Amazon 根 CAs1–4 和 Starfield 根，以確保所有區域的相容性。

使用範例中的名稱 *keyspaces-bundle.pem*，將所有下載的憑證合併成單一`pem`檔案。您可以執行下列 命令，即可進行新增：請記下 檔案的路徑，您稍後會需要此項目。

```
cat AmazonRootCA1.pem \
 AmazonRootCA2.pem \
 AmazonRootCA3.pem \
 AmazonRootCA4.pem \
 sf-class2-root.crt \
 > keyspaces-bundle.pem
```

## 使用適用於 Apache Cassandra 的 Node.js DataStax 驅動程式和服務特定登入資料連線至 Amazon Keyspaces
<a name="nodejs_ssc"></a>

 將驅動程式設定為使用 TLS `keyspaces-bundle.pem`的合併憑證檔案，並使用服務特定的憑證進行驗證。例如：

```
const cassandra = require('cassandra-driver');
const fs = require('fs');
const auth = new cassandra.auth.PlainTextAuthProvider('ServiceUserName', 'ServicePassword');
const sslOptions1 = {
         ca: [
                    fs.readFileSync('path_to_file/keyspaces-bundle.pem', 'utf-8')],      
                    host: 'cassandra.us-west-2.amazonaws.com',
                    rejectUnauthorized: true
        };
const client = new cassandra.Client({
                   contactPoints: ['cassandra.us-west-2.amazonaws.com'],
                   localDataCenter: 'us-west-2',
                   authProvider: auth,
                   sslOptions: sslOptions1,
                   protocolOptions: { port: 9142 }
        });
const query = 'SELECT * FROM system_schema.keyspaces';
 
client.execute(query)
                    .then( result => console.log('Row from Keyspaces %s', result.rows[0]))
                    .catch( e=> console.log(`${e}`));
```

用量備註：

1. `"path_to_file/keyspaces-bundle.pem"` 以第一個步驟中儲存的合併憑證檔案路徑取代 。

1. 依照 的步驟，確保 *ServiceUserName* 和 *ServicePassword* 與您產生服務特定登入資料時取得的使用者名稱和密碼相符[建立服務特定的登入資料，以程式設計方式存取 Amazon Keyspaces](programmatic.credentials.ssc.md)。

1. 如需可用端點的清單，請參閱 [Amazon Keyspaces 的服務端點](programmatic.endpoints.md)。

## 使用適用於 Apache Cassandra 的 DataStax Node.js 驅動程式和 SigV4 身分驗證外掛程式連線至 Amazon Keyspaces
<a name="nodejs_SigV4"></a>

下一節說明如何針對 Apache Cassandra 的開放原始碼 DataStax Node.js 驅動程式使用 SigV4 身分驗證外掛程式來存取 Amazon Keyspaces （適用於 Apache Cassandra)。

如果您尚未這麼做，請依照 中的步驟為 IAM 使用者或角色建立登入資料[建立和設定 Amazon Keyspaces 的 AWS 登入資料](access.credentials.md)。

從 [GitHub 儲存庫](https://github.com/aws/aws-sigv4-auth-cassandra-nodejs-driver-plugin)將 Node.js SigV4 身分驗證外掛程式新增至您的應用程式。外掛程式支援適用於 Cassandra 的 DataStax Node.js 驅動程式 4.x 版，並取決於適用於 Node.js 的 AWS SDK。它使用 `AWSCredentialsProvider` 來取得登入資料。

```
$ npm install aws-sigv4-auth-cassandra-plugin --save
```

此程式碼範例示範如何將 的區域特定執行個體設定為`SigV4AuthProvider`身分驗證提供者。

```
const cassandra = require('cassandra-driver');
const fs = require('fs');
const sigV4 = require('aws-sigv4-auth-cassandra-plugin');

const auth = new sigV4.SigV4AuthProvider({
    region: 'us-west-2', 
    accessKeyId:'AKIAIOSFODNN7EXAMPLE',
    secretAccessKey: 'wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY'});

const sslOptions1 = {
  ca: [
      fs.readFileSync('path_to_file/keyspaces-bundle.pem', 'utf-8')],
  host: 'cassandra.us-west-2.amazonaws.com',
  rejectUnauthorized: true
};


const client = new cassandra.Client({
  contactPoints: ['cassandra.us-west-2.amazonaws.com'],
  localDataCenter: 'us-west-2',
  authProvider: auth,
  sslOptions: sslOptions1,
  protocolOptions: { port: 9142 }
});


const query = 'SELECT * FROM system_schema.keyspaces';

client.execute(query).then(
    result => console.log('Row from Keyspaces %s', result.rows[0]))
    .catch( e=> console.log(`${e}`));
```

用量備註：

1. `"path_to_file/keyspaces-bundle.pem"` 將 取代為第一個步驟中儲存的憑證路徑。

1. 確定 *accessKeyId* 和 *secretAccessKey* 與您使用 取得的存取金鑰和私密存取金鑰相符`AWSCredentialsProvider`。如需詳細資訊，請參閱 [Node.js 中適用於 JavaScript 的 SDK 中的在 Node.js 中設定登入](https://docs.aws.amazon.com/sdk-for-javascript/v2/developer-guide/setting-credentials-node.html)資料。 *AWS JavaScript * 

1. 若要將存取金鑰存放在程式碼之外，請參閱 的最佳實務[存放用於程式設計存取的存取金鑰](aws.credentials.manage.md)。

1. 如需可用端點的清單，請參閱 [Amazon Keyspaces 的服務端點](programmatic.endpoints.md)。