使用Lambda节点从S3上的文件创buildS3上的zip文件
我需要创build一个Zip文件,其中包含位于我的s3存储桶中的一系列文件(video和图像)。
目前使用我的代码下面的问题是,我很快达到了Lambda的内存限制。
async.eachLimit(files, 10, function(file, next) { var params = { Bucket: bucket, // bucket name Key: file.key }; s3.getObject(params, function(err, data) { if (err) { console.log('file', file.key); console.log('get image files err',err, err.stack); // an error occurred } else { console.log('file', file.key); zip.file(file.key, data.Body); next(); } }); }, function(err) { if (err) { console.log('err', err); } else { console.log('zip', zip); content = zip.generateNodeStream({ type: 'nodebuffer', streamFiles:true }); var params = { Bucket: bucket, // name of dest bucket Key: 'zipped/images.zip', Body: content }; s3.upload(params, function(err, data) { if (err) { console.log('upload zip to s3 err',err, err.stack); // an error occurred } else { console.log(data); // successful response } }); } });
-
这可能使用Lambda,或者我应该看看不同的方法?
-
是否有可能在运行中写入压缩的zip文件,从而消除内存问题,还是需要在压缩之前收集文件?
任何帮助将非常感激。
使用stream可能会非常棘手,因为我不确定如何将多个stream传输到对象中。 我已经使用标准文件对象多次这样做了。 这是一个多步骤的过程,速度很快。 请记住,Lambda在Linux中运行,因此您拥有所有Linux资源,包括system / tmp目录。
- 在/ tmp中调用“transient”或者任何适合你的东西来创build一个子目录
- 使用s3.getObject()并将文件对象写入/ tmp / transient
- 使用GLOB包来从/ tmp / transient生成一个数组[]
- 循环数组和zip.addLocalFile(array [i]);
- zip.writeZip( 'TMP / files.zip');