Reputation: 539
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. '}))
})
Upvotes: 3
Views: 5315
Reputation: 31
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'
}));
Upvotes: 2
Reputation: 8122
I would like to add that Cloudinary will not accept image files over 10MB (Free plan) and won't accept video files over 100MB (Free plan). Using the upload_large
method does not solve this issue as the source image/video must be smaller than these limits in order for Cloudinary to consider it for any form of tranformation.
Upvotes: 0
Reputation: 610
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
Upvotes: 1