S3 สำหรับจัดเก็บไฟล์และเหตุการณ์
ทำความเข้าใจการใช้ Amazon S3 เพื่อจัดเก็บและเรียกคืนออบเจ็กต์ขนาดใหญ่ รวมถึงวิธีที่เหตุการณ์จาก S3 สามารถกระตุ้น Lambda เพื่อประมวลผลข้อมูล
S3 สำหรับจัดเก็บไฟล์และเหตุการณ์ เป็นบทเรียน Serverless AWS Lambda Development ฟรีบน CoddyKit นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Serverless AWS Lambda Development และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Serverless AWS Lambda Development มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
S3 & Lambda: Event Power
Welcome! In this lesson, we'll dive into how Amazon S3, a powerful storage service, works hand-in-hand with AWS Lambda.
You'll learn to store and retrieve files, and critically, how S3 can trigger your Lambda functions automatically when something happens to your data.
Amazon S3: Cloud Storage
Amazon S3 (Simple Storage Service) is a highly scalable, durable, and available object storage service.
- It stores data as objects within buckets.
- Objects are files (images, videos, documents) and their metadata.
- Buckets are like top-level folders, globally unique names.
- S3 is perfect for storing large amounts of unstructured data.
Storing Objects in S3
You can easily put objects into an S3 bucket. Each object has a key, which is its unique identifier within the bucket.
- Console: Upload files directly via the AWS Management Console.
- CLI: Use the AWS Command Line Interface for scripted uploads.
- SDKs: Programmatically upload from your application using AWS SDKs (e.g., Python Boto3, Node.js SDK).
Retrieving Objects from S3
Getting your data back from S3 is just as simple. You reference the bucket name and the object's key.
S3 also offers features like versioning to keep multiple versions of an object, and pre-signed URLs for temporary, secure access to private objects.
S3 Event Notifications
One of S3's most powerful features for serverless is Event Notifications. S3 can notify you when specific events occur in your buckets.
These events include:
- Object creation (PUT, POST, COPY)
- Object deletion
- Object restoration from Glacier
These notifications can be sent to various AWS services, including Lambda functions!
Configure an S3 Trigger
Setting up an S3 trigger for Lambda is done via the S3 console or infrastructure-as-code tools.
You specify:
- The event type (e.g.,
ObjectCreated:Put). - The destination (your Lambda function).
- Optional prefix or suffix to filter events (e.g., only trigger for files in the
images/folder or.jpgfiles).
Lambda Processing S3 Events
When an S3 event occurs, Lambda invokes your function and passes the event details as a JSON payload to your handler. Let's see an example in Python.
import json
def lambda_handler(event, context):
print("Received S3 event!")
for record in event['Records']:
bucket_name = record['s3']['bucket']['name']
object_key = record['s3']['object']['key']
event_name = record['eventName']
print(f"Bucket: {bucket_name}")
print(f"Object Key: {object_key}")
print(f"Event Type: {event_name}")
# In a real scenario, you'd download and process the object here
# using the bucket_name and object_key
print(f"Processing '{object_key}' from '{bucket_name}'...")
return {
'statusCode': 200,
'body': json.dumps('S3 event processed successfully!')
}Understanding Event Payload
The event object passed to your Lambda function's handler contains crucial information about the S3 event.
Records: A list, as multiple events can be batched.s3: Contains details about the bucket and object involved.bucket.name: The name of the S3 bucket.object.key: The full path/key of the object that triggered the event.eventName: Describes the specific S3 action (e.g.,ObjectCreated:Put).
Common Use Case: Image Resize
A classic serverless pattern is image processing. Imagine a mobile app where users upload photos:
- User uploads a high-res image to an S3 bucket.
- S3 triggers a Lambda function (e.g.,
ObjectCreated:Putevent). - Lambda downloads the image, resizes it to various thumbnails.
- Lambda uploads the thumbnails to a different S3 bucket.
This is highly scalable and cost-effective!
S3 Event Triggers Check
Which of the following statements are TRUE about S3 Event Notifications for Lambda?
Recap: S3 Events for Lambda
Great job! You've learned how S3 acts as both a storage solution and a powerful event source for Lambda.
- S3 stores objects in buckets, offering high durability and scalability.
- S3 Event Notifications allow specific actions (like object creation/deletion) to trigger Lambda functions.
- Lambda functions receive a JSON payload with event metadata, not the full object.
- This pattern enables powerful serverless workflows like image processing or data transformations.
คำถามที่พบบ่อย
บทเรียน “S3 สำหรับจัดเก็บไฟล์และเหตุการณ์” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “S3 สำหรับจัดเก็บไฟล์และเหตุการณ์” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Serverless AWS Lambda Development ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Serverless AWS Lambda Development มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “S3 สำหรับจัดเก็บไฟล์และเหตุการณ์”
ทำความเข้าใจการใช้ Amazon S3 เพื่อจัดเก็บและเรียกคืนออบเจ็กต์ขนาดใหญ่ รวมถึงวิธีที่เหตุการณ์จาก S3 สามารถกระตุ้น Lambda เพื่อประมวลผลข้อมูล คุณปฏิบัติ Serverless AWS Lambda Development ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Serverless AWS Lambda Development หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Serverless AWS Lambda Development บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน
บทเรียน “S3 สำหรับจัดเก็บไฟล์และเหตุการณ์” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Serverless AWS Lambda Development นี้ได้ไหม
ได้ บทเรียน Serverless AWS Lambda Development ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- การผสานรวมกับ DynamoDB
- S3 สำหรับจัดเก็บไฟล์และเหตุการณ์
- การเลือกแหล่งจัดเก็บข้อมูลที่เหมาะสม
- การแคชด้วย Amazon ElastiCache และ DAX