[英]Write a csv file to AWS S3 Fails
I have this code in TypeScript that I used to write a csv file to AWS S3, which it works fine locally, and recently I started getting and error saying:我在 TypeScript 中有这段代码,我用来将 csv 文件写入 AWS S3,它在本地运行良好,最近我开始收到错误消息:
s3 upload error unsupported body payload object
s3 上传错误不受支持的正文有效负载对象
NOTES :注意事项:
Here is the code:这是代码:
public async writeFileToS3(datasetFile: any): Promise<boolean> {
try {
const readFile = util.promisify(this.fileWriter.readFile);
const unlinkFile = util.promisify(this.fileWriter.unlink);
const s3BucketName = this.appConfig.get<string>(
'infra.fileWriter.bucket'
);
const s3Region = this.appConfig.get<string>(
'infra.fileWriter.region'
);
this.s3Bucket.config.region = s3Region;
console.log(
`datasetFile ${datasetFile.path} ${datasetFile.originalname}`
);
const data = readFile(datasetFile.path);
const params = {
Bucket: s3BucketName,
Key: datasetFile.originalname,
Body: data,
ACL: 'public-read'
};
console.log(
`params ${params.Bucket} ${params.Key} ${params.Body} ${params.ACL}`
);
return await new Promise<boolean>((resolve, reject) => {
this.s3Bucket.upload(params, function(err: any) {
unlinkFile(datasetFile.path);
if (err) {
console.log(err);
throw new OperationError(
'Error wirting file to S3',
err
);
} else {
resolve(true);
}
});
});
} catch (err) {
throw new OperationError('Error wirting file to S3');
}
}
readFile
returns a Promise (you created it with util.promisify
), thus data
is a Promise here: readFile
返回一个 Promise(您使用util.promisify
创建它),因此data
在这里是一个 Promise:
const data = readFile(datasetFile.path);
const params = {
Bucket: s3BucketName,
Key: datasetFile.originalname,
Body: data,
ACL: 'public-read'
};
You should await the Promise:你应该等待承诺:
const data = await readFile(datasetFile.path);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.