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 reason20 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
turns out the problem lies in AWS API Gateway. I have to add a new
multipart/form-data
entry to the Binary Media Types section. AWS documentation is the worstthat's one of the reasons i avoid AWS like the plague, lol
Google products too, like firebase. docs and DX couldn't be worse if they wanted to make devs miserable
seriously, how can such a big company produce such poor docs 🤦♂️
i've been running my hono apps on bun, using the Koyeb platform, but i haven't used it for anything production-ready yet
because they don't care. these companies don't deal treat competition as an opportunity to improve their product
they're monopolies that crush or absorb independent alternatives
their approach to gaining marketshare isn't better tools, it's just eliminating the alernatives
but since they're massive and insanely rich, it's hard to beat the price-point, so a lot of projects end up using them anyways
sadly true. It’s been mostly free using aws compared to a vps for my project so I have to use it
ah, i see. what are you using it for?
for hosting my hono app. I tried digitalocean but the pricing is too high for me at this early stage
what does your app do?
you tried koyeb?
no I haven't. It looks kinda like Vercel?
in my limited experience, digitalocean is a better comparison
its totally agnostic towards your implementation
the way i understand it, they run whatever you want on bare metal
i started using it because it made it easy to run a hono app on bun in docker (w/o using FANG servers)
so idk if they offer any off-the-shelf solutions
their managed db options are somewhat limited atm, so that can be a drawback depending on your needs
also not edge runtime. that's probably the biggest difference w vercel
but it's also more flexible/better dx imo, and may be cheaper in some cases.