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


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs-epc.gitbook.io/ncs-documents/storage/object-storage-s3/access-s3-buckets-with-aws-s3-client-sdk.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
