我正在尝试使用 archiver-zip-encrypted node.js模块将文件压缩两次。当我尝试本地计算机时,文件可以使用加密的AES256中的密码压缩两次。但是,当我在Lambda中尝试相同的功能时,它在本地的表现却不一样。我尝试同时附加两个文件,但是我注意到第一个zip文件中包含实际文件,但是在创建第二个zip文件时遇到了问题,因为其中包含空白文件。
var aws = require('aws-sdk');
var nodemailer = require('nodemailer');
var ses = new aws.SES();
const archiver = require('archiver');
const stream = require('stream')
var fs = require('fs');
var async = require('async');
exports.handler = function (event, context, callback) {
const filepath = event.filepath;
const filename = event.file;
const email = event.email;
const bucket = "BUCKETNAME";
const filepathZip = '/tmp/'+filename +'.zip';
const filenameZip = filename +'.zip';
var s3 = new aws.S3();
var options = {
Bucket:bucket,
Key: 'files/' + filename
};
var fileStream = s3.getObject(options).createReadStream();
archiver.registerFormat('zip-encrypted', require("archiver-zip-encrypted"));
var output = fs.createWriteStream(filepathZip);
var randampassword = Math.random().toString(36).substring(2, 15) + Math.random().toString(36).substring(2, 15);
let archive = archiver.create('zip-encrypted', {zlib: {level: 8}, encryptionMethod: 'aes256', password: randampassword });
output.on('close', function() {
console.log(archive.pointer() + ' total bytes ');
console.log('archiver has been finalized and the output file descriptor has closed.');
console.log(randampassword);
});
archive.on('err', function(err) {
throw err;
});
var FinalZip = '/tmp/'+filename +'_final.zip';
let archive_final = archiver.create('zip-encrypted', {zlib: {level: 8}, encryptionMethod: 'aes256', password: randampassword });
var output_final = fs.createWriteStream(FinalZip)
async.waterfall([
function(callback){
archive.pipe(output);
archive.append(fileStream, { name: filename }).finalize();
archive_final.pipe(output_final);
archive_final
.file(filepathZip, { name: filenameZip })
.finalize();
}
]);
getS3File('BUCKETNAME', 'test.txt')
.then(function (fileData) {
var mailOptions = {
from: 'SENDER',
subject: 'This is an email sent from a Lambda function!',
html: `<p>You got a contact message from: <b>${event.email}</b> paasword: <b>${randampassword}</b></p>`,
to: 'RECEIVER',
// bcc: Any BCC address you want here in an array,
attachments: [
{
filename: "attach.zip",
path: filepathZip
}
]
};
console.log('Creating SES transporter');
var transporter = nodemailer.createTransport({
SES: ses
});
transporter.sendMail(mailOptions, function (err, info) {
if (err) {
console.log(err);
console.log('Error sending email');
callback(err);
} else {
console.log('Email sent successfully');
callback();
}
});
})
.catch(function (error) {
console.log(error);
console.log('Error getting attachment from S3');
callback(error);
});
};