Amazon SDK for JavaScriptV3 API 参考指南详细描述了Amazon SDK for JavaScript版本 3 (V3) 的所有 API 操作。
本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。
管理 Amazon S3 存储桶访问权限
此 Node.js 代码示例演示:
-
如何检索或设置 Amazon S3 存储桶的访问控制列表。
场场场场景
在本示例中,使用 Node.js 模块来显示选定存储桶的存储桶访问控制列表 (ACL),并为选定存储桶应用对 ACL 的更改。Node.js 模块使用 Amazon S3 客户端类的以下方法使用的 SDK JavaScript 来管理 Amazon S3 存储桶访问权限:
有关 Amazon S3 存储桶的访问控制列表的更多信息,请参阅《Amazon S imple Storage 用户指南》中的使用 ACL 管理访问权限。
先场景
要设置和运行此示例,您必须先完成以下任务:
-
按照中的说明设置项目环境以运行 Node JavaScript 示例 GitHub
。 -
使用用户凭证创建共享配置文件。有关提供共享凭证文件的更多信息,请参阅从共享凭证文件加载 Node.js 中的凭证。
这些示例演示了如何使用 ECMAScript6 (ES6) 导入/导出客户端服务对象和命令。
这需要 Node.js 13.x 或更高版本。要下载并安装最新版本的 Node.js,请参阅 Node.js 下载。
。 如果你更喜欢使用 CommonJS 语法,请参阅JavaScript es6/CommonJS 语法。
检索当前存储桶访问控制列表
创建一个libs
目录,然后使用该文件名创建 Node.js 模块s3Client.js
。将以下代码复制并粘贴到其中,这将创建 Amazon S3 客户端对象。将 RE G
ION 替换为您的Amazon区域。
// Create service client module using ES6 syntax. import { S3Client } from "@aws-sdk/client-s3"; // Set the AWS Region. const REGION = "us-east-1"; // Create an Amazon S3 service client object. const s3Client = new S3Client({ region: REGION }); export { s3Client };
此代码可在此处获得 GitHub
创建文件名为 s3_getbucketacl.js
的 Node.js 模块。确保按照前面所示配置 SDK,包括安装所需的客户端和软件包。
创建S3Client
客户端服务对象。在调用 GetBucketAclCommand
方法时,您需要传递的唯一参数是所选存储桶的名称。当前的访问控制列表配置由 Amazon S3 在传递给回调函数的data
参数中返回。
// Import required AWS SDK clients and commands for Node.js. import { GetBucketAclCommand } from "@aws-sdk/client-s3"; import { s3Client } from "./libs/s3Client.js"; // Helper function that creates an Amazon S3 service client module. // Create the parameters. export const bucketParams = { Bucket: "BUCKET_NAME" }; export const run = async () => { try { const data = await s3Client.send(new GetBucketAclCommand(bucketParams)); console.log("Success", data.Grants); return data; // For unit tests. } catch (err) { console.log("Error", err); } }; run();
要运行示例,请在命令提示符处输入以下内容。
node s3_getbucketacl.js
此示例代码可以在此处找到 GitHub
将访问控制列表权限添加到 Amazon S3 存储桶
创建一个libs
目录,然后使用该文件名创建 Node.js 模块s3Client.js
。将以下代码复制并粘贴到其中,这将创建 Amazon S3 客户端对象。将 RE G
ION 替换为您的Amazon区域。
// Create service client module using ES6 syntax. import { S3Client } from "@aws-sdk/client-s3"; // Set the AWS Region. const REGION = "us-east-1"; // Create an Amazon S3 service client object. const s3Client = new S3Client({ region: REGION }); export { s3Client };
此代码可在此处获得 GitHub
创建文件名为 s3_putbucketacl.js
的 Node.js 模块。确保按照前面所示配置 SDK,包括安装所需的客户端和软件包。
将 BUCKET_N
AME 替换为 Amazon S3 存储桶的名称。将 GRANTEE_1
和 GRANTEE_2
替换为要授予相应访问控制权限的用户。
// Import required AWS SDK clients and commands for Node.js. import { PutBucketAclCommand } from "@aws-sdk/client-s3"; import { s3Client } from "./libs/s3Client.js"; // Helper function that creates an Amazon S3 service client module. // Set the parameters. For more information, // see https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/S3.html#putBucketAcl-property. export const bucketParams = { Bucket: "BUCKET_NAME", // 'GrantFullControl' allows grantee the read, write, read ACP, and write ACL permissions on the bucket. // Use a canonical user ID for an AWS account, formatted as follows: // id=002160194XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXa7a49125274 GrantFullControl: "GRANTEE_1", // 'GrantWrite' allows grantee to create, overwrite, and delete any object in the bucket. // For example, 'uri=http://acs.amazonaws.com/groups/s3/LogDelivery' GrantWrite: "GRANTEE_2", }; export const run = async () => { try { const data = await s3Client.send(new PutBucketAclCommand(bucketParams)); console.log("Success, permissions added to bucket", data); return data; // For unit tests. } catch (err) { console.log("Error", err); } }; run();
要运行示例,请在命令提示符处输入以下内容。
node s3_putbucketacl.js
此示例代码可以在此处找到 GitHub