开始使用 Node.js - AWS SDK for JavaScript

AWS SDK for JavaScript V3 API 参考指南详细描述了 AWS SDK for JavaScript 版本 3 (V3) 的所有 API 操作。

本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。

开始使用 Node.js

本指南向您演示了如何初始化 NPM 软件包、向软件包中添加服务客户端以及如何使用 JavaScript SDK 调用服务操作。

情景

使用一个执行以下操作的主文件创建一个新的 NPM 软件包:
  • 创建 Amazon Simple Storage Service 存储桶

  • 将对象放入 Amazon S3 存储桶

  • 读取 Amazon S3 存储桶中的对象

  • 确认用户是否要删除资源

先决条件

在运行示例之前,您必须先执行以下操作:

步骤 1:设置软件包结构并安装客户端程序包

设置程序包结构并安装客户端程序包:

  1. 创建一个新文件夹 nodegetstarted 用于包含程序包。

  2. 从命令行导航到新文件夹。

  3. 运行以下命令以创建默认的 package.json 文件:

    npm init -y
  4. 要安装 Amazon S3 客户端程序包,请运行以下命令:

    npm i @aws-sdk/client-s3
  5. "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 "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 "url"; if (process.argv[1] === fileURLToPath(import.meta.url)) { main(); }

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

步骤 3:运行示例

注意

请记得登录!如果您使用 IAM Identity Center,请记住使用 AWS CLI aws sso login 命令登录。

  1. 运行 node index.js

  2. 选择是否清空并删除存储桶。

  3. 如果您不删除存储桶,请务必手动清空并稍后将其删除。