我正在尝试从S3存储桶(.gzip)获取文件并将其解压缩到另一个存储桶。如果不将文件保存在本地(我的电脑)上,我找不到办法。有没有办法在Lambda上“保存”文件并直接在S3上解压缩?谢谢!
答案 0 :(得分:2)
这里是一个lambda代码,例如(gist):
let path = require('path');
let aws = require('aws-sdk');
let s3Client = new aws.S3();
let zlib = require('zlib');
let s3s = require('s3-streams');
const output_bucket = "stackoverflow-bucket";
exports.handler = (event, context, callback) => {
context.callbackWaitsForEmptyEventLoop = false;
event.Records.forEach(record => {
const params = {
Bucket: record.s3.bucket.name,
Key: record.s3.object.key
};
const isGzip = path.extname(params.Key) === ".gz";
let readStream = s3Client.getObject(params).createReadStream();
readStream = isGzip ? readStream.pipe(zlib.createGunzip()) : readStream;
writeStream = s3s.WriteStream(s3Client, { Bucket: output_bucket, Key: path.basename(params.Key, ".gz") });
// begins the actual streaming
readStream.pipe(writeStream);
writeStream.on('end', () => {
callback(null, `Handled ${JSON.stringify(params)}`);
});
});
};
请注意,此代码使用第三方库将字节流式传输到S3(Node.JS SDK本身不支持)。
为此,文档页面here描述了在将lambda上传到AWS之前如何打包它。
您可以设置S3事件,以便在将新文件放入源存储桶时触发lambda: