You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

S3通过Laravel上传文件时Lambda函数未触发问题咨询

Troubleshooting Lambda Not Triggering on Laravel S3 Uploads

Hey there! Let's figure out why your Lambda function isn't firing when uploading files via your Laravel app, even though it works perfectly when testing through the AWS console. Here are the key areas to check step by step:

1. Verify S3 Event Trigger Configuration

First, double-check your S3 bucket's event trigger setup—this is the most common culprit:

  • Event Type Mismatch: Laravel might use different upload methods than your console test. Make sure your trigger includes all relevant ObjectCreated events:
    • If Laravel uses standard uploads: Include s3:ObjectCreated:Put
    • If Laravel handles large files with multipart uploads: Add s3:ObjectCreated:MultipartUploadCompleted
    • If you're copying files between buckets: Don't forget s3:ObjectCreated:Copy
  • Prefix/Postfix Filters: Check if you set a prefix (e.g., images/) or suffix (e.g., .jpg) on the trigger. If your Laravel app uploads files to a different path (like uploads/) or uses unfiltered file types, the trigger won't activate.
  • Correct Lambda Association: Confirm the trigger is linked to the exact Lambda function you tested—sometimes it's easy to accidentally select the wrong function in the console.

2. Check Laravel's S3 Upload Behavior

How your Laravel app interacts with S3 might be bypassing the trigger:

  • Multipart Uploads: Laravel's Storage::putFile() or put() methods automatically switch to multipart uploads for large files. If your trigger only listens for Put events, it won't fire after a multipart upload completes. Update the trigger to include MultipartUploadCompleted.
  • Bucket Misconfiguration: Ensure your Laravel .env file points to the correct S3 bucket (check AWS_BUCKET). It's easy to upload to a staging bucket while your trigger is on the production one.
  • Copy Operations: If your app first uploads to a temporary bucket then copies to the target bucket, the copy action requires the s3:ObjectCreated:Copy event in your trigger.

3. Validate Permissions & Resource Policies

Even if your Lambda works in tests, missing permissions can block S3 from invoking it:

  • Lambda Resource Policy: S3 needs explicit permission to call your Lambda function. Go to your Lambda's Configuration > Permissions > Resource-based policy statements and verify there's a rule allowing S3:
    {
      "Version": "2012-10-17",
      "Id": "S3TriggerPolicy",
      "Statement": [
        {
          "Sid": "AllowS3Invoke",
          "Effect": "Allow",
          "Principal": {
            "Service": "s3.amazonaws.com"
          },
          "Action": "lambda:InvokeFunction",
          "Resource": "arn:aws:lambda:YOUR_REGION:YOUR_ACCOUNT_ID:function:YOUR_FUNCTION_NAME",
          "Condition": {
            "ArnLike": {
              "AWS:SourceArn": "arn:aws:s3:::YOUR_BUCKET_NAME"
            }
          }
        }
      ]
    }
    
    If this policy is missing, add it via the Lambda console or AWS CLI.
  • Lambda Execution Role: Confirm the role has permissions to read the source S3 object (s3:GetObject) and write the resized file back to the bucket (s3:PutObject). Your console test works, so this is less likely, but it's worth a quick check.

4. Dig Into Logs for Clues

Logs will tell you if S3 is even trying to invoke Lambda:

  • Lambda CloudWatch Logs: Go to your Lambda's Monitor > Logs and check for any invocation attempts. If there are no logs at all, S3 isn't triggering the function. If there are error logs, debug those (but your console test works, so this is probably not the case).
  • S3 Access Logs: Enable S3 access logs for your bucket to confirm the upload request from Laravel is reaching the bucket, and that S3 attempted to send an event to Lambda.

5. Rule Out Cache or Delays

Sometimes it's just a timing issue:

  • Trigger Propagation: S3 event triggers can take a few minutes to fully propagate. Wait 5-10 minutes after updating the trigger and test again.
  • Laravel Cache: If you're using a cached S3 client (e.g., via Laravel's cache adapter), clear your app cache with php artisan cache:clear and try uploading again.

Start with the trigger configuration and Laravel upload behavior—those are the fastest fixes. If you still hit walls, share details about your trigger setup and Laravel upload code, and we can dig deeper!

内容的提问来源于stack exchange,提问作者Wai Yan Hein

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 07:29:10