AWS SDK for JavaScript V3 API 參考指南詳細介紹了 AWS SDK for JavaScript 版本 3(V3)的所有 API 操作。
本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。
開始使用 Node.js 的使用者
本指南說明如何初始化 NPM 套件、將服務用戶端新增至套件,以及如何使用 JavaScript SDK 呼叫服務動作。
該方案
使用一個執行下列作業的主要檔案建立新的 NPM 套件:
創建一個 Amazon 簡單存儲服務桶
把一個對象在 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 "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(); }
您可以在這裡
步驟 3:執行範例
注意
記得登入!如果您使用 IAM 身分中心進行驗證,請記得使用 AWS CLI aws sso login
命令登入。
執行
node index.js
。選擇是否要清空並刪除值區。
如果您沒有刪除值區,請務必手動清空並在稍後刪除。