This article is a reminder of what I did before inserting an S3 object into an EC2 DB (SQL Server) with Lambda @ java. Please point out any other solutions.
1: AWS Edition 2: Java part [Part 1] <-Main part 3: Java [Part 2] 3.5: Java version [continued] It will be.
The following is a continuation of the previous one, after the EC2 instance was created.
Eclipse is one of the integrated development environments (IDEs) developed by IBM. It is highly functional yet open source, and supports several languages including Java. Eclipse itself is written in Java.
Note) Since it was already installed for business purposes, the version of eclipse in this article is assumed to be Oxygen.3a Release (4.7.3a). If you want to use another version, please download from https://www.eclipse.org/downloads/. Also, please customize the installation of eclipse according to the wizard.
After the installation of eclipse is completed, start it and select [File]-> [New]-> [Maven Project] to create a project.
--After selecting any workspace, click [Next]. --Select the Maven project archetype in the second wizard. --This time, there is nothing to be particular about, so select [Maven-archetype-quickstart].
Select [Next] to display a new wizard. Enter the "Artif ID" and "Group ID". The following image shows the details of the artifact ID and group ID.
Group Id: Give the project a uniquely identifiable name. Artifact Id: The packaging name of the project. An image of the name given to the jar file etc.
Leave the other items at their defaults and click Finish to create the project.
Immediately after creating the project, I think that the directory structure is as follows. Note) Change the compiler compliance level to 1.8.
Let's edit it now.
If you double-click the default pom.xml, it should look like the one below. If jUnit is not included, the description will be a little shorter.
pom.xml
<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>S3test</groupId>
<artifactId>S3toLambda</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>S3toLambda</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>
</dependencies>
</project>
Let's add a description inside the dependencies block to add AWS related libraries. Save with Ctrl + s.
After editing ↓
pom.xml
<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>S3test</groupId>
<artifactId>S3toLambda</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>S3toLambda</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>
<!-- https://mvnrepository.com/artifact/com.amazonaws/aws-lambda-java-core -->
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-lambda-java-core</artifactId>
<version>1.0.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.amazonaws/aws-lambda-java-events -->
<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>
This will add multiple AWS related jars to your Maven dependency and allow you to work with them.
Next, let's edit the App.java class that is created by default.
The default is as follows.
App.java
package S3test.S3toLambda;
/**
* Hello world!
*
*/
public class App
{
public static void main( String[] args )
{
System.out.println( "Hello World!" );
}
}
Remove unnecessary comments and name the class ReadS3Object. In addition, set the method name to listingNames.
After editing ↓
ReadS3Object.java
package S3test.S3toLambda;
import java.util.List;
import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3Client;
import com.amazonaws.services.s3.AmazonS3ClientBuilder;
import com.amazonaws.services.s3.model.ListObjectsRequest;
import com.amazonaws.services.s3.model.ObjectListing;
import com.amazonaws.services.s3.model.S3ObjectSummary;
public class ReadS3Object
{
public static void listingNames( String[] args )
{
@SuppressWarnings("deprecation")
AmazonS3 client = new AmazonS3Client(
new BasicAWSCredentials(
"<accessKey>",
"<secertKey>"));
ListObjectsRequest request = new ListObjectsRequest()
.withBucketName("<bucketName>");
ObjectListing objectList = client.listObjects(request);
//Get list of objects
List<S3ObjectSummary> objects = list.getObjectSummaries();
System.out.println("objectList:");
objects.forEach(object -> System.out.println(object.getKey()));
}
}
For \ <accessKey > and \ <secretKey >, specify the access key and secret access key of the IAM user created in AWS, respectively. For \ <bucketName >, enter the name of the S3 bucket you created.
In this state, build the project once and create a jar file.
Right-click the project name-> [Run]-> [Maven Build] to display the pop-up for editing the run configuration. Enter "package shade: shade" in the goal part to package including the dependent libraries.
When executed, the project name / target / \ <ProjectName > -0.0.1-SNAPSHOT.jar will be created in, so upload it to Lambda.
Connect to your Lambda management console. Click [Create Function] at the top of the screen to move to the creation screen.
Make sure Create from scratch is selected and fill out the central form. Here, the function name is "test-function" and the runtime is set to Java 8. Select an existing role, and select the role you created last time (the capture has changed the name from the last time).
When you execute [Create Function], the Java 8 execution environment will be created, so let's upload the jar here.
In the [Handler] of the arrow part, describe as "package name.class name :: execution method name".When the upload is complete, click Save-> Test at the top of the screen.
You will be prompted to create test data only for the first time, but this time you do not need to be aware of it, so leave the data as it is and give it an appropriate event name.
Click Create to execute the Lambda function and print the result. If you see the name of the test file you uploaded to your S3 bucket, you're good to go:
Next time, I would like to create a program that detects that some file has been uploaded to the S3 bucket and notifies it by email, and a program that inserts it into the DB.
Recommended Posts