# Mount S3 Bucket on instances with s3fs-fuse

## Prerequisite

* มียอดคงเหลือใน project มากกว่า 0 บาท ([ขั้นตอนการเติมเงินเข้าสู่ project](/ncs-documents/nipa-cloud-space-overview/billing-and-wallet/topup-a-project-wallet.md))
* ต้องมี bucket สร้างเอาไว้แล้ว ([ขั้นตอนในการสร้าง bucket](/ncs-documents/storage/object-storage-s3/create-an-object-storage-bucket.md))
* สร้าง bucket policy ให้มีสิทธิ์เข้าถึง ([ขั้นตอนในการสร้าง bucket policy](/ncs-documents/storage/object-storage-s3/create-bucket-policy.md))

## Instructions

### 1. ติดตั้ง package s3fs&#x20;

```bash
# Ubuntu and Debian
sudo apt install s3fs

# CentOS
sudo yum install epel-release
sudo yum install s3fs-fuse
```

### 2. สร้าง credential และกำหนด permission&#x20;

สร้าง sub-user และทำการสร้าง credential&#x20;

โดยก่อนที่จะนำ credential ไปใช้งานได้จะต้องตั้งค่า permission ก่อน ([ขั้นตอนในการสร้าง bucket policy](/ncs-documents/storage/object-storage-s3/create-bucket-policy.md))&#x20;

### 3. config credential file&#x20;

นำ access\_key และ secret\_key ที่ได้จากการสร้าง sub-user บนหน้า nipa cloud space นำมาเขียนลงในไฟล์

```bash
echo "ACCESS_KEY:SECRET_KEY" | sudo tee /etc/passwd-s3fs
```

และเพื่อให้ key ถูกเก็บอย่างปลอดภัย ให้ทำการ chmod 600 เพื่อจะให้มั่นใจว่า มีเฉพาะ root user ที่ทำการอ่านและเขียนไฟล์ได้เท่านั้น

```bash
chmod 600 /etc/passwd-s3fs
```

### 4. mouth your s3 bucket

ก่อนการ mount s3 bucket ให้ทำการสร้าง directory บนเครื่อง server ที่จะให้ object storage ถูก mount โดยสามารถใช้โฟล์เดอร์ที่มีอยู่แล้วหรือสร้างขึ้นใหม่ได้ตามคำสั่ง ดังนี้

```
sudo mkdir /mnt/my-bucket
```

ในขั้นตอนนี้หากต้องการ mount ชั่วคราวสามารถใช้ command ด้านล่างในการสั่งให้ s3fs เชื่อมต่อไปยัง bucket ที่กำหนด โดยมีคำสั่งดังนี้

```bash
sudo s3fs {bucketname} {/mountpoint} \
-o passwd_file=/etc/passwd-s3fs \
-o allow_other \
-o url=https://{nipa-s3-endpoint}
```

&#x20; โดยมีลายละเอียด option ดังนี้

* {bucketname} = ชื่อของ bucket ที่สร้าง ตัวอย่างเช่น my-bucket
* {/mountpoint} = path ที่ต้องการ mount ไฟล์จาก s3 bucket ตัวอย่างเช่น /mnt/my-bucket
* {nipa-s3-endpoint} = endpoint ของ s3 ที่ต้องการไปเชื่อมต่อ เช่นหากเลือกสร้าง bucket ที่ NCP-BKK จะได้ endpoint เป็น s3-bkk.nipa.cloud

ตัวอย่าง command

```bash
sudo s3fs my-bucket /mnt/my-bucket -o passwd_file=/etc/passwd-s3fs -o allow_other 
-o url=https://s3-bkk.nipa.cloud
```

### 5. ทดสอบ เขียนอ่านไฟล์

สามารถ test โดยการเขียนไฟล์ลงใน path ที่ทำการ mount s3 bucket ลงมา โดยใช้คำสั่งดังนี้

```bash
cd {/mountpoint} # แก้ไขเป็นของตนเอง
echo "1234" >> testing.txt

cat testing.txt
1234
```

### 6. Mounting your s3 bucket automatically on boot

หากต้องการทำให้การ mount อยู่ถาวร และอัตโนมัติเมื่อเปิดเครื่องขึ้นมา สามารถทำตามขั้นตอนดังนี้

ก่อนอื่นหากยัง mount อยู่ให้ทำการ unmount ก่อน โดยใช้คำสั่ง

```bash
umount {/mountpoint} # แก้ไขเป็นของตนเอง
```

ในการตั้งค่าให้ server mount s3 bucket ตั้งแต่ boot สามารถทำได้โดยการเพิ่ม s3fs command ไปยังบรรทัดใหม่ของไฟล์ /etc/fstab และจะมีคำสั่งดังนี้

```bash
s3fs#my-bucket /mnt/my-bucket fuse _netdev,allow_other,passwd_file=/etc/passwd-s3fs,url=https://s3-bkk.nipa.cloud 0 0
```


---

# 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/mount-s3-bucket-on-instances-with-s3fs-fuse.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.
