添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
25
24

More than 5 years have passed since last update.

AWS LambdaのファンクションをJavaで書く(S3イベント処理編)

Posted at

前回に引き続き、AWS LambdaがJava対応したのでその簡単なチュートリアルをしてみます。
前回はすごく簡単なサンプルでしたので今回はS3のイベントを受け取って処理を行うファンクションをJavaで実装してみます。
前回同様、作業環境はMac OSX YosemiteでIDEはEclipseベースのSTS(Spring Tool Suite)を利用しています。あと、mavenは導入済みの前提です。

Lambdaファンクションを書く

今回はS3上の特定バケット上にオブジェクト(ファイルとか)がアップロードされたらそのイベントをトリガーにアップロードされたファイル名を出力するだけのLambdaファンクションを用意します。
まず、前回は使わなかったaws-lambda-java-eventsを使うのでpom.xmlに追加してください。

<dependency>
    <groupId>com.amazonaws</groupId>
    <artifactId>aws-lambda-java-events</artifactId>
    <version>1.0.0</version>
</dependency>

今回はこのライブラリに含まれるS3Eventという事前定義されたクラスを利用してハンドラメソッドを実装します。自分自身でPOJOオブジェクト作って頑張ってもいいのですがサポートされているイベントソースのものであれば事前定義済みのものがあるので特別な要件が無い限りこれを使うのが楽でいいと思います。

package com.sample.lambda;
import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.LambdaLogger;
import com.amazonaws.services.lambda.runtime.events.S3Event;
import com.amazonaws.services.s3.event.S3EventNotification.S3EventNotificationRecord;
public class S3EventHandler {
    public String handler(S3Event event, Context context){
        LambdaLogger lambdaLogger = context.getLogger();
        S3EventNotificationRecord record = event.getRecords().get(0);
        lambdaLogger.log(record.getEventName()); //イベント名
        lambdaLogger.log(record.getS3().getBucket().getName()); //バケット名
        lambdaLogger.log(record.getS3().getObject().getKey()); //オブジェクトのキー(オブジェクト名)
        return record.getS3().getObject().getKey();

S3EventオブジェクトからgetRecords()でS3EventNotificationRecordのListを取得するのですが今回は1件しか入っていないので最初の要素を決め打ちで取得しています。
S3EventNotificationRecordオブジェクトからはイベントの各種情報が取得できるのですが、getS3()でS3のエンティティを示すS3Entityオブジェクトを取得できます。このオブジェクトからはイベントの発生元となったバケットの情報やオブジェクトの情報が取得できるので基本的にはこれを利用していく形になります。今回はオブジェクトのキー名を取得して出力および返却しています。

パッケージング

前回同様、以下のようなpom.xmlを用意しパッケージングします。ポイントとしては作成されるJARファイルに必要となるライブラリ群も含まれているということです。興味がある方は作成されたJARファイルを解凍して確認してみてください。

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.sample.lambda</groupId> <artifactId>com.sample.lambda</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>lambda-java</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <dependency> <groupId>com.amazonaws</groupId> <artifactId>aws-lambda-java-core</artifactId> <version>1.0.0</version> </dependency> <dependency> <groupId>com.amazonaws</groupId> <artifactId>aws-lambda-java-events</artifactId> <version>1.0.0</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-shade-plugin</artifactId> <version>2.3</version> <configuration> <createDependencyReducedPom>false</createDependencyReducedPom> </configuration> <executions> <execution> <phase>package</phase> <goals> <goal>shade</goal> </goals> </execution> </executions> </plugin> </plugins> </build> </project>

こんな感じでLambdaがサポートしているイベントソースのイベントであれば簡単に処理ができます。今回はS3でしたがKinesisの場合はKinesisEventというものが用意されていて同様に処理できます(ただし、Kinesisの場合はRecordが複数になるのでKinesisEventRecordのListを処理することになります)。
次回はPOJOを用意してカスタムイベントを実装する例をご紹介したいと思います。

25
24
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
25
24