Amazon SDK for JavaScriptV3 API 参考指南详细描述了Amazon SDK for JavaScript版本 3 (V3) 的所有 API 操作。
本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。
配置 Amazon S3 存储桶
此 Node.js 代码示例演示:
-
如何为存储桶配置跨源资源共享 (CORS) 权限。
场场场场景
在本示例中,使用一系列 Node.js 模块来列出您的 Amazon S3 存储桶以及配置 CORS 和存储桶日志记录。Node.js 模块使用的 SDK 使用 Amazon S3 客户端类的以下方法配置选定的 Amazon S3 存储桶: JavaScript
有关在 Amazon S3 存储桶中使用 CORS 配置的更多信息,请参阅《Amazon S imple Storage 用户指南》中的跨源资源共享(CORS)。
先场景
要设置和运行此示例,您必须先完成以下任务:
-
按照中的说明设置项目环境以运行 Node JavaScript 示例 GitHub
。 -
使用用户凭证创建共享配置文件。有关提供共享凭证文件的更多信息,请参阅从共享凭证文件加载 Node.js 中的凭证。
这些示例演示了如何使用 ECMAScript6 (ES6) 导入/导出客户端服务对象和命令。
这需要 Node.js 13.x 或更高版本。要下载并安装最新版本的 Node.js,请参阅 Node.js 下载。
。 如果你更喜欢使用 CommonJS 语法,请参阅JavaScript es6/CommonJS 语法。
检索存储桶 CORS 配置
创建一个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 };
创建文件名为 s3_getcors.js
的 Node.js 模块。模块将获取单个命令行参数来指定需要其 CORS 配置的存储桶。确保按照前面所示配置 SDK,包括安装所需的客户端和软件包。创建S3
客户端服务对象。
在调用 GetBucketCorsCommand
方法时,您需要传递的唯一参数是所选存储桶的名称。如果存储桶当前有 CORS 配置,则该配置将由 Amazon S3 作为传递给回调函数的data
参数的CORSRules
属性返回。
如果所选存储桶没有 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 客户端对象。将 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_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) { 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