DEV Community

Nutchanon Leelapornudom for AWS Community ASEAN

Posted on

8 2

การแชร์ข้อมูลบน Amazon S3 ผ่าน Presigned URL

สารบัญ (Table of Content)

  1. บทนำ
  2. การประเมินราคาเบื้องต้น
  3. ขั้นตอนที่ 1: ทำการสร้าง IAM User
  4. ขั้นตอนที่ 2: กำหนด Amazon S3 Bucket Policy
  5. ขั้นตอนที่ 3: สร้าง presigned URL ของ Amazon S3 ผ่าน AWS CLI
  6. บทสรุป

บทนำ

โดยปกติแล้ว ทุก ๆ object ใน Amazon S3 จะเป็นลักษณะ private คือไม่สามารถแชร์ให้กับผู้ที่ไม่มีสิทธิเข้าถึงได้ แต่ในบางกรณี ผู้ใช้งานต้องการที่จะแชร์ข้อมูลใน Amazon S3 Bucket ให้ผู้ใช้งานทั่วไปสามารถเข้าถึงได้แบบ public ในช่วงระยะเวลาหนึ่ง ๆ แบบจำกัด

ทาง Amazon S3 มีความสามารถที่ให้ผู้ใช้งานสามารถสร้าง URL พิเศษ ที่มีสิทธิในการเข้าถึงข้อมูล Amazon S3 Bucket แบบ public เป็นการชั่วคราว เรียกว่า presigned URL โดยผู้ใช้งาน สามารถระบุเวลาที่จะให้หมดอายุได้อีกด้วย

การประเมินราคาเบื้องต้น (Cost Estimation)

  • ราคาการเรียกใช้งาน Amazon S3 - Amazon S3 Pricing

ขั้นตอนที่ 1: ทำการสร้าง IAM User

  • ทำการสร้าง IAM User โดยไม่ต้องกำหนด permission ใด ๆ ให้เลย IAM User

ขั้นตอนที่ 2: กำหนด Amazon S3 Bucket Policy

  • หากผู้ใช้งานยังไม่มี Amazon S3 Bucket ให้ทำการสร้างก่อน
  • ทำการกำหนดให้ IAM User ที่ต้องการให้สามารถอ่านข้อมูล object ใน Amazon S3 บน bucket ที่ระบุไว้ผ่าน Bucket Policy (อย่าลืมเปลี่ยน AWS_ACCOUNT_ID กับชื่อของ bucket
  • ผู้ใช้งานยังสามารถกำหนด policy เฉพาะส่วนของ prefix ที่ต้องการได้ เพื่อความปลอดภัยที่มากขึ้นได้
{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "s3-presigned-read-only",
            "Effect": "Allow",
            "Principal": {
                "AWS": "arn:aws:iam::<AWS_ACCOUNT_ID>:user/presigned"
            },
            "Action": "s3:GetObject",
            "Resource": "arn:aws:s3:::nutchanon-thailand-shared/*"
        }
    ]
}
Enter fullscreen mode Exit fullscreen mode

ขั้นตอนที่ 3: สร้าง presigned URL ของ Amazon S3 ผ่าน AWS CLI

  • ทำการติดตั้ง AWS CLI และสร้าง configuration profile ให้เรียบร้อย
  • จากนั้นสร้าง presigned URL ด้วย command ดังต่อไปนี้
    • โดยผู้ใช้งานสามารถเลือก option "--expires-in" ในการกำหนดเวลาที่จะให้ presigned URL หมดอายุในหลักวินาทีได้อีกด้วย
aws s3 presign s3://${S3_BUCKET}/${S3_OBJECT} --expires-in ${S3_EXPIRE} --profile default
Enter fullscreen mode Exit fullscreen mode
  • ผู้ใช้งานจะได้รับ presigned URL เป็น response ที่สามารถนำไปแชร์ให้กับผู้อื่นได้
  • นอกจากวิธีการสร้าง presigned URL ผ่าน AWS CLI แล้ว ผู้ใช้งานยังสามารถสร้างจาก AWS SDK ในภาษาต่าง ๆ ได้อีกด้วย

บทสรุป

ผู้ใช้งานสามารถเลือกที่จะแชร์ข้อมูลบน Amazon S3 ผ่าน Presigned URL ที่สามารถกำหนดให้ผู้อื่นเข้าถึงข้อมูลแบบชั่วคราวได้ โดยจะเป็นการเพิ่มความปลอดภัยให้กับข้อมูล และยังช่วยให้ผู้ใช้งานไม่ต้องจดจำสิทธิหรือ configuration ต่าง ๆ

นอกจากนี้การทำ presigned URL บน Amazon S3 ยังสามารถนำไปประยุกต์ใช้กับระบบ automation ได้หลากหลาย ทั้งการติดต่อกับ Amazon API Gateway ในกรณีที่ต้องการ upload/download ข้อมูลขนาดใหญ่ผ่าน API ได้อีกด้วย[3]

แหล่งอ้างอิงอื่น ๆ

[1] https://docs.aws.amazon.com/AmazonS3/latest/userguide/ShareObjectPreSignedURL.html
[2] https://github.com/nutchanon-l/s3-presigned-url
[3] https://aws.amazon.com/blogs/compute/uploading-to-amazon-s3-directly-from-a-web-or-mobile-application/

CastleArm

Image of Datadog

The Future of AI, LLMs, and Observability on Google Cloud

Datadog sat down with Google’s Director of AI to discuss the current and future states of AI, ML, and LLMs on Google Cloud. Discover 7 key insights for technical leaders, covering everything from upskilling teams to observability best practices

Learn More

Top comments (0)

Image of Docusign

🛠️ Bring your solution into Docusign. Reach over 1.6M customers.

Docusign is now extensible. Overcome challenges with disconnected products and inaccessible data by bringing your solutions into Docusign and publishing to 1.6M customers in the App Center.

Learn more