I'm using cloudinary for image upload and storage. When I try to upload a simple JPG file, it gives me the following error:
"PayloadTooLargeError: request entity too large"
{
message: 'request entity too large',
expected: 1127957,
length: 1127957,
limit: 102400,
type: 'entity.too.large'
}
following is my backend code:
cloudinary.config({
cloud_name: process.env.CLOUD_NAME,
api_key: process.env.CLOUD_API_KEY,
api_secret: process.env.CLOUD_API_SECRET
})
exports.uploadImage = catchAsync(async(req, res, next) => {
cloudinary.uploader.upload(req.body.img)
.then(async res => {
await User.updateOne({
_id: req.user._id
},{
$push: {
images: {
imgId: res.public_id,
imgVersion: res.version
}
}
})
})
.then(() => res.status(200).json({ msg: 'image uploaded. '}))
})
What I discovered when I got this error was that it was not coming from Cloudinary but rather from Express, which blocks request bodies of a certain size unless this limit is expressly set higher. You should (provided you are using Express), be able to fix it this way:
app.use(express.json({
limit: '50mb'
}));
Looks like the request body is larger than 100 MB which is Cloudinary's limit for upload requests. Any request that is larger than this limit would receive the error you're seeing. To upload files larger than 100MB you'll have to send the request in chunks (see docs here).
Instead of using the upload method, you should use the upload_large one as it splits the file and uploads it in parts automatically for you.
See - https://github.com/cloudinary/cloudinary_npm/blob/4b0bbccc9bc7c9340b59536e7c73e57c55da2e6f/lib/uploader.js#L54
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With