質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
AWS(Amazon Web Services)

Amazon Web Services (AWS)は、仮想空間を機軸とした、クラスター状のコンピュータ・ネットワーク・データベース・ストーレッジ・サポートツールをAWSというインフラから提供する商用サービスです。

Q&A

解決済

1回答

893閲覧

aws s3の更新を即時反映したい

kishiuser0

総合スコア11

AWS(Amazon Web Services)

Amazon Web Services (AWS)は、仮想空間を機軸とした、クラスター状のコンピュータ・ネットワーク・データベース・ストーレッジ・サポートツールをAWSというインフラから提供する商用サービスです。

0グッド

0クリップ

投稿2018/02/08 03:52

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]が無いということだと思いますが何故でしょうか。

どなたかご教授お願いいたします。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

[0]が無いということなので、Recordsの配列の0番目がない=Recordsというのは配列ではないということになると思います。

そもそも、eventの中身がどうなっているのかconsoleに出力してみてはいかがでしょう。

投稿2018/02/08 04:16

yatta47

総合スコア208

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

kishiuser0

2018/02/08 05:21

lambdaをテストする際に、テスト用のeventを手動で作成するのですが、その作成したものが正しく 無く配列ではありませんでした。 有難うございました。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問