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 儲存貯體中的物件
確認使用者是否要刪除資源
必要條件
您必須先執行下列動作,才能執行範例:
-
設定您的SDK身分驗證。如需詳細資訊,請參閱SDK 使用 進行身分驗證 AWS。
-
安裝 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:執行範例
注意
請記得登入!如果您使用 IAM Identity Center 進行身分驗證,請記得使用 AWS CLI aws sso login
命令登入。
執行
node index.js
。選擇是否清空並刪除儲存貯體。
如果您未刪除儲存貯體,請務必手動清空並稍後刪除。