Mount S3 Bucket on instances with s3fs-fuse

หากผู้ใช้งานต้องการนำ object storage ร่วมกับ instance เพื่อเข้าถึงไฟล์ต่างๆ บน bucket ของตัวเองนั้น สามารถนำ s3 bucket mount ลงเป็น file system ได้ดังนี้

Prerequisite

Instructions

1. ติดตั้ง package s3fs

# Ubuntu and Debian
sudo apt install s3fs

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

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

สร้าง sub-user และทำการสร้าง credential

โดยก่อนที่จะนำ credential ไปใช้งานได้จะต้องตั้งค่า permission ก่อน (ขั้นตอนในการสร้าง bucket policy)

3. config credential file

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

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

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

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 ที่กำหนด โดยมีคำสั่งดังนี้

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

โดยมีลายละเอียด 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

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 ลงมา โดยใช้คำสั่งดังนี้

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

cat testing.txt
1234

6. Mounting your s3 bucket automatically on boot

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

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

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

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

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

Last updated