0Pricing
AWS for Backend Developers (EC2, S3, RDS, Lambda) · บทเรียน

การแจ้งเตือนเหตุการณ์ของ S3

กำหนดค่า S3 ให้เผยแพร่เหตุการณ์ไปยัง Lambda, SQS หรือ SNS เมื่อเกิดการกระทำที่ระบุบนออบเจ็กต์ เพื่อเปิดใช้กระบวนการทำงานแบบตอบสนองที่ทรงพลัง

การแจ้งเตือนเหตุการณ์ของ S3 เป็นบทเรียน AWS for Backend Developers (EC2, S3, RDS, Lambda) ฟรีบน CoddyKit นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน AWS for Backend Developers (EC2, S3, RDS, Lambda) และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส AWS for Backend Developers (EC2, S3, RDS, Lambda) มีบทเรียนทั้งหมด 4 บทเรียน

บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ

Intro to S3 Event Notifications

Welcome to this lesson on S3 Event Notifications! This powerful feature allows your S3 buckets to automatically notify other AWS services when specific actions occur.

Think of it as setting up an alert system for your data. When a file is uploaded, deleted, or modified, S3 can tell another service to take action.

The Event Flow

S3 event notifications work by publishing an event message whenever a configured action happens in your bucket. These messages contain details about the event, such as the bucket name, object key, and event time.

  • Object Created: When a new file is uploaded.
  • Object Deleted: When a file is removed.
  • Object Restore: When an archived object is restored.

These events enable powerful, reactive workflows for your applications.

Common Event Destinations

S3 can send these event notifications to several AWS services. The most common destinations are:

  • AWS Lambda: For running custom code in response to events (e.g., resizing images, processing data).
  • Amazon SQS (Simple Queue Service): For queuing events reliably for later processing by other applications.
  • Amazon SNS (Simple Notification Service): For fanning out notifications to multiple subscribers (e.g., sending emails, triggering other services).

Choosing the right destination depends on your specific use case.

Setting Up Notifications

Configuring S3 event notifications is done at the bucket level. You define which event types you want to monitor and which destination service should receive the notifications.

You can also apply filters to only trigger notifications for objects with specific prefixes (folders) or suffixes (file extensions).

Example: Notifying on New Objects

A very common use case is triggering an event when new objects are created (uploaded) to an S3 bucket. This can kick off an automated process.

For instance, if users upload profile pictures, an s3:ObjectCreated:Put event can trigger a Lambda function to resize the image and store different versions.

Lambda for Processing Events

AWS Lambda is an excellent choice for processing S3 events because it's serverless. You only pay when your code runs, and it scales automatically.

When S3 sends an event to Lambda, the event details are passed as an input to your Lambda function, allowing your code to react specifically to what happened.

Lambda Event Handler

Here's a simple Python Lambda function that receives and logs an S3 event. This is the entry point for your custom logic.

Try running it to see how the event structure looks!

import json

def lambda_handler(event, context):
    print("Received S3 event:")
    print(json.dumps(event, indent=2))

    # Extract bucket and key from the event
    for record in event['Records']:
        bucket_name = record['s3']['bucket']['name']
        object_key = record['s3']['object']['key']
        print(f"Object '{object_key}' in bucket '{bucket_name}' was {record['eventName']}")

    return {
        'statusCode': 200,
        'body': json.dumps('Event processed successfully!')
    }

Linking S3 to Lambda

To connect your S3 bucket to a Lambda function, you configure the notification settings in the S3 console or via AWS CLI/SDK.

You'll select the event types (e.g., 'All object create events'), choose 'Lambda Function' as the destination, and specify your function's ARN. You can also add prefix and suffix filters here.

SQS & SNS for Events

While Lambda is great for immediate code execution, SQS and SNS offer different benefits:

  • SQS: Ideal for buffering events, decoupling components, and ensuring messages are processed even if the consumer is temporarily unavailable.
  • SNS: Perfect for fan-out scenarios, where one event needs to trigger multiple subscribers (e.g., sending an email notification AND triggering a Lambda function).

They provide flexibility for complex architectures.

Event Notification Check

Let's test your understanding of S3 Event Notifications.

S3 Events Recap

Great job! You've learned about S3 Event Notifications, a powerful feature for creating reactive, event-driven architectures.

  • S3 publishes events for actions like object creation or deletion.
  • These events can be sent to Lambda, SQS, or SNS.
  • You can use filters (prefix/suffix) to control which events trigger notifications.

This allows you to automate tasks and build dynamic applications based on changes in your S3 buckets!

คำถามที่พบบ่อย

บทเรียน “การแจ้งเตือนเหตุการณ์ของ S3” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “การแจ้งเตือนเหตุการณ์ของ S3” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส AWS for Backend Developers (EC2, S3, RDS, Lambda) ให้อัปเกรดเป็น CoddyKit PRO คอร์ส AWS for Backend Developers (EC2, S3, RDS, Lambda) มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “การแจ้งเตือนเหตุการณ์ของ S3”

กำหนดค่า S3 ให้เผยแพร่เหตุการณ์ไปยัง Lambda, SQS หรือ SNS เมื่อเกิดการกระทำที่ระบุบนออบเจ็กต์ เพื่อเปิดใช้กระบวนการทำงานแบบตอบสนองที่ทรงพลัง คุณปฏิบัติ AWS for Backend Developers (EC2, S3, RDS, Lambda) ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน AWS for Backend Developers (EC2, S3, RDS, Lambda) หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน AWS for Backend Developers (EC2, S3, RDS, Lambda) บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน

บทเรียน “การแจ้งเตือนเหตุการณ์ของ S3” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน AWS for Backend Developers (EC2, S3, RDS, Lambda) นี้ได้ไหม

ได้ บทเรียน AWS for Backend Developers (EC2, S3, RDS, Lambda) ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. การจำลองข้อมูล S3 ข้ามภูมิภาค
  2. การโฮสต์เว็บไซต์สแตติกด้วย S3
  3. การแจ้งเตือนเหตุการณ์ของ S3
  4. นโยบายวงจรชีวิต S3 และคลาสพื้นที่จัดเก็บ
← กลับไปที่ AWS for Backend Developers (EC2, S3, RDS, Lambda)