帮助我们改进AWS SDK for JavaScript版本 3 (V3) 文档,方法是使用反馈链接,或者在上创建议题或拉取请求GitHub
这些区域有:AWS SDK for JavaScriptV3 API 参考指南详细描述了所有的 API 操作AWS SDK for JavaScript版本 3 (V3)。
本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。
配置 Amazon S3 存储桶
此 Node.js 代码示例演示:
-
如何为存储桶配置跨源资源共享 (CORS) 权限。
场景
在本示例中,使用一系列 Node.js 模块来列出您的 Amazon S3 存储桶以及配置 CORS 和存储桶日志记录。Node.js 模块使用 SDK JavaScript 使用 Amazon S3 客户端类的以下方法来配置选定 Amazon S3 存储桶:
有关将 CORS 配置与 Amazon S3 存储桶结合使用的更多信息,请参阅。跨源资源共享 (CORS)中的Amazon Simple Storage Service 用户指南.
先决条件任
要设置和运行此示例,您必须先完成以下任务:
-
设置项目环境以运行 Node JavaScript 按照上的说明操作示例GitHub
. -
使用用户凭证创建共享配置文件。有关提供共享凭证文件的更多信息,请参阅从共享凭证文件加载 Node.js 中的凭证。
这些示例演示了如何使用 ECMASCRIPT6 (ES6) 导入/导出客户端服务对象和命令。
这需要 Node.js 版本 13.x 或更高版本。要下载并安装最新版本的 Node.js,请参阅Node.js 下载。
. 如果您希望使用 Simple Js 语法,请参阅JavaScript ES6/常用JS 语法.
检索存储桶 CORS 配置
创建libs
创建文件名为的 Node.js 模块s3Client.js
. 将以下代码复制并粘贴到其中,这将创建 Amazon S3 客户端对象。Replace领域
使用您的AWSregion 区域。
// Create service client module using ES6 syntax. import { S3Client } from "@aws-sdk/client-s3"; // Set the AWS Region. const REGION = "REGION"; //e.g. "us-east-1" // Create an Amazon S3 service client object. const s3Client = new S3Client({ region: REGION }); export { s3Client };
创建文件名为 s3_getcors.js
的 Node.js 模块。模块将获取单个命令行参数来指定需要其 CORS 配置的存储桶。确保按前面所示配置开发工具包,包括安装所需的客户端和软件包。创建S3
客户端服务对象。
在调用 GetBucketCorsCommand
方法时,您需要传递的唯一参数是所选存储桶的名称。如果存储桶当前具有 CORS 配置,该配置 Amazon S3 作为CORSRules
的财产data
参数传递给回调函数。
如果所选存储桶没有 CORS 配置,该信息将在 error
参数中返回到回调函数。
// Import required AWS SDK clients and commands for Node.js. import { GetBucketCorsCommand } from "@aws-sdk/client-s3"; import { s3Client } from "./libs/s3Client.js"; // Helper function that creates an Amazon S3 service client module. // Create the parameters for calling export const bucketParams = { Bucket: "BUCKET_NAME" }; export const run = async () => { try { const data = await s3Client.send(new GetBucketCorsCommand(bucketParams)); console.log("Success", JSON.stringify(data.CORSRules)); return data; // For unit tests. } catch (err) { console.log("Error", err); } }; run();
要运行示例,请在命令提示符处输入以下内容。
node s3_getcors.js
此示例代码可在 GitHub 上的此处
设置存储桶 CORS 配置
创建libs
创建文件名为的 Node.js 模块s3Client.js
. 将以下代码复制并粘贴到其中,这将创建 Amazon S3 客户端对象。Replace领域
使用您的AWSregion 区域。
// Create service client module using ES6 syntax. import { S3Client } from "@aws-sdk/client-s3"; // Set the AWS Region. const REGION = "REGION"; //e.g. "us-east-1" // Create an Amazon S3 service client object. const s3Client = new S3Client({ region: REGION }); export { s3Client };
这个代码是可用的GitHub 上的
创建文件名为 s3_setcors.js
的 Node.js 模块。该模块获取多个命令行参数,第一个参数指定要设置其 CORS 配置的存储桶。其他参数枚举您希望允许对存储桶使用的 HTTP 方法(POST、GET、PUT、PATCH、DELETE、POST)。如前所示配置 SDK,包括安装所需的客户端和软件包。
接下来,根据 S3
服务对象的 PutBucketCorsCommand
方法的要求,创建一个 JSON 对象来保存 CORS 配置的值。为 AllowedHeaders
值指定 "Authorization"
,为 AllowedOrigins
值指定 "*"
。最初,将 AllowedMethods
的值设置为空数组。
指定允许的方法作为 Node.js 模块的命令行参数,添加与参数之一匹配的各个方法。将生成的 CORS 配置添加到 CORSRules
参数中包含的配置的数组。在 Bucket
参数中指定您要为 CORS 配置的存储桶。
// Import required AWS-SDK clients and commands for Node.js. import { PutBucketCorsCommand } from "@aws-sdk/client-s3"; import { s3Client } from "./libs/s3Client.js"; // Helper function that creates an Amazon S3 service client module. // Set parameters. // Create initial parameters JSON for putBucketCors. const thisConfig = { AllowedHeaders: ["Authorization"], AllowedMethods: [], AllowedOrigins: ["*"], ExposeHeaders: [], MaxAgeSeconds: 3000, }; // Assemble the list of allowed methods based on command line parameters const allowedMethods = []; process.argv.forEach(function (val, index, array) { if (val.toUpperCase() === "POST") { allowedMethods.push("POST"); } if (val.toUpperCase() === "GET") { allowedMethods.push("GET"); } if (val.toUpperCase() === "PUT") { allowedMethods.push("PUT"); } if (val.toUpperCase() === "PATCH") { allowedMethods.push("PATCH"); } if (val.toUpperCase() === "DELETE") { allowedMethods.push("DELETE"); } if (val.toUpperCase() === "HEAD") { allowedMethods.push("HEAD"); } }); // Copy the array of allowed methods into the config object thisConfig.AllowedMethods = allowedMethods; // Create an array of configs then add the config object to it. const corsRules = new Array(thisConfig); // Create CORS parameters. export const corsParams = { Bucket: "BUCKET_NAME", CORSConfiguration: { CORSRules: corsRules }, }; export async function run() { try { const data = await s3Client.send(new PutBucketCorsCommand(corsParams)); console.log("Success", data); return data; // For unit tests. } catch (err) { console.log("Error", err); } } run();
要运行示例,请在命令提示符处输入以下内容,其中包括一个或多个如所示的 HTTP 方法。
node s3_setcors.js
此示例代码可在 GitHub 上的此处