使用 AWS SDK 版本 3,如何从基于 nodejs 的 lambda 读取 S3 存储桶文件?我正在创建这个以供我将来参考。
问问题
14 次
1 回答
0
有几个解决方案,我展示的解决方案是使用node-fetch npm。
import Log from '@dazn/lambda-powertools-logger'; // it will be modified after integrating with lambda-powertools
import { S3Event } from 'aws-lambda';
import { S3Client, GetObjectCommand } from '@aws-sdk/client-s3';
import middy from '@middy/core';
import csv from 'csvtojson';
import { Response } from 'node-fetch';
import internal from 'stream';
import { incomingEventLogger, onErrorHandler } from '../helpers/middleware';
const { BUCKET: Bucket, REGION } = process.env;
const client = new S3Client( { region: REGION } );
export const index = async (event: S3Event) => {
const s3GetObjectInput = {
Bucket,
Key: 'PrivateCSVFiles/test.csv', // hardcoded for the sake of example
};
const getObjectCommand = new GetObjectCommand(s3GetObjectInput);
const { Body } = await client.send(getObjectCommand);
const csvFileContentResponse = new Response(Body as internal.Readable);
const csvFileContent = await csvFileContentResponse.text();
Log.debug('csvFileContent', { csvFileContent });
const jsonObj = await csv({ delimiter: ','})
.fromString(csvFileContent);
Log.debug('jsonObj', { jsonObj });
};
export const handler = middy(index);
handler.before(incomingEventLogger);
handler.onError(onErrorHandler);
于 2022-03-04T17:29:18.030 回答