【发布时间】:2017-04-29 14:03:39
【问题描述】:
我正在和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);
}
});
};
【问题讨论】:
标签: node.js amazon-web-services amazon-s3 lambda aws-lambda