It is possible to limit the file size when uploading using signed URL? #4743
-
I have this code in my back end
And in the front end, I'm validating the file size, but if I remove the client file validation, I can upload any file size to my bucket. For example, I want that the maximum size to be 8MB, if the user tries to upload a file greater than 8MB, AWS will throw an error in the PUT request. |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments
-
Hi @jvitormelo, you can include the ContentLength parameter when generating the signed url as follow: import {PutObjectCommand, S3Client} from "@aws-sdk/client-s3";
import {getSignedUrl} from "@aws-sdk/s3-request-presigner";
const client = new S3Client({
region: 'us-east-2'
});
const body = "This is a test body";
const command = new PutObjectCommand({
Bucket: process.env.TEST_BUCKET,
Key: process.env.TEST_KEY,
ContentLength: body.length
});
const signedUrl = await getSignedUrl(client, command);
fetch(signedUrl, {
method: 'PUT',
body: body
}).then(async (response) => {
console.log('Status: ', response.status)
console.log('StatusText: ', response.statusText)
console.log('StatusText: ', await response.text())
}).catch((err) => {
console.log('Error: ', err)
}); The only thing about this is that the size of the body when executing the request must match what you specified in that parameter. For example, if you set ContentLength to 5 so then the body needs to be exact 5 characters or bytes. I hope this helps! Thanks! |
Beta Was this translation helpful? Give feedback.
-
Is there a way to speicfy the content length range link in createPresignedPost
|
Beta Was this translation helpful? Give feedback.
-
Hello! Reopening this discussion to make it searchable. |
Beta Was this translation helpful? Give feedback.
Hi @jvitormelo, you can include the ContentLength parameter when generating the signed url as follow:
Note: The value for ContentLength is evaluated in bytes.