我在使用@AWS-SDK/CLIENT-S3库使用PutObjectCommand将文件上载到S3存储桶时遇到问题,而@AWS-SDK/lib-store上载超过70kbps的文件或多或少会返回错误:
`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`
但是,下面的文件通常会
用于上载文件的函数
`从"@AWS-SDK/Client-S3"导入{PutObjectCommand,S3Client}; 从"@aws-sdk/lib-store"导入{Upload}; 从"@smithy/Fetch-http-Handler"导入{FetchHttpHandler};
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);
}
};`