使用 Amazon S3 存储桶策略 - 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 存储桶策略

JavaScript code example that applies to Node.js execution

此 Node.js 代码示例演示:

  • 如何检索 Amazon S3 桶的桶策略。

  • 如何添加或更新 Amazon S3 桶的桶策略。

  • 如何删除 Amazon S3 桶的桶策略。

情景

本示例使用一系列 Node.js 模块来检索、设置或删除 Amazon S3 桶上的桶策略。这些 Node.js 模块使用 SDK for JavaScript,通过 Amazon S3 客户端类的以下方法来配置选定的 Amazon S3 桶的策略:

有关 Amazon S3 桶的桶策略的更多信息,请参阅《Amazon Simple Storage Service 用户指南》中的使用桶策略和用户策略

先决条件任务

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

配置 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_getbucketpolicy.js 的 Node.js 模块。该模块将获取单个命令行参数,指定需要其策略的存储桶。确保按前面所示配置开发工具包。

创建 AWS.S3 服务对象。在调用 getBucketPolicy 方法时,您需要传递的唯一参数是所选存储桶的名称。如果桶当前具有策略,该策略在由 Amazon S3 传递到回调函数的 data 参数中返回。

如果所选存储桶没有策略,该信息将在 error 参数中返回给回调函数。

// 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 policy for selected bucket s3.getBucketPolicy(bucketParams, function (err, data) { if (err) { console.log("Error", err); } else if (data) { console.log("Success", data.Policy); } });

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

node s3_getbucketpolicy.js BUCKET_NAME

此示例代码可在 GitHub 上的此处找到。

设置简单存储桶策略

创建文件名为 s3_setbucketpolicy.js 的 Node.js 模块。该模块将获取单个命令行参数,指定需要应用其策略的存储桶。按前面所示配置 SDK。

创建 AWS.S3 服务对象。存储桶策略以 JSON 形式指定。首先,创建一个 JSON 对象,该对象包含用于指定策略的所有值,但标识存储桶的 Resource 值除外。

格式化策略所需的 Resource 字符串,在其中包含所选存储桶的名称。将该字符串插入到 JSON 对象中。准备 putBucketPolicy 方法的参数,包括存储桶的名称以及转换为字符串值的 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" }); var readOnlyAnonUserPolicy = { Version: "2012-10-17", Statement: [ { Sid: "AddPerm", Effect: "Allow", Principal: "*", Action: ["s3:GetObject"], Resource: [""], }, ], }; // create selected bucket resource string for bucket policy var bucketResource = "arn:aws:s3:::" + process.argv[2] + "/*"; readOnlyAnonUserPolicy.Statement[0].Resource[0] = bucketResource; // convert policy JSON into string and assign into params var bucketPolicyParams = { Bucket: process.argv[2], Policy: JSON.stringify(readOnlyAnonUserPolicy), }; // set the new policy on the selected bucket s3.putBucketPolicy(bucketPolicyParams, function (err, data) { if (err) { // display error message console.log("Error", err); } else { console.log("Success", data); } });

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

node s3_setbucketpolicy.js BUCKET_NAME

此示例代码可在 GitHub 上的此处找到。

删除存储桶策略

创建文件名为 s3_deletebucketpolicy.js 的 Node.js 模块。该模块将获取单个命令行参数,指定需要删除其策略的存储桶。按前面所示配置 SDK。

创建 AWS.S3 服务对象。在调用 deleteBucketPolicy 方法时,您需要传递的唯一参数是所选存储桶的名称。

// 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 policy for selected bucket s3.deleteBucketPolicy(bucketParams, function (err, data) { if (err) { console.log("Error", err); } else if (data) { console.log("Success", data); } });

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

node s3_deletebucketpolicy.js BUCKET_NAME

此示例代码可在 GitHub 上的此处找到。