lambdaの関数を次のように用意しました
console.log('Loading event');
var aws = require('aws-sdk');
var s3 = new aws.S3({apiVersion: '2006-03-01'});
var http = require('http');
exports.handler = function(event, context) {
console.log('Received event:');
console.log(JSON.stringify(event, null, ' '));
// Get the object from the event and show its content type
const bucket = event.Records[0].s3.bucket.name;
const key = event.Records[0].s3.object.key;
s3.getObject({Bucket:bucket, Key:key},
function(err,data) {
if (err) {
console.log('error getting object ' + key + ' from bucket ' + bucket +
'. Make sure they exist and your bucket is in the same region as this function.');
context.done('error','error getting file'+err);
}
else {
console.log('CONTENT TYPE:' + data.ContentType + ' bucket: ' + bucket + ' path: ' + key);
context.done(null,'');
}
}
);
var options = {
hostname: 'xxxxxxxxxxxxxxxxx',
port: xx,
path: '/' + key,
method: 'PURGE',
};
global.callback = function(response) {
var str = '';
//another chunk of data has been recieved, so append it to `str` response.on('data', function (chunk) { str += chunk; }); //the whole response has been recieved, so we just print it out here response.on('end', function () { console.log(options.hostname + ':' + options.port + options.path + ' method:' + options.method); console.log(str); });
};
http.request(options, global.callback).end();
};
上記関数のテストをした際に出たエラー
TypeError: Cannot read property '0' of undefined
at exports.handler
event.Records[0].s3.bucket.nameの[0]が無いということだと思いますが何故でしょうか。
どなたかご教授お願いいたします。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/02/08 05:21