> For the complete documentation index, see [llms.txt](https://docs-epc.gitbook.io/ncs-documents/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs-epc.gitbook.io/ncs-documents/storage/object-storage-s3/access-s3-buckets-with-aws-s3-client-sdk.md).

# Access S3 buckets With AWS S3 Client SDK

## Prerequisite

* ต้องมี bucket สร้างเอาไว้แล้ว ([ขั้นตอนในการสร้าง bucket](/ncs-documents/storage/object-storage-s3/create-an-object-storage-bucket.md))
* มีการสร้าง subuser และ ให้สิทธิ์การเข้าถึง bucket เอาไว้แล้ว ([ขั้นตอนการสร้าง subuser และ access key](https://docs-epc.gitbook.io/ncs-documents/~/changes/U2SVceQUXXCaek3t7CoE/object-storage/create-object-storage-sub-user))

## Preparing

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

```sh
npm install @aws-sdk/client-s3
```

<figure><img src="/files/p1qWjXeNcO1zuLAj8VZa" alt=""><figcaption><p>package.json</p></figcaption></figure>

## Getting started

### Import

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

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

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

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

* ทำการประกาศตัวแปรจาก S3Client เพื่อใช้สำหรับการ configuration และการส่ง request ต่างๆ
* ทำการประกาศตัวแปรจาก command ที่ต้องการใช้งาน และใส่ parameter ตามความต้องการของ command นั้น
* เรียกใช้งานคำสั่ง send จาก S3Client เพื่อทำการส่ง request โดยมี command ที่ต้องการใช้งานเป็น parameter

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

```js
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.
}
```
