Amazon SDK for JavaScript V3 API 参考指南详细描述了 Amazon SDK for JavaScript 版本 3 (V3) 的所有API操作。
本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。
开始使用 Node.js
本指南向您介绍如何初始化NPM软件包、向包中添加服务客户端以及 JavaScript SDK如何使用调用服务操作。
情景
使用一个主文件创建一个新NPM包,该文件执行以下操作:
创建 Amazon Simple Storage Service 存储桶
将对象放入 Amazon S3 存储桶
读取 Amazon S3 存储桶中的对象
确认用户是否要删除资源
先决条件
在运行示例之前,您必须先执行以下操作:
-
配置您的SDK身份验证。有关更多信息,请参阅 SDK使用进行身份验证 Amazon。
-
安装 Node.js
。
步骤 1:设置软件包结构并安装客户端程序包
设置程序包结构并安装客户端程序包:
创建一个新文件夹
nodegetstarted
用于包含程序包。从命令行导航到新文件夹。
运行以下命令以创建默认的
package.json
文件:npm init -y
要安装 Amazon S3 客户端程序包,请运行以下命令:
npm i @aws-sdk/client-s3
-
将
"type": "module"
添加到package.json
文件。这告诉 Node.js 使用现代ESM语法。最终的package.json
应类似于以下内容:{ "name": "example-javascriptv3-get-started-node", "version": "1.0.0", "description": "This guide shows you how to initialize an NPM package, add a service client to your package, and use the JavaScript SDK to call a service action.", "main": "index.js", "scripts": { "test": "vitest run **/*.unit.test.js" }, "author": "Your Name", "license": "Apache-2.0", "dependencies": { "@aws-sdk/client-s3": "^3.420.0" }, "type": "module" }
第 2 步:添加必要的导入和SDK代码
将以下代码添加到 nodegetstarted
文件夹中名为 index.js
的文件中。
// This is used for getting user input. import { createInterface } from "node:readline/promises"; import { S3Client, PutObjectCommand, CreateBucketCommand, DeleteObjectCommand, DeleteBucketCommand, paginateListObjectsV2, GetObjectCommand, } from "@aws-sdk/client-s3"; export async function main() { // A region and credentials can be declared explicitly. For example // `new S3Client({ region: 'us-east-1', credentials: {...} })` would //initialize the client with those settings. However, the SDK will // use your local configuration and credentials if those properties // are not defined here. const s3Client = new S3Client({}); // Create an Amazon S3 bucket. The epoch timestamp is appended // to the name to make it unique. const bucketName = `test-bucket-${Date.now()}`; await s3Client.send( new CreateBucketCommand({ Bucket: bucketName, }), ); // Put an object into an Amazon S3 bucket. await s3Client.send( new PutObjectCommand({ Bucket: bucketName, Key: "my-first-object.txt", Body: "Hello JavaScript SDK!", }), ); // Read the object. const { Body } = await s3Client.send( new GetObjectCommand({ Bucket: bucketName, Key: "my-first-object.txt", }), ); console.log(await Body.transformToString()); // Confirm resource deletion. const prompt = createInterface({ input: process.stdin, output: process.stdout, }); const result = await prompt.question("Empty and delete bucket? (y/n) "); prompt.close(); if (result === "y") { // Create an async iterator over lists of objects in a bucket. const paginator = paginateListObjectsV2( { client: s3Client }, { Bucket: bucketName }, ); for await (const page of paginator) { const objects = page.Contents; if (objects) { // For every object in each page, delete it. for (const object of objects) { await s3Client.send( new DeleteObjectCommand({ Bucket: bucketName, Key: object.Key }), ); } } } // Once all the objects are gone, the bucket can be deleted. await s3Client.send(new DeleteBucketCommand({ Bucket: bucketName })); } } // Call a function if this file was run directly. This allows the file // to be runnable without running on import. import { fileURLToPath } from "node:url"; if (process.argv[1] === fileURLToPath(import.meta.url)) { main(); }
示例代码可以在此处找到 GitHub
步骤 3:运行示例
注意
请记得登录!如果您使用 Ident IAM ity Center 进行身份验证,请记住使用 Amazon CLI aws sso login
命令登录。
运行
node index.js
。选择是否清空并删除存储桶。
如果您不删除存储桶,请务必手动清空并稍后将其删除。