im尝试将一些文件上传到AWS s3,但是图像未完全加载到存储桶中。其中大多数都是0字节大小,有些只有kb大小,并且显示质量差或不完整...我将图像保存在项目的静态文件夹中,并且已经在不同设备上进行了测试并正确显示。>
我已经尝试过readFile()和readFileSync()了。
public async uploadFile(filename: string) {
await fs.readFile(`static/${filename}`, async (e: NodeJS.ErrnoException, file: Buffer) => {
// setup config object
const params = {
Bucket: this.bucketName, // name of the destination bucket
Key: filename, // name of the file
Body: file, // actual file
ContentType: 'image/jpeg',
ACL: 'public-read' // access control policy
};
await this.bucket.upload(params, async (err: any, data: any) => {
if (err) {
console.log('There was an error uploading your file: ', err);
return false;
}
const imageUrl = await this.bucket.getSignedUrl('getObject', { Bucket: this.bucketName, Key: filename }).split('?')[0]
console.log('Successfully uploaded file.', `${imageUrl}`);
// fs.unlink(`static/${filename}`, (error: any) => { // delete files from static folder after upload
// if (error) {
// console.error(error)
// }
// })
return true;
}).promise();
if (e) {
console.log(e)
}
});
}
预期的行为是将图像正确地托管在S3存储桶中