简体   繁体   English

将 csv 文件写入 AWS S3 失败

[英]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 :注意事项

  • I'm not passing the credentials because the code is running in the same container with AWS S3 (EC2) that's why I don't need to pass the credentials.我没有传递凭据,因为代码在与 AWS S3 (EC2) 相同的容器中运行,这就是我不需要传递凭据的原因。
  • I'm printing all the params I'm reading/passing and I have them read properly.我正在打印我正在阅读/传递的所有参数,并且我已经正确阅读了它们。

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM