

 適用於 JavaScript 的 AWS SDK v2 已end-of-support。我們建議您遷移至 [適用於 JavaScript 的 AWS SDK v3](https://docs.aws.amazon.com//sdk-for-javascript/v3/developer-guide/)。如需如何遷移的其他詳細資訊和資訊，請參閱此[公告](https://aws.amazon.com/blogs//developer/announcing-end-of-support-for-aws-sdk-for-javascript-v2/)。

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

# 建立與使用 Amazon S3 儲存貯體
<a name="s3-example-creating-buckets"></a>

![\[JavaScript code example that applies to Node.js execution\]](http://docs.aws.amazon.com/zh_tw/sdk-for-javascript/v2/developer-guide/images/nodeicon.png)

**這個 Node.js 程式碼範例會說明：**
+ 如何取得和顯示您帳戶中的 Amazon S3 儲存貯體清單。
+ 如何建立 Amazon S3 儲存貯體。
+ 如何上傳物件至指定的儲存貯體。

## 使用案例
<a name="s3-example-creating-buckets-scenario"></a>

在此範例中，一系列 Node.js 模組用於取得現有 Amazon S3 儲存貯體的清單、建立儲存貯體，以及將檔案上傳至指定的儲存貯體。這些 Node.js 模組使用適用於 JavaScript 的 SDK，透過這些 Amazon S3 用戶端類別的方法，從 Amazon S3 儲存貯體取得資訊並上傳檔案至 Amazon S3 儲存貯體：
+ [https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/S3.html#listBuckets-property](https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/S3.html#listBuckets-property)
+ [https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/S3.html#createBucket-property](https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/S3.html#createBucket-property)
+ [https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/S3.html#listObjects-property](https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/S3.html#listObjects-property)
+ [https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/S3.html#upload-property](https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/S3.html#upload-property)
+ [https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/S3.html#deleteBucket-property](https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/S3.html#deleteBucket-property)

## 先決條件任務
<a name="s3-example-creating-buckets-prerequisites"></a>

若要設定和執行此範例，您必須先完成這些任務：
+ 安裝 Node.js。如需安裝 Node.js 的詳細資訊，請參閱 [Node.js 網站](https://nodejs.org)。
+ 透過使用者登入資料建立共用組態檔。如需提供共用登入資料檔案的詳細資訊，請參閱 [從共用登入資料檔案中在 Node.js 中載入登入資料](loading-node-credentials-shared.md)。

## 設定軟體開發套件
<a name="s3-example-creating-buckets-configure-sdk"></a>

建立全域組態物件，然後為您的程式碼設定 區域，以設定適用於 JavaScript 的 SDK。在此範例中，區域會設為 `us-west-2`。

```
// Load the SDK for JavaScript
var AWS = require('aws-sdk');
// Set the Region 
AWS.config.update({region: 'us-west-2'});
```

## 顯示 Amazon S3 儲存貯體的清單
<a name="s3-example-creating-buckets-list-buckets"></a>

以檔名 `s3_listbuckets.js` 建立一個 Node.js 模組。請務必依前述的內容來設定軟體開發套件。若要存取 Amazon Simple Storage Service，請建立 `AWS.S3`服務物件。呼叫 Amazon S3 服務物件的 `listBuckets`方法，以擷取儲存貯體的清單。回呼函數的 `data` 參數具有一個包含映射陣列的 `Buckets` 屬性以代表儲存貯體。透過將其記錄至主控台以顯示儲存貯體清單。

```
// Load the AWS SDK for Node.js
var AWS = require("aws-sdk");
// Set the region
AWS.config.update({ region: "REGION" });

// Create S3 service object
s3 = new AWS.S3({ apiVersion: "2006-03-01" });

// Call S3 to list the buckets
s3.listBuckets(function (err, data) {
  if (err) {
    console.log("Error", err);
  } else {
    console.log("Success", data.Buckets);
  }
});
```

若要執行範例，請在命令列中輸入以下內容。

```
node s3_listbuckets.js
```

您可以在 [GitHub 上](https://github.com/awsdocs/aws-doc-sdk-examples/blob/master/javascript/example_code/s3/s3_listbuckets.js)找到這個範本程式碼。

## 建立 Amazon S3 儲存貯體
<a name="s3-example-creating-buckets-new-bucket"></a>

以檔名 `s3_createbucket.js` 建立一個 Node.js 模組。請務必依前述的內容來設定軟體開發套件。建立一個 `AWS.S3` 服務物件。該模組將採用單一命令行引數以指定新儲存貯體的名稱。

新增變數以保留用來呼叫 Amazon S3 服務物件`createBucket`方法的參數，包括新建立儲存貯體的名稱。回呼函數會在 Amazon S3 成功建立儲存貯體後，將新儲存貯體的位置記錄至主控台。

```
// Load the AWS SDK for Node.js
var AWS = require("aws-sdk");
// Set the region
AWS.config.update({ region: "REGION" });

// Create S3 service object
s3 = new AWS.S3({ apiVersion: "2006-03-01" });

// Create the parameters for calling createBucket
var bucketParams = {
  Bucket: process.argv[2],
};

// call S3 to create the bucket
s3.createBucket(bucketParams, function (err, data) {
  if (err) {
    console.log("Error", err);
  } else {
    console.log("Success", data.Location);
  }
});
```

若要執行範例，請在命令列中輸入以下內容。

```
node s3_createbucket.js BUCKET_NAME
```

您可以在 [GitHub 上](https://github.com/awsdocs/aws-doc-sdk-examples/blob/master/javascript/example_code/s3/s3_createbucket.js)找到這個範本程式碼。

## 將檔案上傳至 Amazon S3 儲存貯體
<a name="s3-example-creating-buckets-upload-file"></a>

以檔名 `s3_upload.js` 建立一個 Node.js 模組。請務必依前述的內容來設定軟體開發套件。建立一個 `AWS.S3` 服務物件。該模組將採用兩條命令行引數，第一個用來指定目的地儲存貯體，第二個指定要上傳的檔案。

使用呼叫 Amazon S3 服務物件`upload`方法所需的參數來建立變數。在 `Bucket` 參數中提供目標儲存貯體的名稱。`Key` 參數設為所選檔案的名稱，您可使用 Node.js `path` 模組來取得該名稱。`Body` 參數設為該檔案的內容，您可使用 Node.js `fs` 模組的 `createReadStream` 來取得該內容。

```
// Load the AWS SDK for Node.js
var AWS = require("aws-sdk");
// Set the region
AWS.config.update({ region: "REGION" });

// Create S3 service object
var s3 = new AWS.S3({ apiVersion: "2006-03-01" });

// call S3 to retrieve upload file to specified bucket
var uploadParams = { Bucket: process.argv[2], Key: "", Body: "" };
var file = process.argv[3];

// Configure the file stream and obtain the upload parameters
var fs = require("fs");
var fileStream = fs.createReadStream(file);
fileStream.on("error", function (err) {
  console.log("File Error", err);
});
uploadParams.Body = fileStream;
var path = require("path");
uploadParams.Key = path.basename(file);

// call S3 to retrieve upload file to specified bucket
s3.upload(uploadParams, function (err, data) {
  if (err) {
    console.log("Error", err);
  }
  if (data) {
    console.log("Upload Success", data.Location);
  }
});
```

若要執行範例，請在命令列中輸入以下內容。

```
node s3_upload.js BUCKET_NAME FILE_NAME
```

您可以在 [GitHub 上](https://github.com/awsdocs/aws-doc-sdk-examples/blob/master/javascript/example_code/s3/s3_upload.js)找到這個範本程式碼。

## 列出 Amazon S3 儲存貯體中的物件
<a name="s3-example-listing-objects"></a>

以檔名 `s3_listobjects.js` 建立一個 Node.js 模組。請務必依前述的內容來設定軟體開發套件。建立一個 `AWS.S3` 服務物件。

新增變數以保留用來呼叫 Amazon S3 服務物件`listObjects`方法的參數，包括要讀取的儲存貯體名稱。回呼函數會記錄物件 (檔案) 清單或故障訊息。

```
// Load the AWS SDK for Node.js
var AWS = require("aws-sdk");
// Set the region
AWS.config.update({ region: "REGION" });

// Create S3 service object
s3 = new AWS.S3({ apiVersion: "2006-03-01" });

// Create the parameters for calling listObjects
var bucketParams = {
  Bucket: "BUCKET_NAME",
};

// Call S3 to obtain a list of the objects in the bucket
s3.listObjects(bucketParams, function (err, data) {
  if (err) {
    console.log("Error", err);
  } else {
    console.log("Success", data);
  }
});
```

若要執行範例，請在命令列中輸入以下內容。

```
node s3_listobjects.js
```

您可以在 [GitHub 上](https://github.com/awsdocs/aws-doc-sdk-examples/blob/master/javascript/example_code/s3/s3_listobjects.js)找到這個範本程式碼。

## 刪除 Amazon S3 儲存貯體
<a name="s3-example-deleting-buckets"></a>

以檔名 `s3_deletebucket.js` 建立一個 Node.js 模組。請務必依前述的內容來設定軟體開發套件。建立一個 `AWS.S3` 服務物件。

新增變數以保留用來呼叫 Amazon S3 服務物件`createBucket`方法的參數，包括要刪除的儲存貯體名稱。儲存貯體必須為空後始可將其刪除。回呼函數會記錄成功或故障訊息。

```
// Load the AWS SDK for Node.js
var AWS = require("aws-sdk");
// Set the region
AWS.config.update({ region: "REGION" });

// Create S3 service object
s3 = new AWS.S3({ apiVersion: "2006-03-01" });

// Create params for S3.deleteBucket
var bucketParams = {
  Bucket: "BUCKET_NAME",
};

// Call S3 to delete the bucket
s3.deleteBucket(bucketParams, function (err, data) {
  if (err) {
    console.log("Error", err);
  } else {
    console.log("Success", data);
  }
});
```

若要執行範例，請在命令列中輸入以下內容。

```
node s3_deletebucket.js
```

您可以在 [GitHub 上](https://github.com/awsdocs/aws-doc-sdk-examples/blob/master/javascript/example_code/s3/s3_deletebucket.js)找到這個範本程式碼。