AWS S3 CLI


Amazon Simple Storage Service (S3) เป็นหนึ่งในบริการของ AWS ที่ใช้สำหรับเก็บไฟล์บนคลาวด์ รองรับทั้งไฟล์ขนาดเล็กและขนาดใหญ่ โดยมีจุดเด่นเรื่อง ความสามารถในการขยายขนาด (Scalability), ความพร้อมใช้งานของข้อมูล (Data Availability), ความปลอดภัย (Security), และ ประสิทธิภาพ (Performance)
เมื่อก่อนเราอาจเก็บไฟล์ รูปภาพ, PDF, HTML ไว้บนเซิร์ฟเวอร์ของตัวเอง ซึ่งหากลบไฟล์หรือปิดเซิร์ฟเวอร์ ไฟล์เหล่านั้นก็จะหายไป แต่ S3 มีความสามารถเพิ่มเติม เช่น การสำรองข้อมูล, การกำหนดสิทธิ์การเข้าถึง, และฟีเจอร์อื่นๆ ที่ช่วยให้การจัดเก็บไฟล์ปลอดภัยและยืดหยุ่นมากขึ้น
Bucket
เป็นเหมือนโฟลเดอร์หลัก ภายในจะมีไฟล์ (Object) เก็บไว้ เช่น /photos/puppy.jpg
เก็บอยู่ใน bucket ชื่อ amzn-s3-demo-bucket
ตอนสร้างกำหนดภูมิภาค US West Region จะสามารถเข้าถึงได้โดย URL https://amzn-s3-demo-bucket.s3.us-west-2.amazonaws.com/photos/puppy.jpg
Object
เหมือนเป็นไฟล์ทั่วไปที่อยู่ใน bucket โดย object จะเก็บไฟล์และ metadata ของไฟล์นั้นๆเช่น ชื่อไฟล์ ไฟล์ต้นฉบับ ประเภทไฟล์ วันที่อัพโหลด สิทธิ์การเข้าถึง
ลองใช้คำสั่ง aws cli
aws
aws [options] <command> <subcommand> [<subcommand> ...] [parameters]
ก่อนจะใช้ cli จะต้องมีสิทธิ์การใช้งานก่อน
aws s3 มีคำสั่งอะไรให้เราใช้บ้าง aws s3 help
aws s3 help
...
SYNOPSIS
aws s3 <Command> [<Arg> ...]
OPTIONS None
AVAILABLE COMMANDS
o cp
o ls
o mb
o mv
o presign
o rb
o rm
o sync
o website
cp
คำสั่งใช้สำหรับ download หรือ upload ไฟล์ระว่างเครื่องเรากับ S3
source path หรือ destination path สามารถเป็นได้ทั้งเครื่องเราหรือ s3 ก็ได้เช่น
# Copy Local to S3aws s3 cp ./test.txt s3://mybucket/test.txt
# Copy S3 to S3aws s3 cp s3://mybucket/test.txt s3://mybucket/test2.txt
# Copy S3 to Localaws s3 cp s3://mybucket/test.txt test.txt
# Copy S3 to S3 another bucketaws s3 cp s3://mybucket/test.txt s3://mybucket-2/
# Copy S3 to Local ทุกไฟล์# `--recursive` คือหลาย level เช่น ไฟล์ที่อยู่ใน folder ในสุดaws s3 cp s3://mybucket . --recursive
mv
คำสั่งใช้สำหรับย้ายไฟล์ สามารถใช้คำสั่งนี้ rename ไฟล์นี้
# Move Local to S3aws s3 mv test.txt s3://mybucket/test2.txt
# Move S3 to S3aws s3 mv s3://mybucket/test.txt s3://mybucket/test2.txt
# Move S3 to Localaws s3 mv s3://mybucket/test.txt test2.txt
# Move S3 to S3 another bucketaws s3 mv s3://mybucket/test.txt s3://mybucket2/
rm
คำสั่งใช้สำหรับลบไฟล์
# Delete S3 objectaws s3 rm s3://mybucket/test2.txt
# Delete all in bucketaws s3 rm s3://mybucket --recursiveaws s3 rm s3://mybucket --recursive --exclude "*.jpg"
sync
คำสั่งใช้สำหรับทำให้มีข้อมูลตรงกันเช่น การอัพโหลดไฟล์จาก Local ไปไว้บน bucket ให้มีไฟล์ตรงกัน โดยจะ sync แค่ไฟล์ที่มรกายเปลี่ยนแปลงเท่านั้น ใช้คำสั่งนี้ deploy static web ได้หลังจาก build เสร็จ
#### Local to bucketaws s3 sync . s3://mybucket
# delete all files that do not matchaws s3 sync . s3://mybucket --delete
# except .jpg filesaws s3 sync . s3://mybucket --exclude "*.jpg"
#### bucket to bucketaws s3 sync s3://mybucket s3://mybucket2
#### bucket to Localaws s3 sync s3://mybucket .
mb
คำสั่งใช้สำหรับสร้าง bucket ตั้งชื่อต้องไม่ซ้ำกับคนอื่น (global unique)
#### create bucketaws s3 mb s3://mybucketaws s3 mb s3://mybucket --region ap-southeast-1
rb
คำสั่งใช้สำหรับลบ bucket
# the bucket must be emptyaws s3 rb s3://mybucket
# first remove all of the objects in the bucket and then remove the bucket itselfaws s3 rb s3://mybucket --force
ls
คำสั่งใช้สำหรับเรียกดู bucket หรือ object ภายใน
# list all bucketsaws s3 ls
# in bucketaws s3 ls s3://mybucket
# prefix in bucketaws s3 ls s3://mybucket/noExistPrefix
# recursive all prefixes in bucketaws s3 ls s3://mybucket --recursive
# --human-readable displays file size, --summarize displays the total number of objects and total sizeaws s3 ls s3://mybucket --recursive --human-readable --summarize