

适用于 JavaScript 的 AWS SDK v2 已终止支持。建议您迁移到 [适用于 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 存储桶作为静态 Web 主机
<a name="s3-example-static-web-host"></a>

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

**此 Node.js 代码示例演示：**
+ 如何设置 Amazon S3 桶作为静态 Web 主机。

## 情景
<a name="s3-example-static-web-host-scenario"></a>

在此示例中，使用一系列 Node.js 模块将您的任意存储桶配置作为静态 Web 主机。这些 Node.js 模块使用 SDK for JavaScript，通过 Amazon S3 客户端类的以下方法来配置选定的 Amazon S3 桶：
+ [https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/S3.html#getBucketWebsite-property](https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/S3.html#getBucketWebsite-property)
+ [https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/S3.html#putBucketWebsite-property](https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/S3.html#putBucketWebsite-property)
+ [https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/S3.html#deleteBucketWebsite-property](https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/S3.html#deleteBucketWebsite-property)

有关使用 Amazon S3 桶作为静态 Web 主机的更多信息，请参阅《Amazon Simple Storage Service 用户指南》中的**[在 Amazon S3 上托管静态网站](https://docs.aws.amazon.com/AmazonS3/latest/userguide/WebsiteHosting.html)。

## 先决条件任务
<a name="s3-example-static-web-host-prerequisites"></a>

要设置和运行此示例，您必须先完成以下任务：
+ 安装 Node.js。有关安装 Node.js 的更多信息，请参阅 [Node.js 网站](https://nodejs.org)。
+ 使用用户凭证创建共享配置文件。有关提供共享凭证文件的更多信息，请参阅[从共享凭证文件加载 Node.js 中的凭证](loading-node-credentials-shared.md)。

## 配置 SDK
<a name="s3-example-static-web-host-configure-sdk"></a>

通过创建全局配置对象然后为代码设置区域，来配置 SDK for JavaScript。在此示例中，区域设置为 `us-west-2`。

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

## 检索当前存储桶网站配置
<a name="s3-example-static-web-host-get-website"></a>

创建文件名为 `s3_getbucketwebsite.js` 的 Node.js 模块。该模块将获取单个命令行参数，指定需要其网站配置的存储桶。按前面所示配置 SDK。

创建 `AWS.S3` 服务对象。创建一个函数，检索在存储桶列表中选择的存储桶的当前存储桶网站配置。在调用 `getBucketWebsite` 方法时，您需要传递的唯一参数是所选存储桶的名称。如果桶当前具有网站配置，该配置在由 Amazon S3 传递到回调函数的 `data` 参数中返回。

如果所选存储桶没有网站配置，该信息将在 `err` 参数中返回给回调函数。

```
// 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" });

var bucketParams = { Bucket: process.argv[2] };

// call S3 to retrieve the website configuration for selected bucket
s3.getBucketWebsite(bucketParams, function (err, data) {
  if (err) {
    console.log("Error", err);
  } else if (data) {
    console.log("Success", data);
  }
});
```

要运行示例，请在命令行中键入以下内容。

```
node s3_getbucketwebsite.js BUCKET_NAME
```

此示例代码可在 [GitHub 上的此处](https://github.com/awsdocs/aws-doc-sdk-examples/blob/master/javascript/example_code/s3/s3_getbucketwebsite.js)找到。

## 设置存储桶的网站配置
<a name="s3-example-static-web-host-set-website"></a>

创建文件名为 `s3_setbucketwebsite.js` 的 Node.js 模块。确保按前面所示配置开发工具包。创建 `AWS.S3` 服务对象。

创建应用存储桶网站配置的函数。该配置允许将所选存储桶用作静态 Web 主机。网站配置以 JSON 形式指定。首先，创建包含用于指定网站配置的所有值的 JSON 对象，但标识错误文档的 `Key` 值以及标识索引文档的 `Suffix` 值除外。

将文本输入元素的值插入到 JSON 对象中。准备 `putBucketWebsite` 方法的参数，包括存储桶的名称以及 JSON 网站配置。

```
// 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 JSON for putBucketWebsite parameters
var staticHostParams = {
  Bucket: "",
  WebsiteConfiguration: {
    ErrorDocument: {
      Key: "",
    },
    IndexDocument: {
      Suffix: "",
    },
  },
};

// Insert specified bucket name and index and error documents into params JSON
// from command line arguments
staticHostParams.Bucket = process.argv[2];
staticHostParams.WebsiteConfiguration.IndexDocument.Suffix = process.argv[3];
staticHostParams.WebsiteConfiguration.ErrorDocument.Key = process.argv[4];

// set the new website configuration on the selected bucket
s3.putBucketWebsite(staticHostParams, function (err, data) {
  if (err) {
    // display error message
    console.log("Error", err);
  } else {
    // update the displayed website configuration for the selected bucket
    console.log("Success", data);
  }
});
```

要运行示例，请在命令行中键入以下内容。

```
node s3_setbucketwebsite.js BUCKET_NAME INDEX_PAGE ERROR_PAGE
```

此示例代码可在 [GitHub 上的此处](https://github.com/awsdocs/aws-doc-sdk-examples/blob/master/javascript/example_code/s3/s3_setbucketwebsite.js)找到。

## 删除存储桶网站配置
<a name="s3-example-static-web-host-delete-website"></a>

创建文件名为 `s3_deletebucketwebsite.js` 的 Node.js 模块。确保按前面所示配置开发工具包。创建 `AWS.S3` 服务对象。

创建删除所选存储桶的网站配置的函数。在调用 `deleteBucketWebsite` 方法时，您需要传递的唯一参数是所选存储桶的名称。

```
// 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" });

var bucketParams = { Bucket: process.argv[2] };

// call S3 to delete website configuration for selected bucket
s3.deleteBucketWebsite(bucketParams, function (error, data) {
  if (error) {
    console.log("Error", err);
  } else if (data) {
    console.log("Success", data);
  }
});
```

要运行示例，请在命令行中键入以下内容。

```
node s3_deletebucketwebsite.js BUCKET_NAME
```

此示例代码可在 [GitHub 上的此处](https://github.com/awsdocs/aws-doc-sdk-examples/blob/master/javascript/example_code/s3/s3_deletebucketwebsite.js)找到。