我与AWS Lambda
玩Node.js
。我创建了一个lambda函数并使用S3 event
对其进行配置。
我想提取在S3上传的zip文件,并将提取的文件上传到同一个存储桶上的另一个文件夹。
我从以下代码获取存储桶和文件信息,但之后我不知道如何提取并上传到s3。
任何建议或一大堆代码对我都有帮助。
'use strict';
console.log('Loading function to get all latest object from S3 service');
const aws = require('aws-sdk');
const s3 = new aws.S3({ apiVersion: '2006-03-01' });
exports.handler = (event, context, callback) => {
console.log('Received event:', JSON.stringify(event, null, 2));
// Get the object from the event and show its content type
const bucket = event.Records[0].s3.bucket.name;
const key = decodeURIComponent(event.Records[0].s3.object.key.replace(/\+/g, ' '));
const params = {
Bucket: bucket,
Key: key,
};
s3.getObject(params, (err, data) => {
if (err) {
console.log(err);
const message = `Error getting object ${key} from bucket ${bucket}. Make sure they exist and your bucket is in the same region as this function.`;
console.log(message);
callback(message);
} else {
console.log('CONTENT TYPE:', data.ContentType);
callback(null, data);
}
});
};
答案 0 :(得分:1)
您可以使用zlib解压缩从s3获得的缓冲区。
s3.getObject(params, (err, data) => {
if (err) {
console.log(err);
const message = `Error getting object ${key} from bucket ${bucket}. Make sure they exist and your bucket is in the same region as this function.`;
console.log(message);
callback(message);
} else {
zlib.gunzip(data.Body, function (err, result) {
if (err) {
console.log(err);
} else {
var extractedData = result;
s3.putObject({
Bucket: "bucketName",
Key: "filename",
Body: extractedData,
ContentType: 'content-type'
}, function (err) {
console.log('uploaded file: ' + err);
});
}
});
}
});
我认为上述功能对您有帮助。