# Migrate file S3 AWS to S3 NIPA

ในกรณีที่ลูกค้าต้องการย้ายข้อมูลจาก Amazon S3 มาใช้งาน S3 Bucket ของ NIPA Cloud Space สามารถดำเนินการ โดยใช้ s3fs และ rsync ได้ตามขั้นตอนต่อไปนี้

## Restriction

1. ต้องจัดเตรียม S3 Bucket, Access ID และ Secret Key ของ AWS และ NIPA Cloud Space เพื่อใช้สำหรับ mount s3fs (สำหรับ AWS สามารถเตรียมโดยรายละเอียดเพิ่มเติมตาม link :<https://www.nakivo.com/blog/mount-amazon-s3-as-a-drive-how-to-guide/>)
2. เตรียม Compute Instance สำหรับ mount s3fs (ตัวอย่างในเอกสารใช้ Ubuntu 22.04)

## Instruction

ขั้นตอนการ sync file จาก S3 AWS มายัง Object Storage ของ NIPA Cloud Space รายละเอียดดังนี้

### 1. Create Bucket on NIPA Cloud Space

ดำเนินการสร้าง S3 bucket บน NIPA Cloud Space ขั้นตอนการสร้างรายละเอียดตาม link ด้านล่าง

* สร้าง bucket เพื่อใช้เป็น container เก็บ object ต่างๆ สำหรับ object storage\
  <https://docs-epc.gitbook.io/ncs-documents/object-storage/create-an-object-storage-bucket>
* ในการ authen เข้าใช้งาน bucket ที่สร้างบน Nipa Cloud Space จะทำโดยใช้ access ID และ secret key ที่อยู่ใน sub-user\
  <https://docs-epc.gitbook.io/ncs-documents/object-storage/create-object-storage-sub-user>
* สร้าง policy เพื่อจำกัดการเข้าใช้งาน bucket\
  <https://docs-epc.gitbook.io/ncs-documents/object-storage/create-bucket-policy>

### 2. Secure Shell

เมื่อสร้าง S3 Bucket Nipa และ user Access-ID, Secret Key เรียบร้อย ให้ SSH ไปยัง instance ที่สร้างไว้

### 3. Install s3fs dependencies

ติดตั้ง dependency สำหรับใช้งาน s3fs

```
apt update
apt-get -y install automake autotools-dev fuse g++ git libcurl4-gnutls-dev libfuse-dev libssl-dev libxml2-dev make pkg-config

# ติดตั้ง s3fs-fuse
git clone https://github.com/s3fs-fuse/s3fs-fuse.git
cd s3fs-fuse/
./autogen.sh
./configure --prefix=/usr --with-openssl
make
make install

#ตรวจสอบว่าติดตั้ง s3fs สำเร็จหรือไม่
which s3fs
```

![](https://t9003005656.p.clickup-attachments.com/t9003005656/02dd8814-09ba-4469-a62a-46ccbfc30119/image.png)

### 4. Create Access Key File

ดำเนินการสร้างไฟล์สำหรับเก็บ Access ID, Secret Key

```
# Access ID สำหรับ mount S3 AWS
echo "ACCESS_KEY:SECRET_KEY" | sudo tee /etc/passwd-s3fs-aws
# Access ID สำหรับ mount S3 NIPA
echo "ACCESS_KEY:SECRET_KEY" | sudo tee /etc/passwd-s3fs-nipa

# เปลี่ยน Permission ของไฟล์
chmod 600 /etc/passwd-s3fs-aws
chmod 600 /etc/passwd-s3fs-nipa
```

### 5. Create Paths

ดำเนินการสร้าง Path สำหรับ mount S3 AWS และ mount S3 NIPA

```
mkdir -p /mnt/s3aws
mkdir -p /mnt/s3nipacloud
```

### 6. Mount S3 Bucket

ดำเนินการ mount S3 Bucket AWS ไปยัง /mnt/s3aws โดยมีรายละเอียด option ดังนี้

```
sudo s3fs {bucketname} {/mountpoint} -o passwd_file=/etc/passwd-s3fs-aws
```

{bucketname} = ชื่อของ bucket ที่สร้าง ตัวอย่างเช่น my-bucket

{/mountpoint} = path ที่ต้องการ mount ไฟล์จาก s3 bucket aws ตัวอย่างเช่น /mnt/s3aws

```
# ตัวอย่าง Command
sudo s3fs my-bucket /mnt/s3aws -o passwd_file=/etc/passwd-s3fs-aws
```

### 7. Mount NCS Bucket

ดำเนินการ mount S3 Bucket NIPA ไปยัง /mnt/s3nipacloud

```
sudo s3fs {bucketname} {/mountpoint} -o passwd_file=/etc/passwd-s3fs-nipa -o allow_other -o url=https://s3-bkk.nipa.cloud
```

{bucketname} = ชื่อของ bucket ที่สร้าง ตัวอย่างเช่น my-bucket

{/mountpoint} = path ที่ต้องการ mount ไฟล์จาก s3 bucket aws ตัวอย่างเช่น /mnt/s3aws

```
# ตัวอย่าง Command
sudo s3fs my-bucket /mnt/s3nipacloud -o passwd_file=/etc/passwd-s3fs-nipa -o allow_other -o url=https://s3-bkk.nipa.cloud
```

### 8. Check Mounting

ตรวจสอบว่าสามารถ mount S3 Bucket สำเร็จหรือไม่ โดย run command

```
df -h
```

![](https://t9003005656.p.clickup-attachments.com/t9003005656/92cbde96-9f03-4197-906a-b45f71d0a693/image.png)

### 9. Syncing Buckets

ดำเนินการ sync file จาก S3 Bucket AWS ไปยัง S3 Bucket NIPA

```
rsync -av --progress {/path/to/source/} {/path/to/destination/}

# ตัวอย่าง Command
rsync -av --progress /mnt/s3aws/ /mnt/s3nipacloud/
```

### 10. Checking

เมื่อ sync ไฟล์เสร็จเรียบร้อย ให้ตรวจสอบไฟล์ใน S3 Bucket NIPA ว่าครบหรือไม่โดยสามารถ run command

```
diff -r {/path/to/source} {/path/to/destination}
# คำสั่ง diff จะแสดงความแตกต่างระหว่างไฟล์ในไดเรกทอรีต้นทางและไดเรกทอรีปลายทาง ถ้าไม่มีผลลัพธ์ที่แสดงออกมา แสดงว่าไฟล์ต้นทางและปลายทางเท่ากัน

#ตัวอย่าง Command
diff -r /mnt/s3aws /mnt/s3nipacloud
```
