Access S3 buckets With AWS S3 Client SDK

ตัวอย่างเบื้องต้นสำหรับกาเข้าใช้งาน Object storage (S3) ด้วย NodeJS ผ่าน AWS S3 Client SDK

Prerequisite

Preparing

  • ทำการติดตั้ง Package aws-sdk/client-s3

npm install @aws-sdk/client-s3

Getting started

Import

  • ทำการ import S3Client และ Command ที่ต้องการใช้งาน

จากตัวอย่างข้างล่างนี้ คือการ import S3Client และ Command ที่ต้องการใช้งานคือ ListBucketsCommand

import { S3Client, ListBucketsCommand } from "@aws-sdk/client-s3";

การทำงานเบื้องต้น

  • ทำการประกาศตัวแปรจาก S3Client เพื่อใช้สำหรับการ configuration และการส่ง request ต่างๆ

  • ทำการประกาศตัวแปรจาก command ที่ต้องการใช้งาน และใส่ parameter ตามความต้องการของ command นั้น

  • เรียกใช้งานคำสั่ง send จาก S3Client เพื่อทำการส่ง request โดยมี command ที่ต้องการใช้งานเป็น parameter

ตัวอย่างการทำงานเบื้องต้น

import { S3Client, ListBucketsCommand } from "@aws-sdk/client-s3";

const client = new S3Client({
  /** configuration */
});

const params = {
  /** input parameters */
};

const command = new ListBucketsCommand(params);

try {
  const data = await client.send(command);
  // process data.
} catch (error) {
  // error handling.
}

Last updated