0Pricing
Serverless AWS Lambda Development · 课时

使用 S3 存储文件与处理事件

深入了解如何使用 Amazon S3 存储和读取大型对象,以及如何利用 S3 事件触发 Lambda 进行处理

使用 S3 存储文件与处理事件 是 CoddyKit 上的免费 Serverless AWS Lambda Development 课时。 这是第 2 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 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 .jpg files).

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:

  1. User uploads a high-res image to an S3 bucket.
  2. S3 triggers a Lambda function (e.g., ObjectCreated:Put event).
  3. Lambda downloads the image, resizes it to various thumbnails.
  4. 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 导师)并解锁 Serverless AWS Lambda Development 课程的其余内容,请升级到 CoddyKit PRO。 Serverless AWS Lambda Development 课程共包含 4 节课。

「使用 S3 存储文件与处理事件」这节课中我会学到什么?

深入了解如何使用 Amazon S3 存储和读取大型对象,以及如何利用 S3 事件触发 Lambda 进行处理 你通过在浏览器中直接运行的动手代码来练习 Serverless AWS Lambda Development,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 Serverless AWS Lambda Development 需要有经验吗?

无需任何先前经验。CoddyKit 上的 Serverless AWS Lambda Development 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 2 节课,共 4 节。

「使用 S3 存储文件与处理事件」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 Serverless AWS Lambda Development 课中编写并运行代码吗?

能。每节 Serverless AWS Lambda Development 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. 与 DynamoDB 集成
  2. 使用 S3 存储文件与处理事件
  3. 选择合适的数据存储
  4. 使用 Amazon ElastiCache 和 DAX 进行缓存
← 返回 Serverless AWS Lambda Development