Having Trouble Uploading Photos to S3 via Lambda and API Gateway

0
31
Asked By TechNinja123 On

Hey everyone! I'm relatively new to AWS and just started integrating it with my React/NodeJS app. Here's the situation: I've set up a Lambda function to upload photos to an S3 bucket and exposed it through the API Gateway. I created the S3 bucket and ensured all the necessary permissions are in place. However, I'm facing a 500 error when trying to upload photos. I noticed that there's an OPTIONS call first, followed by a PUSH call to get the upload URL for S3, and then a PUT call to actually store the photo. It seems like the PUT call is trying to point to an undefined endpoint, leading to the 500 error. I've had some CORS issues in the past but fixed those by adding the right headers, so I'm not sure where to check next. Any suggestions?

4 Answers

Answered By CloudySkies On

Make sure to enable detailed logging in CloudWatch for more debugging info. A good practice is to have your API return presigned URLs for direct uploads to S3, which can simplify the flow.

Answered By DevGuru654 On

If you're using Lambda for the upload, check if it’s going through the API Gateway since both have limits on time and payloads. It might be better to grab a presigned URL from the frontend and upload directly to S3. You can set up an event in S3 to trigger a Lambda function after the file is uploaded for any post-processing needed.

PhotoUploader99 -

Yes, the frontend does make the request to get the upload URL before sending the photo to S3.

Answered By LambdaLover77 On

Honestly, using Lambda to handle file uploads isn’t the best approach. You should create a presigned URL, have your API return that, and let the app use it for uploading files directly to S3. Check out the AWS SDK documentation for how to set that up.

PhotoUploader99 -

Thanks, I’ll definitely check that out!

Answered By CodeWizard88 On

The OPTIONS call is just a preflight request for CORS checks from the browser. If your Lambda isn’t returning the correct CORS headers for the PUT request, S3 might block the upload even if the URL looks fine.

PhotoUploader99 -

I noticed that the POST gets the right URL for the Lambda function, but the PUT seems to go to an undefined endpoint. Also, the error isn’t even logged in the Lambda logs, making it hard to troubleshoot further.

Related Questions

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.