使用 Amazon S3 存储桶作为静态 Web 主机 - Amazon SDK for JavaScript
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅 中国的 Amazon Web Services 服务入门 (PDF)

我们已宣布即将终止对 Amazon SDK for JavaScript v2 的支持。建议您迁移到 Amazon SDK for JavaScript v3。有关日期、其他详细信息以及如何迁移的信息,请参阅链接的公告。

使用 Amazon S3 存储桶作为静态 Web 主机

JavaScript code example that applies to Node.js execution

此 Node.js 代码示例演示:

  • 如何设置 Amazon S3 桶作为静态 Web 主机。

情景

在此示例中,使用一系列 Node.js 模块将您的任意存储桶配置作为静态 Web 主机。这些 Node.js 模块使用 SDK for JavaScript,通过 Amazon S3 客户端类的以下方法来配置选定的 Amazon S3 桶:

有关使用 Amazon S3 桶作为静态 Web 主机的更多信息,请参阅《Amazon Simple Storage Service 用户指南》中的在 Amazon S3 上托管静态网站

先决条件任务

要设置和运行此示例,您必须先完成以下任务:

配置 SDK

通过创建全局配置对象然后为代码设置区域,来配置 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'});

检索当前存储桶网站配置

创建文件名为 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 上的此处找到。

设置存储桶的网站配置

创建文件名为 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 上的此处找到。

删除存储桶网站配置

创建文件名为 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 上的此处找到。