将csv文件写入AWS S3失败

问题描述 投票:0回答:1

我在TypeScript中有此代码,该代码曾用于向AWS S3写一个csv文件,该文件在本地运行良好,最近我开始出现错误提示:

s3上传错误,不支持的主体有效载荷对象

NOTES

  • 我没有传递凭据,因为代码正在与AWS S3(EC2)相同的容器,这就是为什么我不需要通过证书。
  • 我正在打印我正在阅读/传递的所有参数,并且已经有了它们正确阅读。

这里是代码:

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');
        }
    }
javascript node.js typescript amazon-web-services amazon-s3
1个回答
0
投票
[readFile返回一个Promise(您使用util.promisify创建它),因此data在这里是一个Promise:

const data = readFile(datasetFile.path); const params = { Bucket: s3BucketName, Key: datasetFile.originalname, Body: data, ACL: 'public-read' };

© www.soinside.com 2019 - 2024. All rights reserved.