Need suggestions for bypassing the 4.5MB body size limit of Vercel Serverless Functions.
Answered
Abu Zain posted this in #help-forum
Abu ZainOP
I am working on a project with a backend in NestJS and a frontend in Next.js. I'm sending videos to the NestJS backend via server actions of Next.js, but I'm encountering a "413 Payload Too Large" error.
I found that there is a limit on the body size in the server action, and I bypassed it locally with the following configuration:
This configuration works fine locally. However, after deploying to Vercel, I discovered that there is a 4.4 MB limit on Vercel.
I am using S3 in the NestJS backend. What possible solutions can I use to handle larger video uploads in the deployed environment?
This is what i got after researching :
1):
I am considering uploading the file directly to S3. Before uploading, I can compress the video, which I am currently doing in NestJS. After the file is uploaded to S3, I plan to send a request to the NestJS backend to perform any additional processing. But this apprach includes a lot of security concerns
2):
Another good approach is using streaming functions. This means instead of using server actions, I can use API endpoints in the Next.js 14 app router. However, in this approach, I would first need to upload the video to the Next.js API, and then send that video to the NestJS backend, which is a time-consuming process.
Please let me know which apprach should i use and Do you have an better alternative apprach
I found that there is a limit on the body size in the server action, and I bypassed it locally with the following configuration:
/** @type {import('next').NextConfig} */
const nextConfig = {
experimental: {
serverActions: {
bodySizeLimit: '100mb',
},
},
images: {
remotePatterns: [
// S3 configuration
{
protocol: 'https',
hostname: 'muuve.s3.eu-west-1.amazonaws.com',
pathname: '/**',
},
{
protocol: 'http',
hostname: '54.74.143.212',
pathname: '/images/users/**',
},
],
},
};
export default nextConfig;This configuration works fine locally. However, after deploying to Vercel, I discovered that there is a 4.4 MB limit on Vercel.
I am using S3 in the NestJS backend. What possible solutions can I use to handle larger video uploads in the deployed environment?
This is what i got after researching :
1):
I am considering uploading the file directly to S3. Before uploading, I can compress the video, which I am currently doing in NestJS. After the file is uploaded to S3, I plan to send a request to the NestJS backend to perform any additional processing. But this apprach includes a lot of security concerns
2):
Another good approach is using streaming functions. This means instead of using server actions, I can use API endpoints in the Next.js 14 app router. However, in this approach, I would first need to upload the video to the Next.js API, and then send that video to the NestJS backend, which is a time-consuming process.
Please let me know which apprach should i use and Do you have an better alternative apprach
Answered by B33fb0n3
here you can see a full implementation of it and of course with code: https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/Package/-aws-sdk-s3-presigned-post/
10 Replies
@Abu Zain I am working on a project with a backend in NestJS and a frontend in Next.js. I'm sending videos to the NestJS backend via server actions of Next.js, but I'm encountering a "413 Payload Too Large" error.
I found that there is a limit on the body size in the server action, and I bypassed it locally with the following configuration:
/** @type {import('next').NextConfig} */
const nextConfig = {
experimental: {
serverActions: {
bodySizeLimit: '100mb',
},
},
images: {
remotePatterns: [
// S3 configuration
{
protocol: 'https',
hostname: 'muuve.s3.eu-west-1.amazonaws.com',
pathname: '/**',
},
{
protocol: 'http',
hostname: '54.74.143.212',
pathname: '/images/users/**',
},
],
},
};
export default nextConfig;
This configuration works fine locally. However, after deploying to Vercel, I discovered that there is a 4.4 MB limit on Vercel.
I am using S3 in the NestJS backend. What possible solutions can I use to handle larger video uploads in the deployed environment?
This is what i got after researching :
1):
I am considering uploading the file directly to S3. Before uploading, I can compress the video, which I am currently doing in NestJS. After the file is uploaded to S3, I plan to send a request to the NestJS backend to perform any additional processing. But this apprach includes a lot of security concerns
2):
Another good approach is using streaming functions. This means instead of using server actions, I can use API endpoints in the Next.js 14 app router. However, in this approach, I would first need to upload the video to the Next.js API, and then send that video to the NestJS backend, which is a time-consuming process.
Please let me know which apprach should i use and Do you have an better alternative apprach
I am using presigned urls from s3. I generate this url serverside and give the user the policy and everything he needs. While generating the link I can define what the user is allowed to do and what not. Like that I can controll the user. The user then uploads the file directly to s3 instead of my server (next serverside) uploading it to s3. Like that you wont hit the 4.5mb limit
Abu ZainOP
Can you please share the code with me? @B33fb0n3
@Abu Zain Can you please share the code with me? <@301376057326567425>
here you can see a full implementation of it and of course with code: https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/Package/-aws-sdk-s3-presigned-post/
Answer
This is a description how it works: https://aws.amazon.com/de/blogs/compute/uploading-to-amazon-s3-directly-from-a-web-or-mobile-application/
I couldn't get this code working from that page. I stick to the link that I shared before this message. That works perfectly
I couldn't get this code working from that page. I stick to the link that I shared before this message. That works perfectly
Drop in solution for file uploading to s3 with nextjs
Even if you don’t use it, the source code can teach you how this process works
linesofcode also suggested the package that I suggested. You can see a whole and very big project that is using it. To keep it only to the things, that you need, keep the following link in mind: https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/Package/-aws-sdk-s3-presigned-post/
Abu ZainOP
@linesofcode @B33fb0n3 Thanks guys
@Abu Zain <@414145877335080960> <@301376057326567425> Thanks guys
Sure thing. Please mark solution