Image uploaded to S3 cannot be displayed when run on Lambda
I'm trying to upload images to S3 via AWS Lambda using Hono, but when I open the image url it shows
The image cannot be displayed because it contains error
My Setup:
- Frontend: SvelteKit, sending a multipart/form-data request with the image.
- Backend: AWS Lambda with Hono
Here's my route
The code for the uploading part is in the comment. It works perfectly fine on my local machine but once run on Lambda, the image is corrupted for whatever reason11 Replies
ChannelService.updateChannel
S3Service
1. have you checked your S3? what's getting uploaded, or not?
2. have you confirmed that the
file.type
is being set correctly in deployment?
you might also want to try using c.parseBody
instead of c.formData
parseBody
actually works w multipart form data, and is the hono-recommended way to deal w file uploads: https://hono.dev/examples/file-upload
while formData
specifically returns a FormData
instance@ambergristle
1. the image is uploaded successfully but when I view it it says
The image cannot be displayed because contains error
2. yes it's correct. Here's what I see in CloudWatch
3. I tried c.parseBody
but still no luckwhat's the logged content type of the files you're trying to upload?
what do you mean? the type is jpeg
ah, i see, you shared already:
'image/jpeg'
huh
do all the filenames have extensions?yes they do. But the final url still looks correct
https://bucketname.s3.region.amazonaws.com/644cfd89-38b7-4dfb-8100-e49c106f8fe7.jpg
sorry, it's been a minute since i used aws. i tried to avoid it
i feel like in the past, i've needed to generate signed s3 urls. the s3 client has a method called
getSignedUrl
. maybe that would help?I have no idea 😅 I’ve tried Google, Gpt but nothing works. I guess I have to use something else
oh, i meant to link this: https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/S3.html#getSignedUrl-property
i'd never discourage someone from ditching AWS, lol, but i hope signing helps
Thanks! I will try this one