I have a problem uploading files to the S3 bucket using the @aws-sdk/client-s3 library using PutObjectCommand and @aws-sdk/lib-storage Uploading files above 70kbps more or less returns the error:
`TypeError: Failed to fetch
at FetchHttpHandler.handle (fetch-http-handler.js:56:13)
at PutObjectCommand.js:56:58
at flexibleChecksumsResponseMiddleware.js:17:26
at deserializerMiddleware.js:2:32
at check-content-length-header.js:18:16
at awsAuthMiddleware.js:10:26
at async retryMiddleware.js:24:46
at async flexibleChecksumsMiddleware.js:54:20
at async loggerMiddleware.js:3:26
at async Promise.all (index 0)
upload.ts:31`
however files below that will normally
Functions that are used to upload files
` import { PutObjectCommand, S3Client } from "@aws-sdk/client-s3";
import { Upload } from "@aws-sdk/lib-storage";
import { FetchHttpHandler } from "@smithy/fetch-http-handler";
const secretAccessKey = import.meta.env.VITE_AWS_SECRET_ACCESS_KEY;
const accessKeyId = import.meta.env.VITE_AWS_ACCESS_KEY_ID;
const region = import.meta.env.VITE_AWS_DEFAULT_REGION;
const bucket = import.meta.env.VITE_BUCKET_NAME;
const client = new S3Client({
credentials: { secretAccessKey, accessKeyId },
region,
requestHandler: new FetchHttpHandler({
requestTimeout: 5000
})
});
export const onUploadFiles = async (file: any) => {
const upload = new Upload({
client,
params: {
Bucket: bucket,
Key: file.name,
Body: file.file,
ACL: "public-read"
},
partSize: 5 * 1024 * 1024
});
try {
upload.on("httpUploadProgress", (progress) => {
console.log(progress);
});
await upload.done();
} catch (error) {
console.log(error);
}
};
export const onUpload = async (file: any, callback: (data: any) => void) => {
const input = {
Bucket: bucket,
Key: file.name,
Body: file.file,
ACL: "public-read"
};
try {
const response = await client.send(new PutObjectCommand(input));
callback(response);
} catch (error) {
console.log(error);
}
};`
3
Answers
try to upload with these params while upload object :
Bucket,Key,ACL,Body,ContentLength,ContentType
Same issue here. I figured out that there is a bug:
https://github.com/aws/aws-sdk-js-v3/pull/5319
with files less than one mega it work.
Dont figured out how to resolve yet
I had the same problem today. Attempting to upload any file (regardless of its size) using
client-s3
andPutObjectCommand
resulted into"TypeError: Failed to fetch"
message. What helped me was downgrading theaws-sdk/client-s3
package to version3.317.0
:After that, upload to S3 worked again. I hope this helps anyone facing a similar issue.