JAVA-10628: Create new aws-modules and move other aws related modules
inside it
This commit is contained in:
@@ -0,0 +1,3 @@
|
||||
### Relevant Articles:
|
||||
|
||||
- [AWS AppSync With Spring Boot](https://www.baeldung.com/aws-appsync-spring)
|
||||
@@ -0,0 +1,48 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>aws-app-sync</artifactId>
|
||||
<name>aws-app-sync</name>
|
||||
<description>Spring Boot using AWS App Sync</description>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-boot-2</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../../parent-boot-2</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
<exclusions>
|
||||
<exclusion>
|
||||
<groupId>org.junit.vintage</groupId>
|
||||
<artifactId>junit-vintage-engine</artifactId>
|
||||
</exclusion>
|
||||
</exclusions>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-webflux</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
</project>
|
||||
+32
@@ -0,0 +1,32 @@
|
||||
package com.baeldung.awsappsync;
|
||||
|
||||
import org.springframework.http.HttpMethod;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.web.reactive.function.BodyInserters;
|
||||
import org.springframework.web.reactive.function.client.WebClient;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Map;
|
||||
|
||||
public class AppSyncClientHelper {
|
||||
|
||||
static String apiUrl = "<INSERT API URL HERE>";
|
||||
static String apiKey = "<INSERT API KEY HERE>";
|
||||
static String API_KEY_HEADER = "x-api-key";
|
||||
|
||||
public static WebClient.ResponseSpec getResponseBodySpec(Map<String, Object> requestBody) {
|
||||
return WebClient
|
||||
.builder()
|
||||
.baseUrl(apiUrl)
|
||||
.defaultHeader(API_KEY_HEADER, apiKey)
|
||||
.defaultHeader("Content-Type", "application/json")
|
||||
.build()
|
||||
.method(HttpMethod.POST)
|
||||
.uri("/graphql")
|
||||
.body(BodyInserters.fromValue(requestBody))
|
||||
.accept(MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML)
|
||||
.acceptCharset(StandardCharsets.UTF_8)
|
||||
.retrieve();
|
||||
}
|
||||
|
||||
}
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
package com.baeldung.awsappsync;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class AwsAppSyncApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(AwsAppSyncApplication.class, args);
|
||||
}
|
||||
|
||||
}
|
||||
+74
@@ -0,0 +1,74 @@
|
||||
package com.baeldung.awsappsync;
|
||||
|
||||
import org.junit.jupiter.api.Disabled;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.web.reactive.function.client.WebClient;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
@SpringBootTest
|
||||
@Disabled
|
||||
class AwsAppSyncApplicationUnitTest {
|
||||
|
||||
@Test
|
||||
void givenGraphQuery_whenListEvents_thenReturnAllEvents() {
|
||||
|
||||
Map<String, Object> requestBody = new HashMap<>();
|
||||
requestBody.put("query", "query ListEvents {"
|
||||
+ " listEvents {"
|
||||
+ " items {"
|
||||
+ " id"
|
||||
+ " name"
|
||||
+ " where"
|
||||
+ " when"
|
||||
+ " description"
|
||||
+ " }"
|
||||
+ " }"
|
||||
+ "}");
|
||||
requestBody.put("variables", "");
|
||||
requestBody.put("operationName", "ListEvents");
|
||||
|
||||
String bodyString = AppSyncClientHelper.getResponseBodySpec(requestBody)
|
||||
.bodyToMono(String.class).block();
|
||||
|
||||
assertNotNull(bodyString);
|
||||
assertTrue(bodyString.contains("My First Event"));
|
||||
assertTrue(bodyString.contains("where"));
|
||||
assertTrue(bodyString.contains("when"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenGraphAdd_whenMutation_thenReturnIdNameDesc() {
|
||||
|
||||
String queryString = "mutation add {"
|
||||
+ " createEvent("
|
||||
+ " name:\"My added GraphQL event\""
|
||||
+ " where:\"Day 2\""
|
||||
+ " when:\"Saturday night\""
|
||||
+ " description:\"Studying GraphQL\""
|
||||
+ " ){"
|
||||
+ " id"
|
||||
+ " name"
|
||||
+ " description"
|
||||
+ " }"
|
||||
+ "}";
|
||||
|
||||
Map<String, Object> requestBody = new HashMap<>();
|
||||
requestBody.put("query", queryString);
|
||||
requestBody.put("variables", "");
|
||||
requestBody.put("operationName", "add");
|
||||
|
||||
WebClient.ResponseSpec response = AppSyncClientHelper.getResponseBodySpec(requestBody);
|
||||
|
||||
String bodyString = response.bodyToMono(String.class).block();
|
||||
|
||||
assertNotNull(bodyString);
|
||||
assertTrue(bodyString.contains("My added GraphQL event"));
|
||||
assertFalse(bodyString.contains("where"));
|
||||
assertFalse(bodyString.contains("when"));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
.aws-sam/
|
||||
@@ -0,0 +1,11 @@
|
||||
## AWS Lambda
|
||||
|
||||
This module contains articles about AWS Lambda
|
||||
|
||||
### Relevant Articles:
|
||||
- [A Basic AWS Lambda Example With Java](https://www.baeldung.com/java-aws-lambda)
|
||||
- [Using AWS Lambda with API Gateway](https://www.baeldung.com/aws-lambda-api-gateway)
|
||||
- [Introduction to AWS Serverless Application Model](https://www.baeldung.com/aws-serverless)
|
||||
- [How to Implement Hibernate in an AWS Lambda Function in Java](https://www.baeldung.com/java-aws-lambda-hibernate)
|
||||
- [Writing an Enterprise-Grade AWS Lambda in Java](https://www.baeldung.com/java-enterprise-aws-lambda)
|
||||
- [AWS Lambda Using DynamoDB With Java](https://www.baeldung.com/aws-lambda-dynamodb-java)
|
||||
@@ -0,0 +1,105 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<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>
|
||||
<artifactId>lambda</artifactId>
|
||||
<version>0.1.0-SNAPSHOT</version>
|
||||
<name>lambda</name>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>aws-lambda</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.amazonaws</groupId>
|
||||
<artifactId>aws-java-sdk-dynamodb</artifactId>
|
||||
<version>${aws-java-sdk.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.amazonaws</groupId>
|
||||
<artifactId>aws-java-sdk-core</artifactId>
|
||||
<version>${aws-java-sdk.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.amazonaws</groupId>
|
||||
<artifactId>aws-lambda-java-core</artifactId>
|
||||
<version>${aws-lambda-java-core.version}</version>
|
||||
<exclusions>
|
||||
<exclusion>
|
||||
<artifactId>commons-logging</artifactId>
|
||||
<groupId>commons-logging</groupId>
|
||||
</exclusion>
|
||||
</exclusions>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.amazonaws</groupId>
|
||||
<artifactId>aws-lambda-java-events</artifactId>
|
||||
<version>${aws-lambda-java-events.version}</version>
|
||||
<exclusions>
|
||||
<exclusion>
|
||||
<artifactId>commons-logging</artifactId>
|
||||
<groupId>commons-logging</groupId>
|
||||
</exclusion>
|
||||
</exclusions>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.google.code.gson</groupId>
|
||||
<artifactId>gson</artifactId>
|
||||
<version>${gson.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>commons-io</groupId>
|
||||
<artifactId>commons-io</artifactId>
|
||||
<version>${commons-io.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.googlecode.json-simple</groupId>
|
||||
<artifactId>json-simple</artifactId>
|
||||
<version>${json-simple.version}</version>
|
||||
<exclusions>
|
||||
<!-- junit4 dependency is excluded as it should to be resolved from junit-vintage-engine
|
||||
included in parent-modules. -->
|
||||
<exclusion>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
</exclusion>
|
||||
</exclusions>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-shade-plugin</artifactId>
|
||||
<version>${maven-shade-plugin.version}</version>
|
||||
<configuration>
|
||||
<createDependencyReducedPom>false</createDependencyReducedPom>
|
||||
</configuration>
|
||||
<executions>
|
||||
<execution>
|
||||
<phase>package</phase>
|
||||
<goals>
|
||||
<goal>shade</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
<properties>
|
||||
<json-simple.version>1.1.1</json-simple.version>
|
||||
<aws-lambda-java-events.version>1.3.0</aws-lambda-java-events.version>
|
||||
<aws-lambda-java-core.version>1.2.0</aws-lambda-java-core.version>
|
||||
<gson.version>2.8.2</gson.version>
|
||||
<aws-java-sdk.version>1.11.241</aws-java-sdk.version>
|
||||
<maven-shade-plugin.version>3.0.0</maven-shade-plugin.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
@@ -0,0 +1,60 @@
|
||||
AWSTemplateFormatVersion: '2010-09-09'
|
||||
Transform: 'AWS::Serverless-2016-10-31'
|
||||
Description: Baeldung Serverless Application Model Example with Implicit API Definition
|
||||
Globals:
|
||||
Api:
|
||||
EndpointConfiguration: REGIONAL
|
||||
Name: "TestAPI"
|
||||
Resources:
|
||||
PersonTable:
|
||||
Type: AWS::Serverless::SimpleTable
|
||||
Properties:
|
||||
PrimaryKey:
|
||||
Name: id
|
||||
Type: Number
|
||||
TableName: Person
|
||||
StorePersonFunction:
|
||||
Type: AWS::Serverless::Function
|
||||
Properties:
|
||||
Handler: com.baeldung.lambda.apigateway.APIDemoHandler::handleRequest
|
||||
Runtime: java8
|
||||
Timeout: 15
|
||||
MemorySize: 512
|
||||
CodeUri: ../target/aws-lambda-0.1.0-SNAPSHOT.jar
|
||||
Policies:
|
||||
- DynamoDBCrudPolicy:
|
||||
TableName: !Ref PersonTable
|
||||
Environment:
|
||||
Variables:
|
||||
TABLE_NAME: !Ref PersonTable
|
||||
Events:
|
||||
StoreApi:
|
||||
Type: Api
|
||||
Properties:
|
||||
Path: /persons
|
||||
Method: PUT
|
||||
GetPersonByHTTPParamFunction:
|
||||
Type: AWS::Serverless::Function
|
||||
Properties:
|
||||
Handler: com.baeldung.lambda.apigateway.APIDemoHandler::handleGetByParam
|
||||
Runtime: java8
|
||||
Timeout: 15
|
||||
MemorySize: 512
|
||||
CodeUri: ../target/aws-lambda-0.1.0-SNAPSHOT.jar
|
||||
Policies:
|
||||
- DynamoDBReadPolicy:
|
||||
TableName: !Ref PersonTable
|
||||
Environment:
|
||||
Variables:
|
||||
TABLE_NAME: !Ref PersonTable
|
||||
Events:
|
||||
GetByPathApi:
|
||||
Type: Api
|
||||
Properties:
|
||||
Path: /persons/{id}
|
||||
Method: GET
|
||||
GetByQueryApi:
|
||||
Type: Api
|
||||
Properties:
|
||||
Path: /persons
|
||||
Method: GET
|
||||
@@ -0,0 +1,112 @@
|
||||
AWSTemplateFormatVersion: '2010-09-09'
|
||||
Transform: 'AWS::Serverless-2016-10-31'
|
||||
Description: Baeldung Serverless Application Model Example with Inline Swagger API Definition
|
||||
Resources:
|
||||
PersonTable:
|
||||
Type: AWS::Serverless::SimpleTable
|
||||
Properties:
|
||||
PrimaryKey:
|
||||
Name: id
|
||||
Type: Number
|
||||
TableName: Person
|
||||
StorePersonFunction:
|
||||
Type: AWS::Serverless::Function
|
||||
Properties:
|
||||
Handler: com.baeldung.lambda.apigateway.APIDemoHandler::handleRequest
|
||||
Runtime: java8
|
||||
Timeout: 15
|
||||
MemorySize: 512
|
||||
CodeUri: ../target/aws-lambda-0.1.0-SNAPSHOT.jar
|
||||
Policies:
|
||||
- DynamoDBCrudPolicy:
|
||||
TableName: !Ref PersonTable
|
||||
Environment:
|
||||
Variables:
|
||||
TABLE_NAME: !Ref PersonTable
|
||||
Events:
|
||||
StoreApi:
|
||||
Type: Api
|
||||
Properties:
|
||||
Path: /persons
|
||||
Method: PUT
|
||||
RestApiId:
|
||||
Ref: MyApi
|
||||
GetPersonByHTTPParamFunction:
|
||||
Type: AWS::Serverless::Function
|
||||
Properties:
|
||||
Handler: com.baeldung.lambda.apigateway.APIDemoHandler::handleGetByParam
|
||||
Runtime: java8
|
||||
Timeout: 15
|
||||
MemorySize: 512
|
||||
CodeUri: ../target/aws-lambda-0.1.0-SNAPSHOT.jar
|
||||
Policies:
|
||||
- DynamoDBReadPolicy:
|
||||
TableName: !Ref PersonTable
|
||||
Environment:
|
||||
Variables:
|
||||
TABLE_NAME: !Ref PersonTable
|
||||
Events:
|
||||
GetByPathApi:
|
||||
Type: Api
|
||||
Properties:
|
||||
Path: /persons/{id}
|
||||
Method: GET
|
||||
RestApiId:
|
||||
Ref: MyApi
|
||||
GetByQueryApi:
|
||||
Type: Api
|
||||
Properties:
|
||||
Path: /persons
|
||||
Method: GET
|
||||
RestApiId:
|
||||
Ref: MyApi
|
||||
MyApi:
|
||||
Type: AWS::Serverless::Api
|
||||
Properties:
|
||||
StageName: test
|
||||
EndpointConfiguration: REGIONAL
|
||||
DefinitionBody:
|
||||
swagger: "2.0"
|
||||
info:
|
||||
title: "TestAPI"
|
||||
paths:
|
||||
/persons:
|
||||
get:
|
||||
parameters:
|
||||
- name: "id"
|
||||
in: "query"
|
||||
required: true
|
||||
type: "string"
|
||||
x-amazon-apigateway-request-validator: "Validate query string parameters and\
|
||||
\ headers"
|
||||
x-amazon-apigateway-integration:
|
||||
uri:
|
||||
Fn::Sub: arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${GetPersonByHTTPParamFunction.Arn}/invocations
|
||||
responses: {}
|
||||
httpMethod: "POST"
|
||||
type: "aws_proxy"
|
||||
put:
|
||||
x-amazon-apigateway-integration:
|
||||
uri:
|
||||
Fn::Sub: arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${StorePersonFunction.Arn}/invocations
|
||||
responses: {}
|
||||
httpMethod: "POST"
|
||||
type: "aws_proxy"
|
||||
/persons/{id}:
|
||||
get:
|
||||
parameters:
|
||||
- name: "id"
|
||||
in: "path"
|
||||
required: true
|
||||
type: "string"
|
||||
responses: {}
|
||||
x-amazon-apigateway-integration:
|
||||
uri:
|
||||
Fn::Sub: arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${GetPersonByHTTPParamFunction.Arn}/invocations
|
||||
responses: {}
|
||||
httpMethod: "POST"
|
||||
type: "aws_proxy"
|
||||
x-amazon-apigateway-request-validators:
|
||||
Validate query string parameters and headers:
|
||||
validateRequestParameters: true
|
||||
validateRequestBody: false
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
package com.baeldung.lambda;
|
||||
|
||||
import com.amazonaws.services.lambda.runtime.Context;
|
||||
|
||||
public class LambdaMethodHandler {
|
||||
public String handleRequest(String input, Context context) {
|
||||
context.getLogger().log("Input: " + input);
|
||||
return "Hello World - " + input;
|
||||
}
|
||||
}
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
package com.baeldung.lambda;
|
||||
|
||||
import com.amazonaws.services.lambda.runtime.Context;
|
||||
import com.amazonaws.services.lambda.runtime.RequestHandler;
|
||||
|
||||
public class LambdaRequestHandler implements RequestHandler<String, String> {
|
||||
public String handleRequest(String input, Context context) {
|
||||
context.getLogger().log("Input: " + input);
|
||||
return "Hello World - " + input;
|
||||
}
|
||||
}
|
||||
+17
@@ -0,0 +1,17 @@
|
||||
package com.baeldung.lambda;
|
||||
|
||||
import com.amazonaws.services.lambda.runtime.Context;
|
||||
import com.amazonaws.services.lambda.runtime.RequestStreamHandler;
|
||||
import org.apache.commons.io.IOUtils;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
|
||||
public class LambdaRequestStreamHandler implements RequestStreamHandler {
|
||||
|
||||
public void handleRequest(InputStream inputStream, OutputStream outputStream, Context context) throws IOException {
|
||||
String input = IOUtils.toString(inputStream, "UTF-8");
|
||||
outputStream.write(("Hello World - " + input).getBytes());
|
||||
}
|
||||
}
|
||||
+121
@@ -0,0 +1,121 @@
|
||||
package com.baeldung.lambda.apigateway;
|
||||
|
||||
import com.amazonaws.services.dynamodbv2.AmazonDynamoDB;
|
||||
import com.amazonaws.services.dynamodbv2.AmazonDynamoDBClientBuilder;
|
||||
import com.amazonaws.services.dynamodbv2.document.*;
|
||||
import com.amazonaws.services.dynamodbv2.document.spec.PutItemSpec;
|
||||
import com.amazonaws.services.lambda.runtime.Context;
|
||||
import com.amazonaws.services.lambda.runtime.RequestStreamHandler;
|
||||
import com.baeldung.lambda.apigateway.model.Person;
|
||||
import org.json.simple.JSONObject;
|
||||
import org.json.simple.parser.JSONParser;
|
||||
import org.json.simple.parser.ParseException;
|
||||
|
||||
import java.io.*;
|
||||
|
||||
public class APIDemoHandler implements RequestStreamHandler {
|
||||
|
||||
private JSONParser parser = new JSONParser();
|
||||
private static final String DYNAMODB_TABLE_NAME = System.getenv("TABLE_NAME");
|
||||
|
||||
@Override
|
||||
public void handleRequest(InputStream inputStream, OutputStream outputStream, Context context) throws IOException {
|
||||
|
||||
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
|
||||
JSONObject responseJson = new JSONObject();
|
||||
|
||||
AmazonDynamoDB client = AmazonDynamoDBClientBuilder.defaultClient();
|
||||
DynamoDB dynamoDb = new DynamoDB(client);
|
||||
|
||||
try {
|
||||
JSONObject event = (JSONObject) parser.parse(reader);
|
||||
|
||||
if (event.get("body") != null) {
|
||||
|
||||
Person person = new Person((String) event.get("body"));
|
||||
|
||||
dynamoDb.getTable(DYNAMODB_TABLE_NAME)
|
||||
.putItem(new PutItemSpec().withItem(new Item().withNumber("id", person.getId())
|
||||
.withString("name", person.getName())));
|
||||
}
|
||||
|
||||
JSONObject responseBody = new JSONObject();
|
||||
responseBody.put("message", "New item created");
|
||||
|
||||
JSONObject headerJson = new JSONObject();
|
||||
headerJson.put("x-custom-header", "my custom header value");
|
||||
|
||||
responseJson.put("statusCode", 200);
|
||||
responseJson.put("headers", headerJson);
|
||||
responseJson.put("body", responseBody.toString());
|
||||
|
||||
} catch (ParseException pex) {
|
||||
responseJson.put("statusCode", 400);
|
||||
responseJson.put("exception", pex);
|
||||
}
|
||||
|
||||
OutputStreamWriter writer = new OutputStreamWriter(outputStream, "UTF-8");
|
||||
writer.write(responseJson.toString());
|
||||
writer.close();
|
||||
}
|
||||
|
||||
public void handleGetByParam(InputStream inputStream, OutputStream outputStream, Context context) throws IOException {
|
||||
|
||||
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
|
||||
JSONObject responseJson = new JSONObject();
|
||||
|
||||
AmazonDynamoDB client = AmazonDynamoDBClientBuilder.defaultClient();
|
||||
DynamoDB dynamoDb = new DynamoDB(client);
|
||||
|
||||
Item result = null;
|
||||
try {
|
||||
JSONObject event = (JSONObject) parser.parse(reader);
|
||||
JSONObject responseBody = new JSONObject();
|
||||
|
||||
if (event.get("pathParameters") != null) {
|
||||
|
||||
JSONObject pps = (JSONObject) event.get("pathParameters");
|
||||
if (pps.get("id") != null) {
|
||||
|
||||
int id = Integer.parseInt((String) pps.get("id"));
|
||||
result = dynamoDb.getTable(DYNAMODB_TABLE_NAME)
|
||||
.getItem("id", id);
|
||||
}
|
||||
|
||||
} else if (event.get("queryStringParameters") != null) {
|
||||
|
||||
JSONObject qps = (JSONObject) event.get("queryStringParameters");
|
||||
if (qps.get("id") != null) {
|
||||
|
||||
int id = Integer.parseInt((String) qps.get("id"));
|
||||
result = dynamoDb.getTable(DYNAMODB_TABLE_NAME)
|
||||
.getItem("id", id);
|
||||
}
|
||||
}
|
||||
if (result != null) {
|
||||
|
||||
Person person = new Person(result.toJSON());
|
||||
responseBody.put("Person", person);
|
||||
responseJson.put("statusCode", 200);
|
||||
} else {
|
||||
|
||||
responseBody.put("message", "No item found");
|
||||
responseJson.put("statusCode", 404);
|
||||
}
|
||||
|
||||
JSONObject headerJson = new JSONObject();
|
||||
headerJson.put("x-custom-header", "my custom header value");
|
||||
|
||||
responseJson.put("headers", headerJson);
|
||||
responseJson.put("body", responseBody.toString());
|
||||
|
||||
} catch (ParseException pex) {
|
||||
responseJson.put("statusCode", 400);
|
||||
responseJson.put("exception", pex);
|
||||
}
|
||||
|
||||
OutputStreamWriter writer = new OutputStreamWriter(outputStream, "UTF-8");
|
||||
writer.write(responseJson.toString());
|
||||
writer.close();
|
||||
}
|
||||
}
|
||||
+38
@@ -0,0 +1,38 @@
|
||||
package com.baeldung.lambda.apigateway.model;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.GsonBuilder;
|
||||
|
||||
public class Person {
|
||||
|
||||
private int id;
|
||||
private String name;
|
||||
|
||||
public Person(String json) {
|
||||
Gson gson = new Gson();
|
||||
Person request = gson.fromJson(json, Person.class);
|
||||
this.id = request.getId();
|
||||
this.name = request.getName();
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
final Gson gson = new GsonBuilder().setPrettyPrinting().create();
|
||||
return gson.toJson(this);
|
||||
}
|
||||
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(int id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
}
|
||||
+49
@@ -0,0 +1,49 @@
|
||||
package com.baeldung.lambda.dynamodb;
|
||||
|
||||
import com.amazonaws.regions.Region;
|
||||
import com.amazonaws.regions.Regions;
|
||||
import com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient;
|
||||
import com.amazonaws.services.dynamodbv2.document.DynamoDB;
|
||||
import com.amazonaws.services.dynamodbv2.document.Item;
|
||||
import com.amazonaws.services.dynamodbv2.document.PutItemOutcome;
|
||||
import com.amazonaws.services.dynamodbv2.document.spec.PutItemSpec;
|
||||
import com.amazonaws.services.dynamodbv2.model.ConditionalCheckFailedException;
|
||||
import com.amazonaws.services.lambda.runtime.Context;
|
||||
import com.amazonaws.services.lambda.runtime.RequestHandler;
|
||||
import com.baeldung.lambda.dynamodb.bean.PersonRequest;
|
||||
import com.baeldung.lambda.dynamodb.bean.PersonResponse;
|
||||
|
||||
public class SavePersonHandler implements RequestHandler<PersonRequest, PersonResponse> {
|
||||
|
||||
private DynamoDB dynamoDb;
|
||||
|
||||
private String DYNAMODB_TABLE_NAME = "Person";
|
||||
private Regions REGION = Regions.US_WEST_2;
|
||||
|
||||
public PersonResponse handleRequest(PersonRequest personRequest, Context context) {
|
||||
this.initDynamoDbClient();
|
||||
|
||||
persistData(personRequest);
|
||||
|
||||
PersonResponse personResponse = new PersonResponse();
|
||||
personResponse.setMessage("Saved Successfully!!!");
|
||||
return personResponse;
|
||||
}
|
||||
|
||||
private PutItemOutcome persistData(PersonRequest personRequest) throws ConditionalCheckFailedException {
|
||||
return this.dynamoDb.getTable(DYNAMODB_TABLE_NAME)
|
||||
.putItem(
|
||||
new PutItemSpec().withItem(new Item()
|
||||
.withNumber("id", personRequest.getId())
|
||||
.withString("firstName", personRequest.getFirstName())
|
||||
.withString("lastName", personRequest.getLastName())
|
||||
.withNumber("age", personRequest.getAge())
|
||||
.withString("address", personRequest.getAddress())));
|
||||
}
|
||||
|
||||
private void initDynamoDbClient() {
|
||||
AmazonDynamoDBClient client = new AmazonDynamoDBClient();
|
||||
client.setRegion(Region.getRegion(REGION));
|
||||
this.dynamoDb = new DynamoDB(client);
|
||||
}
|
||||
}
|
||||
+67
@@ -0,0 +1,67 @@
|
||||
package com.baeldung.lambda.dynamodb.bean;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.GsonBuilder;
|
||||
|
||||
public class PersonRequest {
|
||||
private int id;
|
||||
private String firstName;
|
||||
private String lastName;
|
||||
private int age;
|
||||
private String address;
|
||||
|
||||
public static void main(String[] args) {
|
||||
PersonRequest personRequest = new PersonRequest();
|
||||
personRequest.setId(1);
|
||||
personRequest.setFirstName("John");
|
||||
personRequest.setLastName("Doe");
|
||||
personRequest.setAge(30);
|
||||
personRequest.setAddress("United States");
|
||||
System.out.println(personRequest);
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
final Gson gson = new GsonBuilder().setPrettyPrinting().create();
|
||||
return gson.toJson(this);
|
||||
}
|
||||
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(int id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getFirstName() {
|
||||
return firstName;
|
||||
}
|
||||
|
||||
public void setFirstName(String firstName) {
|
||||
this.firstName = firstName;
|
||||
}
|
||||
|
||||
public String getLastName() {
|
||||
return lastName;
|
||||
}
|
||||
|
||||
public void setLastName(String lastName) {
|
||||
this.lastName = lastName;
|
||||
}
|
||||
|
||||
public int getAge() {
|
||||
return age;
|
||||
}
|
||||
|
||||
public void setAge(int age) {
|
||||
this.age = age;
|
||||
}
|
||||
|
||||
public String getAddress() {
|
||||
return address;
|
||||
}
|
||||
|
||||
public void setAddress(String address) {
|
||||
this.address = address;
|
||||
}
|
||||
}
|
||||
+20
@@ -0,0 +1,20 @@
|
||||
package com.baeldung.lambda.dynamodb.bean;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
|
||||
public class PersonResponse {
|
||||
private String message;
|
||||
|
||||
public String toString() {
|
||||
final Gson gson = new Gson();
|
||||
return gson.toJson(this);
|
||||
}
|
||||
|
||||
public String getMessage() {
|
||||
return message;
|
||||
}
|
||||
|
||||
public void setMessage(String message) {
|
||||
this.message = message;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<configuration>
|
||||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<encoder>
|
||||
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
|
||||
</pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<root level="INFO">
|
||||
<appender-ref ref="STDOUT" />
|
||||
</root>
|
||||
</configuration>
|
||||
@@ -0,0 +1,22 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<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>
|
||||
<artifactId>aws-lambda</artifactId>
|
||||
<name>aws-lambda</name>
|
||||
<packaging>pom</packaging>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>aws-modules</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<modules>
|
||||
<module>lambda</module>
|
||||
<module>shipping-tracker/ShippingFunction</module>
|
||||
<module>todo-reminder/ToDoFunction</module>
|
||||
</modules>
|
||||
|
||||
</project>
|
||||
@@ -0,0 +1 @@
|
||||
.aws-sam/
|
||||
@@ -0,0 +1,74 @@
|
||||
<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/maven-v4_0_0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>ShippingFunction</artifactId>
|
||||
<version>1.0</version>
|
||||
<name>ShippingFunction</name>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.amazonaws</groupId>
|
||||
<artifactId>aws-lambda-java-core</artifactId>
|
||||
<version>${aws-lambda-java-core.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.amazonaws</groupId>
|
||||
<artifactId>aws-lambda-java-events</artifactId>
|
||||
<version>${aws-lambda-java-events.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.fasterxml.jackson.core</groupId>
|
||||
<artifactId>jackson-databind</artifactId>
|
||||
<version>${jackson-databind.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.hibernate</groupId>
|
||||
<artifactId>hibernate-core</artifactId>
|
||||
<version>${hibernate.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.hibernate</groupId>
|
||||
<artifactId>hibernate-hikaricp</artifactId>
|
||||
<version>${hibernate.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.postgresql</groupId>
|
||||
<artifactId>postgresql</artifactId>
|
||||
<version>${postgresql.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-shade-plugin</artifactId>
|
||||
<version>3.1.1</version>
|
||||
<configuration>
|
||||
</configuration>
|
||||
<executions>
|
||||
<execution>
|
||||
<phase>package</phase>
|
||||
<goals>
|
||||
<goal>shade</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source>1.8</maven.compiler.source>
|
||||
<maven.compiler.target>1.8</maven.compiler.target>
|
||||
<hibernate.version>5.4.21.Final</hibernate.version>
|
||||
<aws-lambda-java-core.version>1.2.0</aws-lambda-java-core.version>
|
||||
<aws-lambda-java-events.version>3.1.0</aws-lambda-java-events.version>
|
||||
<jackson-databind.version>2.11.2</jackson-databind.version>
|
||||
<postgresql.version>42.2.16</postgresql.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
+121
@@ -0,0 +1,121 @@
|
||||
package com.baeldung.lambda.shipping;
|
||||
|
||||
import com.amazonaws.services.lambda.runtime.Context;
|
||||
import com.amazonaws.services.lambda.runtime.RequestHandler;
|
||||
import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyRequestEvent;
|
||||
import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyResponseEvent;
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.zaxxer.hikari.HikariDataSource;
|
||||
import org.hibernate.SessionFactory;
|
||||
import org.hibernate.boot.MetadataSources;
|
||||
import org.hibernate.boot.registry.StandardServiceRegistry;
|
||||
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
|
||||
import org.hibernate.engine.jdbc.connections.spi.ConnectionProvider;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.hibernate.cfg.AvailableSettings.*;
|
||||
import static org.hibernate.cfg.AvailableSettings.PASS;
|
||||
|
||||
/**
|
||||
* Handler for requests to Lambda function.
|
||||
*/
|
||||
public class App implements RequestHandler<APIGatewayProxyRequestEvent, APIGatewayProxyResponseEvent> {
|
||||
private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
|
||||
private SessionFactory sessionFactory = createSessionFactory();
|
||||
|
||||
public APIGatewayProxyResponseEvent handleRequest(APIGatewayProxyRequestEvent input, Context context) {
|
||||
try {
|
||||
ShippingService service = new ShippingService(sessionFactory, new ShippingDao());
|
||||
return routeRequest(input, service);
|
||||
} finally {
|
||||
flushConnectionPool();
|
||||
}
|
||||
}
|
||||
|
||||
private APIGatewayProxyResponseEvent routeRequest(APIGatewayProxyRequestEvent input, ShippingService service) {
|
||||
Map<String, String> headers = new HashMap<>();
|
||||
headers.put("Content-Type", "application/json");
|
||||
headers.put("X-Custom-Header", "application/json");
|
||||
|
||||
Object result = "OK";
|
||||
|
||||
switch (input.getResource()) {
|
||||
case "/consignment":
|
||||
result = service.createConsignment(
|
||||
fromJson(input.getBody(), Consignment.class));
|
||||
break;
|
||||
case "/consignment/{id}":
|
||||
result = service.view(input.getPathParameters().get("id"));
|
||||
break;
|
||||
case "/consignment/{id}/item":
|
||||
service.addItem(input.getPathParameters().get("id"),
|
||||
fromJson(input.getBody(), Item.class));
|
||||
break;
|
||||
case "/consignment/{id}/checkin":
|
||||
service.checkIn(input.getPathParameters().get("id"),
|
||||
fromJson(input.getBody(), Checkin.class));
|
||||
break;
|
||||
}
|
||||
|
||||
return new APIGatewayProxyResponseEvent()
|
||||
.withHeaders(headers)
|
||||
.withStatusCode(200)
|
||||
.withBody(toJson(result));
|
||||
}
|
||||
|
||||
private static <T> String toJson(T object) {
|
||||
try {
|
||||
return OBJECT_MAPPER.writeValueAsString(object);
|
||||
} catch (JsonProcessingException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
private static <T> T fromJson(String json, Class<T> type) {
|
||||
try {
|
||||
return OBJECT_MAPPER.readValue(json, type);
|
||||
} catch (JsonProcessingException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
private static SessionFactory createSessionFactory() {
|
||||
Map<String, String> settings = new HashMap<>();
|
||||
settings.put(URL, System.getenv("DB_URL"));
|
||||
settings.put(DIALECT, "org.hibernate.dialect.PostgreSQLDialect");
|
||||
settings.put(DEFAULT_SCHEMA, "shipping");
|
||||
settings.put(DRIVER, "org.postgresql.Driver");
|
||||
settings.put(USER, System.getenv("DB_USER"));
|
||||
settings.put(PASS, System.getenv("DB_PASSWORD"));
|
||||
settings.put("hibernate.hikari.connectionTimeout", "20000");
|
||||
settings.put("hibernate.hikari.minimumIdle", "1");
|
||||
settings.put("hibernate.hikari.maximumPoolSize", "2");
|
||||
settings.put("hibernate.hikari.idleTimeout", "30000");
|
||||
|
||||
// commented out as we only need them on first use
|
||||
// settings.put(HBM2DDL_AUTO, "create-only");
|
||||
// settings.put(HBM2DDL_DATABASE_ACTION, "create");
|
||||
|
||||
StandardServiceRegistry registry = new StandardServiceRegistryBuilder()
|
||||
.applySettings(settings)
|
||||
.build();
|
||||
|
||||
return new MetadataSources(registry)
|
||||
.addAnnotatedClass(Consignment.class)
|
||||
.addAnnotatedClass(Item.class)
|
||||
.addAnnotatedClass(Checkin.class)
|
||||
.buildMetadata()
|
||||
.buildSessionFactory();
|
||||
}
|
||||
|
||||
private void flushConnectionPool() {
|
||||
ConnectionProvider connectionProvider = sessionFactory.getSessionFactoryOptions()
|
||||
.getServiceRegistry()
|
||||
.getService(ConnectionProvider.class);
|
||||
HikariDataSource hikariDataSource = connectionProvider.unwrap(HikariDataSource.class);
|
||||
hikariDataSource.getHikariPoolMXBean().softEvictConnections();
|
||||
}
|
||||
}
|
||||
+28
@@ -0,0 +1,28 @@
|
||||
package com.baeldung.lambda.shipping;
|
||||
|
||||
import javax.persistence.Column;
|
||||
import javax.persistence.Embeddable;
|
||||
|
||||
@Embeddable
|
||||
public class Checkin {
|
||||
private String timeStamp;
|
||||
private String location;
|
||||
|
||||
@Column(name = "timestamp")
|
||||
public String getTimeStamp() {
|
||||
return timeStamp;
|
||||
}
|
||||
|
||||
public void setTimeStamp(String timeStamp) {
|
||||
this.timeStamp = timeStamp;
|
||||
}
|
||||
|
||||
@Column(name = "location")
|
||||
public String getLocation() {
|
||||
return location;
|
||||
}
|
||||
|
||||
public void setLocation(String location) {
|
||||
this.location = location;
|
||||
}
|
||||
}
|
||||
+77
@@ -0,0 +1,77 @@
|
||||
package com.baeldung.lambda.shipping;
|
||||
|
||||
import javax.persistence.*;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import static javax.persistence.FetchType.EAGER;
|
||||
|
||||
@Entity(name = "consignment")
|
||||
@Table(name = "consignment")
|
||||
public class Consignment {
|
||||
private String id;
|
||||
private String source;
|
||||
private String destination;
|
||||
private boolean isDelivered;
|
||||
private List<Item> items = new ArrayList<>();
|
||||
private List<Checkin> checkins = new ArrayList<>();
|
||||
|
||||
@Id
|
||||
@Column(name = "consignment_id")
|
||||
public String getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(String id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
@Column(name = "source")
|
||||
public String getSource() {
|
||||
return source;
|
||||
}
|
||||
|
||||
public void setSource(String source) {
|
||||
this.source = source;
|
||||
}
|
||||
|
||||
@Column(name = "destination")
|
||||
public String getDestination() {
|
||||
return destination;
|
||||
}
|
||||
|
||||
public void setDestination(String destination) {
|
||||
this.destination = destination;
|
||||
}
|
||||
|
||||
@Column(name = "delivered", columnDefinition = "boolean")
|
||||
public boolean isDelivered() {
|
||||
return isDelivered;
|
||||
}
|
||||
|
||||
public void setDelivered(boolean delivered) {
|
||||
isDelivered = delivered;
|
||||
}
|
||||
|
||||
@ElementCollection(fetch = EAGER)
|
||||
@CollectionTable(name = "consignment_item", joinColumns = @JoinColumn(name = "consignment_id"))
|
||||
@OrderColumn(name = "item_index")
|
||||
public List<Item> getItems() {
|
||||
return items;
|
||||
}
|
||||
|
||||
public void setItems(List<Item> items) {
|
||||
this.items = items;
|
||||
}
|
||||
|
||||
@ElementCollection(fetch = EAGER)
|
||||
@CollectionTable(name = "consignment_checkin", joinColumns = @JoinColumn(name = "consignment_id"))
|
||||
@OrderColumn(name = "checkin_index")
|
||||
public List<Checkin> getCheckins() {
|
||||
return checkins;
|
||||
}
|
||||
|
||||
public void setCheckins(List<Checkin> checkins) {
|
||||
this.checkins = checkins;
|
||||
}
|
||||
}
|
||||
+38
@@ -0,0 +1,38 @@
|
||||
package com.baeldung.lambda.shipping;
|
||||
|
||||
import javax.persistence.Column;
|
||||
import javax.persistence.Embeddable;
|
||||
|
||||
@Embeddable
|
||||
public class Item {
|
||||
private String location;
|
||||
private String description;
|
||||
private String timeStamp;
|
||||
|
||||
@Column(name = "location")
|
||||
public String getLocation() {
|
||||
return location;
|
||||
}
|
||||
|
||||
public void setLocation(String location) {
|
||||
this.location = location;
|
||||
}
|
||||
|
||||
@Column(name = "description")
|
||||
public String getDescription() {
|
||||
return description;
|
||||
}
|
||||
|
||||
public void setDescription(String description) {
|
||||
this.description = description;
|
||||
}
|
||||
|
||||
@Column(name = "timestamp")
|
||||
public String getTimeStamp() {
|
||||
return timeStamp;
|
||||
}
|
||||
|
||||
public void setTimeStamp(String timeStamp) {
|
||||
this.timeStamp = timeStamp;
|
||||
}
|
||||
}
|
||||
+25
@@ -0,0 +1,25 @@
|
||||
package com.baeldung.lambda.shipping;
|
||||
|
||||
import org.hibernate.Session;
|
||||
import org.hibernate.Transaction;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
public class ShippingDao {
|
||||
/**
|
||||
* Save a consignment to the database
|
||||
* @param consignment the consignment to save
|
||||
*/
|
||||
public void save(Session session, Consignment consignment) {
|
||||
Transaction transaction = session.beginTransaction();
|
||||
session.save(consignment);
|
||||
transaction.commit();
|
||||
}
|
||||
|
||||
/**
|
||||
* Find a consignment in the database by id
|
||||
*/
|
||||
public Optional<Consignment> find(Session session, String id) {
|
||||
return Optional.ofNullable(session.get(Consignment.class, id));
|
||||
}
|
||||
}
|
||||
+62
@@ -0,0 +1,62 @@
|
||||
package com.baeldung.lambda.shipping;
|
||||
|
||||
import org.hibernate.Session;
|
||||
import org.hibernate.SessionFactory;
|
||||
|
||||
import java.util.Comparator;
|
||||
import java.util.UUID;
|
||||
|
||||
public class ShippingService {
|
||||
private SessionFactory sessionFactory;
|
||||
private ShippingDao shippingDao;
|
||||
|
||||
public ShippingService(SessionFactory sessionFactory, ShippingDao shippingDao) {
|
||||
this.sessionFactory = sessionFactory;
|
||||
this.shippingDao = shippingDao;
|
||||
}
|
||||
|
||||
public String createConsignment(Consignment consignment) {
|
||||
try (Session session = sessionFactory.openSession()) {
|
||||
consignment.setDelivered(false);
|
||||
consignment.setId(UUID.randomUUID().toString());
|
||||
shippingDao.save(session, consignment);
|
||||
return consignment.getId();
|
||||
}
|
||||
}
|
||||
|
||||
public void addItem(String consignmentId, Item item) {
|
||||
try (Session session = sessionFactory.openSession()) {
|
||||
shippingDao.find(session, consignmentId)
|
||||
.ifPresent(consignment -> addItem(session, consignment, item));
|
||||
}
|
||||
}
|
||||
|
||||
private void addItem(Session session, Consignment consignment, Item item) {
|
||||
consignment.getItems()
|
||||
.add(item);
|
||||
shippingDao.save(session, consignment);
|
||||
}
|
||||
|
||||
public void checkIn(String consignmentId, Checkin checkin) {
|
||||
try (Session session = sessionFactory.openSession()) {
|
||||
shippingDao.find(session, consignmentId)
|
||||
.ifPresent(consignment -> checkIn(session, consignment, checkin));
|
||||
}
|
||||
}
|
||||
|
||||
private void checkIn(Session session, Consignment consignment, Checkin checkin) {
|
||||
consignment.getCheckins().add(checkin);
|
||||
consignment.getCheckins().sort(Comparator.comparing(Checkin::getTimeStamp));
|
||||
if (checkin.getLocation().equals(consignment.getDestination())) {
|
||||
consignment.setDelivered(true);
|
||||
}
|
||||
shippingDao.save(session, consignment);
|
||||
}
|
||||
|
||||
public Consignment view(String consignmentId) {
|
||||
try (Session session = sessionFactory.openSession()) {
|
||||
return shippingDao.find(session, consignmentId)
|
||||
.orElseGet(Consignment::new);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
AWSTemplateFormatVersion: '2010-09-09'
|
||||
Transform: AWS::Serverless-2016-10-31
|
||||
Description: >
|
||||
shipping-tracker
|
||||
|
||||
Sample SAM Template for shipping-tracker
|
||||
|
||||
# More info about Globals: https://github.com/awslabs/serverless-application-model/blob/master/docs/globals.rst
|
||||
Globals:
|
||||
Function:
|
||||
Timeout: 20
|
||||
|
||||
Resources:
|
||||
ShippingFunction:
|
||||
Type: AWS::Serverless::Function
|
||||
Properties:
|
||||
CodeUri: ShippingFunction
|
||||
Handler: com.baeldung.lambda.shipping.App::handleRequest
|
||||
Runtime: java8
|
||||
MemorySize: 512
|
||||
Environment:
|
||||
Variables:
|
||||
DB_URL: jdbc:postgresql://postgres/postgres
|
||||
DB_USER: postgres
|
||||
DB_PASSWORD: password
|
||||
Events:
|
||||
CreateConsignment:
|
||||
Type: Api
|
||||
Properties:
|
||||
Path: /consignment
|
||||
Method: post
|
||||
AddItem:
|
||||
Type: Api
|
||||
Properties:
|
||||
Path: /consignment/{id}/item
|
||||
Method: post
|
||||
CheckIn:
|
||||
Type: Api
|
||||
Properties:
|
||||
Path: /consignment/{id}/checkin
|
||||
Method: post
|
||||
ViewConsignment:
|
||||
Type: Api
|
||||
Properties:
|
||||
Path: /consignment/{id}
|
||||
Method: get
|
||||
|
||||
@@ -0,0 +1,119 @@
|
||||
<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/maven-v4_0_0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>helloworld</groupId>
|
||||
<artifactId>ToDoFunction</artifactId>
|
||||
<version>1.0</version>
|
||||
<name>ToDoFunction</name>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.amazonaws</groupId>
|
||||
<artifactId>aws-lambda-java-core</artifactId>
|
||||
<version>${aws-lambda-java-core.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.amazonaws</groupId>
|
||||
<artifactId>aws-lambda-java-events</artifactId>
|
||||
<version>${aws-lambda-java-events.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>uk.org.webcompere</groupId>
|
||||
<artifactId>lightweight-config</artifactId>
|
||||
<version>${lightweight-config.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.amazonaws</groupId>
|
||||
<artifactId>aws-lambda-java-log4j2</artifactId>
|
||||
<version>${aws-lambda-java-log4j2.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.logging.log4j</groupId>
|
||||
<artifactId>log4j-slf4j-impl</artifactId>
|
||||
<version>${log4j-slf4j-impl.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.github.openfeign</groupId>
|
||||
<artifactId>feign-core</artifactId>
|
||||
<version>${feign-core.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.github.openfeign</groupId>
|
||||
<artifactId>feign-slf4j</artifactId>
|
||||
<version>${feign-core.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.github.openfeign</groupId>
|
||||
<artifactId>feign-gson</artifactId>
|
||||
<version>${feign-core.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.google.inject</groupId>
|
||||
<artifactId>guice</artifactId>
|
||||
<version>${guice.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.junit.vintage</groupId>
|
||||
<artifactId>junit-vintage-engine</artifactId>
|
||||
<version>${junit-jupiter.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>uk.org.webcompere</groupId>
|
||||
<artifactId>system-stubs-junit4</artifactId>
|
||||
<version>${system-stubs-junit4.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.mockito</groupId>
|
||||
<artifactId>mockito-core</artifactId>
|
||||
<version>${mockito-core.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.assertj</groupId>
|
||||
<artifactId>assertj-core</artifactId>
|
||||
<version>${assertj-core.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-shade-plugin</artifactId>
|
||||
<version>3.2.4</version>
|
||||
<configuration>
|
||||
</configuration>
|
||||
<executions>
|
||||
<execution>
|
||||
<phase>package</phase>
|
||||
<goals>
|
||||
<goal>shade</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source>1.8</maven.compiler.source>
|
||||
<maven.compiler.target>1.8</maven.compiler.target>
|
||||
<aws-lambda-java-core.version>1.2.1</aws-lambda-java-core.version>
|
||||
<aws-lambda-java-events.version>3.6.0</aws-lambda-java-events.version>
|
||||
<lightweight-config.version>1.1.0</lightweight-config.version>
|
||||
<aws-lambda-java-log4j2.version>1.2.0</aws-lambda-java-log4j2.version>
|
||||
<log4j-slf4j-impl.version>2.13.2</log4j-slf4j-impl.version>
|
||||
<feign-core.version>11.2</feign-core.version>
|
||||
<guice.version>5.0.1</guice.version>
|
||||
<system-stubs-junit4.version>1.2.0</system-stubs-junit4.version>
|
||||
<mockito-core.version>4.1.0</mockito-core.version>
|
||||
<assertj-core.version>3.19.0</assertj-core.version>
|
||||
<junit-jupiter.version>5.8.1</junit-jupiter.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
+43
@@ -0,0 +1,43 @@
|
||||
package com.baeldung.lambda.todo;
|
||||
|
||||
import java.io.*;
|
||||
import java.net.URL;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import com.amazonaws.services.lambda.runtime.Context;
|
||||
import com.amazonaws.services.lambda.runtime.RequestHandler;
|
||||
import com.amazonaws.services.lambda.runtime.RequestStreamHandler;
|
||||
import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyRequestEvent;
|
||||
import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyResponseEvent;
|
||||
import com.baeldung.lambda.todo.config.ExecutionContext;
|
||||
import com.baeldung.lambda.todo.service.PostService;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
/**
|
||||
* Handler for requests to Lambda function.
|
||||
*/
|
||||
public class App implements RequestStreamHandler {
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(App.class);
|
||||
|
||||
private String environmentName = System.getenv("ENV_NAME");
|
||||
private ExecutionContext executionContext = new ExecutionContext();
|
||||
|
||||
@Override
|
||||
public void handleRequest(InputStream inputStream, OutputStream outputStream, Context context) throws IOException {
|
||||
context.getLogger().log("App starting\n");
|
||||
context.getLogger().log("Environment: "
|
||||
+ environmentName + "\n");
|
||||
|
||||
try {
|
||||
PostService postService = executionContext.getPostService();
|
||||
executionContext.getToDoReaderService()
|
||||
.getOldestToDo()
|
||||
.ifPresent(postService::makePost);
|
||||
} catch (Exception e) {
|
||||
LOGGER.error("Failed: {}", e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
}
|
||||
+8
@@ -0,0 +1,8 @@
|
||||
package com.baeldung.lambda.todo.api;
|
||||
|
||||
import feign.RequestLine;
|
||||
|
||||
public interface PostApi {
|
||||
@RequestLine("POST /posts")
|
||||
void makePost(PostItem item);
|
||||
}
|
||||
+40
@@ -0,0 +1,40 @@
|
||||
package com.baeldung.lambda.todo.api;
|
||||
|
||||
public class PostItem {
|
||||
private String title;
|
||||
private String body;
|
||||
private int userId;
|
||||
|
||||
public String getTitle() {
|
||||
return title;
|
||||
}
|
||||
|
||||
public void setTitle(String title) {
|
||||
this.title = title;
|
||||
}
|
||||
|
||||
public String getBody() {
|
||||
return body;
|
||||
}
|
||||
|
||||
public void setBody(String body) {
|
||||
this.body = body;
|
||||
}
|
||||
|
||||
public int getUserId() {
|
||||
return userId;
|
||||
}
|
||||
|
||||
public void setUserId(int userId) {
|
||||
this.userId = userId;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "PostItem{"
|
||||
+ "title='" + title + '\''
|
||||
+ ", body='" + body + '\''
|
||||
+ ", userId=" + userId +
|
||||
'}';
|
||||
}
|
||||
}
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
package com.baeldung.lambda.todo.api;
|
||||
|
||||
import feign.RequestLine;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface ToDoApi {
|
||||
@RequestLine("GET /todos")
|
||||
List<ToDoItem> getAllTodos();
|
||||
}
|
||||
+50
@@ -0,0 +1,50 @@
|
||||
package com.baeldung.lambda.todo.api;
|
||||
|
||||
public class ToDoItem {
|
||||
private int userId;
|
||||
private int id;
|
||||
private String title;
|
||||
private boolean completed;
|
||||
|
||||
public int getUserId() {
|
||||
return userId;
|
||||
}
|
||||
|
||||
public void setUserId(int userId) {
|
||||
this.userId = userId;
|
||||
}
|
||||
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(int id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getTitle() {
|
||||
return title;
|
||||
}
|
||||
|
||||
public void setTitle(String title) {
|
||||
this.title = title;
|
||||
}
|
||||
|
||||
public boolean isCompleted() {
|
||||
return completed;
|
||||
}
|
||||
|
||||
public void setCompleted(boolean completed) {
|
||||
this.completed = completed;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "ToDoItem{"
|
||||
+ "userId=" + userId
|
||||
+ ", id=" + id
|
||||
+ ", title='" + title + '\''
|
||||
+ ", completed=" + completed +
|
||||
'}';
|
||||
}
|
||||
}
|
||||
+50
@@ -0,0 +1,50 @@
|
||||
package com.baeldung.lambda.todo.config;
|
||||
|
||||
public class Config {
|
||||
private String toDoEndpoint;
|
||||
private String postEndpoint;
|
||||
private String environmentName;
|
||||
|
||||
private Credentials toDoCredentials;
|
||||
private Credentials postCredentials;
|
||||
|
||||
public String getToDoEndpoint() {
|
||||
return toDoEndpoint;
|
||||
}
|
||||
|
||||
public void setToDoEndpoint(String toDoEndpoint) {
|
||||
this.toDoEndpoint = toDoEndpoint;
|
||||
}
|
||||
|
||||
public String getPostEndpoint() {
|
||||
return postEndpoint;
|
||||
}
|
||||
|
||||
public void setPostEndpoint(String postEndpoint) {
|
||||
this.postEndpoint = postEndpoint;
|
||||
}
|
||||
|
||||
public String getEnvironmentName() {
|
||||
return environmentName;
|
||||
}
|
||||
|
||||
public void setEnvironmentName(String environmentName) {
|
||||
this.environmentName = environmentName;
|
||||
}
|
||||
|
||||
public Credentials getToDoCredentials() {
|
||||
return toDoCredentials;
|
||||
}
|
||||
|
||||
public void setToDoCredentials(Credentials toDoCredentials) {
|
||||
this.toDoCredentials = toDoCredentials;
|
||||
}
|
||||
|
||||
public Credentials getPostCredentials() {
|
||||
return postCredentials;
|
||||
}
|
||||
|
||||
public void setPostCredentials(Credentials postCredentials) {
|
||||
this.postCredentials = postCredentials;
|
||||
}
|
||||
}
|
||||
+22
@@ -0,0 +1,22 @@
|
||||
package com.baeldung.lambda.todo.config;
|
||||
|
||||
public class Credentials {
|
||||
private String username;
|
||||
private String password;
|
||||
|
||||
public String getUsername() {
|
||||
return username;
|
||||
}
|
||||
|
||||
public void setUsername(String username) {
|
||||
this.username = username;
|
||||
}
|
||||
|
||||
public String getPassword() {
|
||||
return password;
|
||||
}
|
||||
|
||||
public void setPassword(String password) {
|
||||
this.password = password;
|
||||
}
|
||||
}
|
||||
+36
@@ -0,0 +1,36 @@
|
||||
package com.baeldung.lambda.todo.config;
|
||||
|
||||
import com.baeldung.lambda.todo.service.PostService;
|
||||
import com.baeldung.lambda.todo.service.ToDoReaderService;
|
||||
import com.google.inject.Guice;
|
||||
import com.google.inject.Injector;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
public class ExecutionContext {
|
||||
private static final Logger LOGGER =
|
||||
LoggerFactory.getLogger(ExecutionContext.class);
|
||||
|
||||
private ToDoReaderService toDoReaderService;
|
||||
private PostService postService;
|
||||
|
||||
public ExecutionContext() {
|
||||
LOGGER.info("Loading configuration");
|
||||
|
||||
try {
|
||||
Injector injector = Guice.createInjector(new Services());
|
||||
this.toDoReaderService = injector.getInstance(ToDoReaderService.class);
|
||||
this.postService = injector.getInstance(PostService.class);
|
||||
} catch (Exception e) {
|
||||
LOGGER.error("Could not start", e);
|
||||
}
|
||||
}
|
||||
|
||||
public ToDoReaderService getToDoReaderService() {
|
||||
return toDoReaderService;
|
||||
}
|
||||
|
||||
public PostService getPostService() {
|
||||
return postService;
|
||||
}
|
||||
}
|
||||
+38
@@ -0,0 +1,38 @@
|
||||
package com.baeldung.lambda.todo.config;
|
||||
|
||||
import com.baeldung.lambda.todo.api.PostApi;
|
||||
import com.baeldung.lambda.todo.api.ToDoApi;
|
||||
import com.google.inject.AbstractModule;
|
||||
import feign.Feign;
|
||||
import feign.auth.BasicAuthRequestInterceptor;
|
||||
import feign.gson.GsonDecoder;
|
||||
import feign.gson.GsonEncoder;
|
||||
import feign.slf4j.Slf4jLogger;
|
||||
import uk.org.webcompere.lightweightconfig.ConfigLoader;
|
||||
|
||||
import static feign.Logger.Level.FULL;
|
||||
|
||||
public class Services extends AbstractModule {
|
||||
@Override
|
||||
protected void configure() {
|
||||
Config config = ConfigLoader.loadYmlConfigFromResource("configuration.yml", Config.class);
|
||||
|
||||
ToDoApi toDoApi = Feign.builder()
|
||||
.decoder(new GsonDecoder())
|
||||
.logger(new Slf4jLogger())
|
||||
.logLevel(FULL)
|
||||
.requestInterceptor(new BasicAuthRequestInterceptor(config.getToDoCredentials().getUsername(), config.getToDoCredentials().getPassword()))
|
||||
.target(ToDoApi.class, config.getToDoEndpoint());
|
||||
|
||||
PostApi postApi = Feign.builder()
|
||||
.encoder(new GsonEncoder())
|
||||
.logger(new Slf4jLogger())
|
||||
.logLevel(FULL)
|
||||
.requestInterceptor(new BasicAuthRequestInterceptor(config.getPostCredentials().getUsername(), config.getPostCredentials().getPassword()))
|
||||
.target(PostApi.class, config.getPostEndpoint());
|
||||
|
||||
bind(Config.class).toInstance(config);
|
||||
bind(ToDoApi.class).toInstance(toDoApi);
|
||||
bind(PostApi.class).toInstance(postApi);
|
||||
}
|
||||
}
|
||||
+30
@@ -0,0 +1,30 @@
|
||||
package com.baeldung.lambda.todo.service;
|
||||
|
||||
import com.baeldung.lambda.todo.api.PostApi;
|
||||
import com.baeldung.lambda.todo.api.PostItem;
|
||||
import com.baeldung.lambda.todo.api.ToDoItem;
|
||||
import com.google.inject.Inject;
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
|
||||
|
||||
public class PostService {
|
||||
private static final Logger LOGGER = LogManager.getLogger(PostService.class);
|
||||
private PostApi postApi;
|
||||
|
||||
@Inject
|
||||
public PostService(PostApi postApi) {
|
||||
this.postApi = postApi;
|
||||
}
|
||||
|
||||
public void makePost(ToDoItem toDoItem) {
|
||||
LOGGER.info("Posting about: {}", toDoItem);
|
||||
PostItem item = new PostItem();
|
||||
item.setTitle("To Do is Out Of Date: " + toDoItem.getId());
|
||||
item.setUserId(toDoItem.getUserId());
|
||||
item.setBody("Not done: " + toDoItem.getTitle());
|
||||
|
||||
LOGGER.info("Post: {}", item);
|
||||
postApi.makePost(item);
|
||||
}
|
||||
}
|
||||
+30
@@ -0,0 +1,30 @@
|
||||
package com.baeldung.lambda.todo.service;
|
||||
|
||||
import com.baeldung.lambda.todo.api.ToDoApi;
|
||||
import com.baeldung.lambda.todo.api.ToDoItem;
|
||||
import com.baeldung.lambda.todo.config.Config;
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
|
||||
import javax.inject.Inject;
|
||||
import java.util.Optional;
|
||||
|
||||
public class ToDoReaderService {
|
||||
// Log4j logger
|
||||
private static final Logger LOGGER = LogManager.getLogger(ToDoReaderService.class);
|
||||
|
||||
private ToDoApi toDoApi;
|
||||
|
||||
@Inject
|
||||
public ToDoReaderService(Config configuration, ToDoApi toDoApi) {
|
||||
LOGGER.info("ToDo Endpoint on: {}", configuration.getToDoEndpoint());
|
||||
|
||||
this.toDoApi = toDoApi;
|
||||
}
|
||||
|
||||
public Optional<ToDoItem> getOldestToDo() {
|
||||
return toDoApi.getAllTodos().stream()
|
||||
.filter(item -> !item.isCompleted())
|
||||
.findFirst();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
toDoEndpoint: https://jsonplaceholder.typicode.com
|
||||
postEndpoint: https://jsonplaceholder.typicode.com
|
||||
environmentName: ${ENV_NAME}
|
||||
toDoCredentials:
|
||||
username: baeldung
|
||||
password: ${TODO_PASSWORD:-password}
|
||||
postCredentials:
|
||||
username: baeldung
|
||||
password: ${POST_PASSWORD:-password}
|
||||
@@ -0,0 +1,15 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<Configuration packages="com.amazonaws.services.lambda.runtime.log4j2">
|
||||
<Appenders>
|
||||
<Lambda name="Lambda">
|
||||
<PatternLayout>
|
||||
<pattern>%d{yyyy-MM-dd HH:mm:ss} %X{AWSRequestId} %-5p %c{1} - %m%n</pattern>
|
||||
</PatternLayout>
|
||||
</Lambda>
|
||||
</Appenders>
|
||||
<Loggers>
|
||||
<Root level="info">
|
||||
<AppenderRef ref="Lambda" />
|
||||
</Root>
|
||||
</Loggers>
|
||||
</Configuration>
|
||||
+58
@@ -0,0 +1,58 @@
|
||||
package com.baeldung.lambda.todo;
|
||||
|
||||
import com.amazonaws.services.lambda.runtime.Context;
|
||||
import com.baeldung.lambda.todo.config.Config;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.Answers;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.junit.MockitoJUnitRunner;
|
||||
import uk.org.webcompere.lightweightconfig.ConfigLoader;
|
||||
import uk.org.webcompere.systemstubs.rules.EnvironmentVariablesRule;
|
||||
import uk.org.webcompere.systemstubs.stream.input.LinesAltStream;
|
||||
import uk.org.webcompere.systemstubs.stream.output.NoopStream;
|
||||
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.util.Optional;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.Mockito.verify;
|
||||
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class AppTest {
|
||||
|
||||
@Mock(answer = Answers.RETURNS_DEEP_STUBS)
|
||||
private Context mockContext;
|
||||
|
||||
@Rule
|
||||
public EnvironmentVariablesRule environmentVariablesRule = new EnvironmentVariablesRule();
|
||||
|
||||
private InputStream fakeInputStream = new LinesAltStream();
|
||||
private OutputStream fakeOutputStream = new NoopStream();
|
||||
|
||||
@Test
|
||||
public void whenTheEnvironmentVariableIsSet_thenItIsLogged() throws Exception {
|
||||
environmentVariablesRule.set("ENV_NAME", "unitTest");
|
||||
new App().handleRequest(fakeInputStream, fakeOutputStream, mockContext);
|
||||
|
||||
verify(mockContext.getLogger())
|
||||
.log("Environment: unitTest\n");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenEnvironmentVariableIsNotSet_thenUseDefault() {
|
||||
String setting = Optional.ofNullable(System.getenv("SETTING"))
|
||||
.orElse("default");
|
||||
|
||||
assertThat(setting).isEqualTo("default");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenConfiguration_canLoadIntoPojo() {
|
||||
environmentVariablesRule.set("ENV_NAME", "unitTest");
|
||||
Config config = ConfigLoader.loadYmlConfigFromResource("configuration.yml", Config.class);
|
||||
assertThat(config.getEnvironmentName()).isEqualTo("unitTest");
|
||||
}
|
||||
}
|
||||
+24
@@ -0,0 +1,24 @@
|
||||
package com.baeldung.lambda.todo.service;
|
||||
|
||||
import com.baeldung.lambda.todo.config.Config;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import uk.org.webcompere.systemstubs.rules.SystemOutRule;
|
||||
|
||||
import static org.assertj.core.api.AssertionsForInterfaceTypes.assertThat;
|
||||
|
||||
public class ToDoReaderServiceTest {
|
||||
|
||||
@Rule
|
||||
public SystemOutRule systemOutRule = new SystemOutRule();
|
||||
|
||||
@Test
|
||||
public void whenTheServiceStarts_thenItOutputsEndpoint() {
|
||||
Config config = new Config();
|
||||
config.setToDoEndpoint("https://todo-endpoint.com");
|
||||
ToDoReaderService service = new ToDoReaderService(config, null);
|
||||
|
||||
assertThat(systemOutRule.getLinesNormalized())
|
||||
.contains("ToDo Endpoint on: https://todo-endpoint.com");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
AWSTemplateFormatVersion: '2010-09-09'
|
||||
Transform: AWS::Serverless-2016-10-31
|
||||
Description: todo-reminder application
|
||||
|
||||
Parameters:
|
||||
EnvironmentName:
|
||||
Type: String
|
||||
Default: dev
|
||||
|
||||
Resources:
|
||||
ToDoFunction:
|
||||
Type: AWS::Serverless::Function # More info about Function Resource: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#awsserverlessfunction
|
||||
Properties:
|
||||
Timeout: 20
|
||||
CodeUri: ToDoFunction
|
||||
Handler: com.baeldung.lambda.todo.App::handleRequest
|
||||
Runtime: java8
|
||||
MemorySize: 512
|
||||
Environment: # More info about Env Vars: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#environment-object
|
||||
Variables:
|
||||
PARAM1: VALUE
|
||||
ENV_NAME: !Ref EnvironmentName
|
||||
@@ -0,0 +1,2 @@
|
||||
/target/
|
||||
.idea/
|
||||
@@ -0,0 +1,10 @@
|
||||
## AWS Miscellaneous
|
||||
|
||||
This module contains articles about various Amazon Web Services (AWS) such as EC2, DynamoDB, SQS, RDS
|
||||
|
||||
### Relevant articles
|
||||
|
||||
- [Managing EC2 Instances in Java](https://www.baeldung.com/ec2-java)
|
||||
- [Integration Testing with a Local DynamoDB Instance](https://www.baeldung.com/dynamodb-local-integration-tests)
|
||||
- [Managing Amazon SQS Queues in Java](https://www.baeldung.com/aws-queues-java)
|
||||
- [Guide to AWS Aurora RDS with Java](https://www.baeldung.com/aws-aurora-rds-java)
|
||||
@@ -0,0 +1,104 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<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>
|
||||
<artifactId>aws-miscellaneous</artifactId>
|
||||
<version>0.1.0-SNAPSHOT</version>
|
||||
<name>aws-miscellaneous</name>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>aws-modules</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.amazonaws</groupId>
|
||||
<artifactId>aws-java-sdk</artifactId>
|
||||
<version>${aws-java-sdk.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.amazonaws</groupId>
|
||||
<artifactId>aws-lambda-java-core</artifactId>
|
||||
<version>${aws-lambda-java-core.version}</version>
|
||||
<exclusions>
|
||||
<exclusion>
|
||||
<artifactId>commons-logging</artifactId>
|
||||
<groupId>commons-logging</groupId>
|
||||
</exclusion>
|
||||
</exclusions>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.amazonaws</groupId>
|
||||
<artifactId>aws-lambda-java-events</artifactId>
|
||||
<version>${aws-lambda-java-events.version}</version>
|
||||
<exclusions>
|
||||
<exclusion>
|
||||
<artifactId>commons-logging</artifactId>
|
||||
<groupId>commons-logging</groupId>
|
||||
</exclusion>
|
||||
</exclusions>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>commons-io</groupId>
|
||||
<artifactId>commons-io</artifactId>
|
||||
<version>${commons-io.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.google.code.gson</groupId>
|
||||
<artifactId>gson</artifactId>
|
||||
<version>${gson.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.amazonaws</groupId>
|
||||
<artifactId>DynamoDBLocal</artifactId>
|
||||
<version>${dynamodblocal.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-shade-plugin</artifactId>
|
||||
<version>${maven-shade-plugin.version}</version>
|
||||
<configuration>
|
||||
<createDependencyReducedPom>false</createDependencyReducedPom>
|
||||
</configuration>
|
||||
<executions>
|
||||
<execution>
|
||||
<phase>package</phase>
|
||||
<goals>
|
||||
<goal>shade</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
<repositories>
|
||||
<repository>
|
||||
<id>dynamodb-local</id>
|
||||
<name>DynamoDB Local Release Repository</name>
|
||||
<url>${dynamodblocal.repository.url}</url>
|
||||
</repository>
|
||||
</repositories>
|
||||
|
||||
<properties>
|
||||
<aws-lambda-java-events.version>1.3.0</aws-lambda-java-events.version>
|
||||
<aws-lambda-java-core.version>1.1.0</aws-lambda-java-core.version>
|
||||
<gson.version>2.8.0</gson.version>
|
||||
<aws-java-sdk.version>1.11.290</aws-java-sdk.version>
|
||||
<dynamodblocal.version>1.11.86</dynamodblocal.version>
|
||||
<dynamodblocal.repository.url>https://s3-us-west-2.amazonaws.com/dynamodb-local/release</dynamodblocal.repository.url>
|
||||
<commons-codec-version>1.10.L001</commons-codec-version>
|
||||
<jets3t-version>0.9.4.0006L</jets3t-version>
|
||||
<maven-shade-plugin.version>3.0.0</maven-shade-plugin.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
+51
@@ -0,0 +1,51 @@
|
||||
package com.baeldung.dynamodb.entity;
|
||||
|
||||
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBAttribute;
|
||||
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBAutoGeneratedKey;
|
||||
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBHashKey;
|
||||
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBTable;
|
||||
|
||||
@DynamoDBTable(tableName = "ProductInfo")
|
||||
public class ProductInfo {
|
||||
|
||||
private String id;
|
||||
private String msrp;
|
||||
private String cost;
|
||||
|
||||
public ProductInfo() {
|
||||
}
|
||||
|
||||
public ProductInfo(String cost, String msrp) {
|
||||
this.msrp = msrp;
|
||||
this.cost = cost;
|
||||
}
|
||||
|
||||
@DynamoDBHashKey
|
||||
@DynamoDBAutoGeneratedKey
|
||||
public String getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
@DynamoDBAttribute
|
||||
public String getMsrp() {
|
||||
return msrp;
|
||||
}
|
||||
|
||||
@DynamoDBAttribute
|
||||
public String getCost() {
|
||||
return cost;
|
||||
}
|
||||
|
||||
public void setId(String id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public void setMsrp(String msrp) {
|
||||
this.msrp = msrp;
|
||||
}
|
||||
|
||||
public void setCost(String cost) {
|
||||
this.cost = cost;
|
||||
}
|
||||
|
||||
}
|
||||
+49
@@ -0,0 +1,49 @@
|
||||
package com.baeldung.dynamodb.repository;
|
||||
|
||||
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapper;
|
||||
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBScanExpression;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.lang.reflect.ParameterizedType;
|
||||
import java.util.List;
|
||||
|
||||
public abstract class AbstractRepository<T, ID extends Serializable> {
|
||||
|
||||
protected DynamoDBMapper mapper;
|
||||
protected Class<T> entityClass;
|
||||
|
||||
protected AbstractRepository() {
|
||||
ParameterizedType genericSuperclass = (ParameterizedType) getClass().getGenericSuperclass();
|
||||
|
||||
// This entityClass refers to the actual entity class in the subclass declaration.
|
||||
|
||||
// For instance, ProductInfoDAO extends AbstractDAO<ProductInfo, String>
|
||||
// In this case entityClass = ProductInfo, and ID is String type
|
||||
// which refers to the ProductInfo's partition key string value
|
||||
this.entityClass = (Class<T>) genericSuperclass.getActualTypeArguments()[0];
|
||||
}
|
||||
|
||||
public void save(T t) {
|
||||
mapper.save(t);
|
||||
}
|
||||
|
||||
public T findOne(ID id) {
|
||||
return mapper.load(entityClass, id);
|
||||
}
|
||||
|
||||
/**
|
||||
* <strong>WARNING:</strong> It is not recommended to perform full table scan
|
||||
* targeting the real production environment.
|
||||
*
|
||||
* @return All items
|
||||
*/
|
||||
public List<T> findAll() {
|
||||
DynamoDBScanExpression scanExpression = new DynamoDBScanExpression();
|
||||
return mapper.scan(entityClass, scanExpression);
|
||||
}
|
||||
|
||||
public void setMapper(DynamoDBMapper dynamoDBMapper) {
|
||||
this.mapper = dynamoDBMapper;
|
||||
}
|
||||
|
||||
}
|
||||
+6
@@ -0,0 +1,6 @@
|
||||
package com.baeldung.dynamodb.repository;
|
||||
|
||||
import com.baeldung.dynamodb.entity.ProductInfo;
|
||||
|
||||
public class ProductInfoRepository extends AbstractRepository<ProductInfo, String> {
|
||||
}
|
||||
@@ -0,0 +1,137 @@
|
||||
package com.baeldung.ec2;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
import com.amazonaws.auth.AWSCredentials;
|
||||
import com.amazonaws.auth.AWSStaticCredentialsProvider;
|
||||
import com.amazonaws.auth.BasicAWSCredentials;
|
||||
import com.amazonaws.regions.Regions;
|
||||
import com.amazonaws.services.ec2.AmazonEC2;
|
||||
import com.amazonaws.services.ec2.AmazonEC2ClientBuilder;
|
||||
import com.amazonaws.services.ec2.model.AuthorizeSecurityGroupIngressRequest;
|
||||
import com.amazonaws.services.ec2.model.CreateKeyPairRequest;
|
||||
import com.amazonaws.services.ec2.model.CreateKeyPairResult;
|
||||
import com.amazonaws.services.ec2.model.CreateSecurityGroupRequest;
|
||||
import com.amazonaws.services.ec2.model.DescribeInstancesRequest;
|
||||
import com.amazonaws.services.ec2.model.DescribeInstancesResult;
|
||||
import com.amazonaws.services.ec2.model.DescribeKeyPairsRequest;
|
||||
import com.amazonaws.services.ec2.model.DescribeKeyPairsResult;
|
||||
import com.amazonaws.services.ec2.model.IpPermission;
|
||||
import com.amazonaws.services.ec2.model.IpRange;
|
||||
import com.amazonaws.services.ec2.model.MonitorInstancesRequest;
|
||||
import com.amazonaws.services.ec2.model.RebootInstancesRequest;
|
||||
import com.amazonaws.services.ec2.model.RunInstancesRequest;
|
||||
import com.amazonaws.services.ec2.model.StartInstancesRequest;
|
||||
import com.amazonaws.services.ec2.model.StopInstancesRequest;
|
||||
import com.amazonaws.services.ec2.model.UnmonitorInstancesRequest;
|
||||
|
||||
public class EC2Application {
|
||||
|
||||
private static final AWSCredentials credentials;
|
||||
|
||||
static {
|
||||
// put your accesskey and secretkey here
|
||||
credentials = new BasicAWSCredentials(
|
||||
"<AWS accesskey>",
|
||||
"<AWS secretkey>"
|
||||
);
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
// Set up the client
|
||||
AmazonEC2 ec2Client = AmazonEC2ClientBuilder.standard()
|
||||
.withCredentials(new AWSStaticCredentialsProvider(credentials))
|
||||
.withRegion(Regions.US_EAST_1)
|
||||
.build();
|
||||
|
||||
// Create a security group
|
||||
CreateSecurityGroupRequest createSecurityGroupRequest = new CreateSecurityGroupRequest().withGroupName("BaeldungSecurityGroup")
|
||||
.withDescription("Baeldung Security Group");
|
||||
ec2Client.createSecurityGroup(createSecurityGroupRequest);
|
||||
|
||||
// Allow HTTP and SSH traffic
|
||||
IpRange ipRange1 = new IpRange().withCidrIp("0.0.0.0/0");
|
||||
|
||||
IpPermission ipPermission1 = new IpPermission().withIpv4Ranges(Arrays.asList(new IpRange[] { ipRange1 }))
|
||||
.withIpProtocol("tcp")
|
||||
.withFromPort(80)
|
||||
.withToPort(80);
|
||||
|
||||
IpPermission ipPermission2 = new IpPermission().withIpv4Ranges(Arrays.asList(new IpRange[] { ipRange1 }))
|
||||
.withIpProtocol("tcp")
|
||||
.withFromPort(22)
|
||||
.withToPort(22);
|
||||
|
||||
AuthorizeSecurityGroupIngressRequest authorizeSecurityGroupIngressRequest = new AuthorizeSecurityGroupIngressRequest()
|
||||
.withGroupName("BaeldungSecurityGroup")
|
||||
.withIpPermissions(ipPermission1, ipPermission2);
|
||||
|
||||
ec2Client.authorizeSecurityGroupIngress(authorizeSecurityGroupIngressRequest);
|
||||
|
||||
// Create KeyPair
|
||||
CreateKeyPairRequest createKeyPairRequest = new CreateKeyPairRequest()
|
||||
.withKeyName("baeldung-key-pair");
|
||||
CreateKeyPairResult createKeyPairResult = ec2Client.createKeyPair(createKeyPairRequest);
|
||||
String privateKey = createKeyPairResult
|
||||
.getKeyPair()
|
||||
.getKeyMaterial(); // make sure you keep it, the private key, Amazon doesn't store the private key
|
||||
|
||||
// See what key-pairs you've got
|
||||
DescribeKeyPairsRequest describeKeyPairsRequest = new DescribeKeyPairsRequest();
|
||||
DescribeKeyPairsResult describeKeyPairsResult = ec2Client.describeKeyPairs(describeKeyPairsRequest);
|
||||
|
||||
// Launch an Amazon Instance
|
||||
RunInstancesRequest runInstancesRequest = new RunInstancesRequest().withImageId("ami-97785bed") // https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/AMIs.html | https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/usingsharedamis-finding.html
|
||||
.withInstanceType("t2.micro") // https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/instance-types.html
|
||||
.withMinCount(1)
|
||||
.withMaxCount(1)
|
||||
.withKeyName("baeldung-key-pair") // optional - if not present, can't connect to instance
|
||||
.withSecurityGroups("BaeldungSecurityGroup");
|
||||
|
||||
String yourInstanceId = ec2Client.runInstances(runInstancesRequest).getReservation().getInstances().get(0).getInstanceId();
|
||||
|
||||
// Start an Instance
|
||||
StartInstancesRequest startInstancesRequest = new StartInstancesRequest()
|
||||
.withInstanceIds(yourInstanceId);
|
||||
|
||||
ec2Client.startInstances(startInstancesRequest);
|
||||
|
||||
// Monitor Instances
|
||||
MonitorInstancesRequest monitorInstancesRequest = new MonitorInstancesRequest()
|
||||
.withInstanceIds(yourInstanceId);
|
||||
|
||||
ec2Client.monitorInstances(monitorInstancesRequest);
|
||||
|
||||
UnmonitorInstancesRequest unmonitorInstancesRequest = new UnmonitorInstancesRequest()
|
||||
.withInstanceIds(yourInstanceId);
|
||||
|
||||
ec2Client.unmonitorInstances(unmonitorInstancesRequest);
|
||||
|
||||
// Reboot an Instance
|
||||
|
||||
RebootInstancesRequest rebootInstancesRequest = new RebootInstancesRequest()
|
||||
.withInstanceIds(yourInstanceId);
|
||||
|
||||
ec2Client.rebootInstances(rebootInstancesRequest);
|
||||
|
||||
// Stop an Instance
|
||||
StopInstancesRequest stopInstancesRequest = new StopInstancesRequest()
|
||||
.withInstanceIds(yourInstanceId);
|
||||
|
||||
ec2Client.stopInstances(stopInstancesRequest)
|
||||
.getStoppingInstances()
|
||||
.get(0)
|
||||
.getPreviousState()
|
||||
.getName();
|
||||
|
||||
// Describe an Instance
|
||||
DescribeInstancesRequest describeInstancesRequest = new DescribeInstancesRequest();
|
||||
DescribeInstancesResult response = ec2Client.describeInstances(describeInstancesRequest);
|
||||
System.out.println(response.getReservations()
|
||||
.get(0)
|
||||
.getInstances()
|
||||
.get(0)
|
||||
.getKernelId());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,184 @@
|
||||
package com.baeldung.rds;
|
||||
|
||||
import com.amazonaws.auth.AWSCredentialsProvider;
|
||||
import com.amazonaws.auth.AWSStaticCredentialsProvider;
|
||||
import com.amazonaws.auth.BasicAWSCredentials;
|
||||
import com.amazonaws.regions.Regions;
|
||||
import com.amazonaws.services.rds.AmazonRDS;
|
||||
import com.amazonaws.services.rds.AmazonRDSClientBuilder;
|
||||
import com.amazonaws.services.rds.model.*;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.sql.*;
|
||||
import java.util.List;
|
||||
import java.util.Properties;
|
||||
import java.util.UUID;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
public class AWSRDSService {
|
||||
|
||||
|
||||
final static Logger logger = Logger.getLogger(AWSRDSService.class.getName());
|
||||
private AWSCredentialsProvider credentials;
|
||||
private AmazonRDS amazonRDS;
|
||||
private String db_username;
|
||||
private String db_password;
|
||||
private String db_database;
|
||||
private String db_hostname;
|
||||
|
||||
/*
|
||||
* User access key and secret key must be set before execute any operation.
|
||||
* Follow the link on how to get the user access and secret key
|
||||
* https://aws.amazon.com/blogs/security/wheres-my-secret-access-key/
|
||||
* **/
|
||||
public AWSRDSService() throws IOException {
|
||||
//Init RDS client with credentials and region.
|
||||
credentials = new
|
||||
AWSStaticCredentialsProvider(new
|
||||
BasicAWSCredentials("<ACCESS_KEY>",
|
||||
"<SECRET_KEY>"));
|
||||
amazonRDS = AmazonRDSClientBuilder.standard().withCredentials(credentials)
|
||||
.withRegion(Regions.AP_SOUTHEAST_2).build();
|
||||
Properties prop = new Properties();
|
||||
InputStream input = AWSRDSService.class.getClassLoader().getResourceAsStream("db.properties");
|
||||
prop.load(input);
|
||||
db_username = prop.getProperty("db_username");
|
||||
db_password = prop.getProperty("db_password");
|
||||
db_database = prop.getProperty("db_database");
|
||||
}
|
||||
|
||||
public AWSRDSService(AmazonRDS amazonRDS){
|
||||
this.amazonRDS = amazonRDS;
|
||||
}
|
||||
|
||||
/**
|
||||
* create the RDS instance.
|
||||
* After instance creation, update the db_hostname with endpoint in db.properties.
|
||||
* */
|
||||
|
||||
public String launchInstance() {
|
||||
|
||||
String identifier = "";
|
||||
CreateDBInstanceRequest request = new CreateDBInstanceRequest();
|
||||
// RDS instance name
|
||||
request.setDBInstanceIdentifier("Sydney");
|
||||
request.setEngine("postgres");
|
||||
request.setMultiAZ(false);
|
||||
request.setMasterUsername(db_username);
|
||||
request.setMasterUserPassword(db_password);
|
||||
request.setDBName(db_database);
|
||||
request.setStorageType("gp2");
|
||||
request.setAllocatedStorage(10);
|
||||
|
||||
DBInstance instance = amazonRDS.createDBInstance(request);
|
||||
|
||||
// Information about the new RDS instance
|
||||
identifier = instance.getDBInstanceIdentifier();
|
||||
String status = instance.getDBInstanceStatus();
|
||||
Endpoint endpoint = instance.getEndpoint();
|
||||
String endpoint_url = "Endpoint URL not available yet.";
|
||||
if (endpoint != null) {
|
||||
endpoint_url = endpoint.toString();
|
||||
}
|
||||
logger.info(identifier + "\t" + status);
|
||||
logger.info(endpoint_url);
|
||||
|
||||
return identifier;
|
||||
|
||||
}
|
||||
|
||||
// Describe DB instances
|
||||
public void listInstances() {
|
||||
DescribeDBInstancesResult result = amazonRDS.describeDBInstances();
|
||||
List<DBInstance> instances = result.getDBInstances();
|
||||
for (DBInstance instance : instances) {
|
||||
// Information about each RDS instance
|
||||
String identifier = instance.getDBInstanceIdentifier();
|
||||
String engine = instance.getEngine();
|
||||
String status = instance.getDBInstanceStatus();
|
||||
Endpoint endpoint = instance.getEndpoint();
|
||||
String endpoint_url = "Endpoint URL not available yet.";
|
||||
if (endpoint != null) {
|
||||
endpoint_url = endpoint.toString();
|
||||
}
|
||||
logger.info(identifier + "\t" + engine + "\t" + status);
|
||||
logger.info("\t" + endpoint_url);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
//Delete RDS instance
|
||||
public void terminateInstance(String identifier) {
|
||||
|
||||
DeleteDBInstanceRequest request = new DeleteDBInstanceRequest();
|
||||
request.setDBInstanceIdentifier(identifier);
|
||||
request.setSkipFinalSnapshot(true);
|
||||
|
||||
// Delete the RDS instance
|
||||
DBInstance instance = amazonRDS.deleteDBInstance(request);
|
||||
|
||||
// Information about the RDS instance being deleted
|
||||
String status = instance.getDBInstanceStatus();
|
||||
Endpoint endpoint = instance.getEndpoint();
|
||||
String endpoint_url = "Endpoint URL not available yet.";
|
||||
if (endpoint != null) {
|
||||
endpoint_url = endpoint.toString();
|
||||
}
|
||||
|
||||
logger.info(identifier + "\t" + status);
|
||||
logger.info(endpoint_url);
|
||||
|
||||
}
|
||||
|
||||
|
||||
public void runJdbcTests() throws SQLException, IOException {
|
||||
|
||||
// Getting database properties from db.properties
|
||||
Properties prop = new Properties();
|
||||
InputStream input = AWSRDSService.class.getClassLoader().getResourceAsStream("db.properties");
|
||||
prop.load(input);
|
||||
db_hostname = prop.getProperty("db_hostname");
|
||||
String jdbc_url = "jdbc:postgresql://" + db_hostname + ":5432/" + db_database;
|
||||
|
||||
// Create a connection using the JDBC driver
|
||||
try (Connection conn = DriverManager.getConnection(jdbc_url, db_username, db_password)) {
|
||||
|
||||
// Create the test table if not exists
|
||||
Statement statement = conn.createStatement();
|
||||
String sql = "CREATE TABLE IF NOT EXISTS jdbc_test (id SERIAL PRIMARY KEY, content VARCHAR(80))";
|
||||
statement.executeUpdate(sql);
|
||||
|
||||
// Do some INSERT
|
||||
PreparedStatement preparedStatement = conn.prepareStatement("INSERT INTO jdbc_test (content) VALUES (?)");
|
||||
String content = "" + UUID.randomUUID();
|
||||
preparedStatement.setString(1, content);
|
||||
preparedStatement.executeUpdate();
|
||||
logger.info("INSERT: " + content);
|
||||
|
||||
// Do some SELECT
|
||||
sql = "SELECT * FROM jdbc_test";
|
||||
ResultSet resultSet = statement.executeQuery(sql);
|
||||
while (resultSet.next()) {
|
||||
String count = resultSet.getString("content");
|
||||
logger.info("Total Records: " + count);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws IOException, SQLException {
|
||||
AWSRDSService awsrdsService = new AWSRDSService();
|
||||
|
||||
String instanceName = awsrdsService.launchInstance();
|
||||
|
||||
//Add some wait for instance creation.
|
||||
|
||||
awsrdsService.listInstances();
|
||||
|
||||
awsrdsService.runJdbcTests();
|
||||
|
||||
awsrdsService.terminateInstance(instanceName);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,146 @@
|
||||
package com.baeldung.sqs;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import com.amazonaws.auth.AWSCredentials;
|
||||
import com.amazonaws.auth.AWSStaticCredentialsProvider;
|
||||
import com.amazonaws.auth.BasicAWSCredentials;
|
||||
import com.amazonaws.regions.Regions;
|
||||
import com.amazonaws.services.sqs.AmazonSQSClientBuilder;
|
||||
import com.amazonaws.services.sqs.model.CreateQueueRequest;
|
||||
import com.amazonaws.services.sqs.model.DeleteMessageRequest;
|
||||
import com.amazonaws.services.sqs.model.GetQueueAttributesRequest;
|
||||
import com.amazonaws.services.sqs.model.GetQueueAttributesResult;
|
||||
import com.amazonaws.services.sqs.model.MessageAttributeValue;
|
||||
import com.amazonaws.services.sqs.model.ReceiveMessageRequest;
|
||||
import com.amazonaws.services.sqs.model.SendMessageBatchRequest;
|
||||
import com.amazonaws.services.sqs.model.SendMessageRequest;
|
||||
import com.amazonaws.services.sqs.model.SetQueueAttributesRequest;
|
||||
import com.amazonaws.services.sqs.model.SendMessageBatchRequestEntry;
|
||||
import com.amazonaws.services.sqs.model.Message;
|
||||
import com.amazonaws.services.sqs.AmazonSQS;
|
||||
|
||||
public class SQSApplication {
|
||||
|
||||
private static final AWSCredentials credentials;
|
||||
|
||||
static {
|
||||
// put your accesskey and secretkey here
|
||||
credentials = new BasicAWSCredentials(
|
||||
"<AWS accesskey>",
|
||||
"<AWS secretkey>"
|
||||
);
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
// Set up the client
|
||||
AmazonSQS sqs = AmazonSQSClientBuilder.standard()
|
||||
.withCredentials(new AWSStaticCredentialsProvider(credentials))
|
||||
.withRegion(Regions.US_EAST_1)
|
||||
.build();
|
||||
|
||||
// Create a standard queue
|
||||
|
||||
CreateQueueRequest createStandardQueueRequest = new CreateQueueRequest("baeldung-queue");
|
||||
String standardQueueUrl = sqs.createQueue(createStandardQueueRequest)
|
||||
.getQueueUrl();
|
||||
|
||||
System.out.println(standardQueueUrl);
|
||||
|
||||
// Create a fifo queue
|
||||
|
||||
Map<String, String> queueAttributes = new HashMap<String, String>();
|
||||
queueAttributes.put("FifoQueue", "true");
|
||||
queueAttributes.put("ContentBasedDeduplication", "true");
|
||||
|
||||
CreateQueueRequest createFifoQueueRequest = new CreateQueueRequest("baeldung-queue.fifo").withAttributes(queueAttributes);
|
||||
String fifoQueueUrl = sqs.createQueue(createFifoQueueRequest)
|
||||
.getQueueUrl();
|
||||
|
||||
System.out.println(fifoQueueUrl);
|
||||
|
||||
// Set up a dead letter queue
|
||||
|
||||
String deadLetterQueueUrl = sqs.createQueue("baeldung-dead-letter-queue")
|
||||
.getQueueUrl();
|
||||
|
||||
GetQueueAttributesResult deadLetterQueueAttributes = sqs.getQueueAttributes(new GetQueueAttributesRequest(deadLetterQueueUrl).withAttributeNames("QueueArn"));
|
||||
|
||||
String deadLetterQueueARN = deadLetterQueueAttributes.getAttributes()
|
||||
.get("QueueArn");
|
||||
|
||||
SetQueueAttributesRequest queueAttributesRequest = new SetQueueAttributesRequest().withQueueUrl(standardQueueUrl)
|
||||
.addAttributesEntry("RedrivePolicy", "{\"maxReceiveCount\":\"2\", " + "\"deadLetterTargetArn\":\"" + deadLetterQueueARN + "\"}");
|
||||
|
||||
sqs.setQueueAttributes(queueAttributesRequest);
|
||||
|
||||
// Send a message to a standard queue
|
||||
|
||||
Map<String, MessageAttributeValue> messageAttributes = new HashMap<>();
|
||||
|
||||
messageAttributes.put("AttributeOne", new MessageAttributeValue().withStringValue("This is an attribute")
|
||||
.withDataType("String"));
|
||||
|
||||
SendMessageRequest sendMessageStandardQueue = new SendMessageRequest().withQueueUrl(standardQueueUrl)
|
||||
.withMessageBody("A simple message.")
|
||||
.withDelaySeconds(30) // Message will arrive in the queue after 30 seconds. We can use this only in standard queues
|
||||
.withMessageAttributes(messageAttributes);
|
||||
|
||||
sqs.sendMessage(sendMessageStandardQueue);
|
||||
|
||||
// Send a message to a fifo queue
|
||||
|
||||
SendMessageRequest sendMessageFifoQueue = new SendMessageRequest().withQueueUrl(fifoQueueUrl)
|
||||
.withMessageBody("FIFO Queue")
|
||||
.withMessageGroupId("baeldung-group-1")
|
||||
.withMessageAttributes(messageAttributes);
|
||||
|
||||
sqs.sendMessage(sendMessageFifoQueue);
|
||||
|
||||
// Send multiple messages
|
||||
|
||||
List<SendMessageBatchRequestEntry> messageEntries = new ArrayList<>();
|
||||
messageEntries.add(new SendMessageBatchRequestEntry().withId("id-1")
|
||||
.withMessageBody("batch-1")
|
||||
.withMessageGroupId("baeldung-group-1"));
|
||||
messageEntries.add(new SendMessageBatchRequestEntry().withId("id-2")
|
||||
.withMessageBody("batch-2")
|
||||
.withMessageGroupId("baeldung-group-1"));
|
||||
|
||||
SendMessageBatchRequest sendMessageBatchRequest = new SendMessageBatchRequest(fifoQueueUrl, messageEntries);
|
||||
sqs.sendMessageBatch(sendMessageBatchRequest);
|
||||
|
||||
// Read a message from a queue
|
||||
|
||||
ReceiveMessageRequest receiveMessageRequest = new ReceiveMessageRequest(fifoQueueUrl).withWaitTimeSeconds(10) // Long polling;
|
||||
.withMaxNumberOfMessages(1); // Max is 10
|
||||
|
||||
List<Message> sqsMessages = sqs.receiveMessage(receiveMessageRequest)
|
||||
.getMessages();
|
||||
|
||||
sqsMessages.get(0)
|
||||
.getAttributes();
|
||||
sqsMessages.get(0)
|
||||
.getBody();
|
||||
|
||||
// Delete a message from a queue
|
||||
|
||||
sqs.deleteMessage(new DeleteMessageRequest().withQueueUrl(fifoQueueUrl)
|
||||
.withReceiptHandle(sqsMessages.get(0)
|
||||
.getReceiptHandle()));
|
||||
|
||||
// Monitoring
|
||||
GetQueueAttributesRequest getQueueAttributesRequest = new GetQueueAttributesRequest(standardQueueUrl).withAttributeNames("All");
|
||||
GetQueueAttributesResult getQueueAttributesResult = sqs.getQueueAttributes(getQueueAttributesRequest);
|
||||
System.out.println(String.format("The number of messages on the queue: %s", getQueueAttributesResult.getAttributes()
|
||||
.get("ApproximateNumberOfMessages")));
|
||||
System.out.println(String.format("The number of messages in flight: %s", getQueueAttributesResult.getAttributes()
|
||||
.get("ApproximateNumberOfMessagesNotVisible")));
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
db_hostname=<RDS EndPoint>
|
||||
db_username=username
|
||||
db_password=password
|
||||
db_database=mydb
|
||||
@@ -0,0 +1,13 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<configuration>
|
||||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<encoder>
|
||||
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
|
||||
</pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<root level="INFO">
|
||||
<appender-ref ref="STDOUT" />
|
||||
</root>
|
||||
</configuration>
|
||||
+120
@@ -0,0 +1,120 @@
|
||||
package com.baeldung.dynamodb;
|
||||
|
||||
import com.amazonaws.auth.BasicAWSCredentials;
|
||||
import com.amazonaws.services.dynamodbv2.AmazonDynamoDB;
|
||||
import com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient;
|
||||
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapper;
|
||||
import com.amazonaws.services.dynamodbv2.model.CreateTableRequest;
|
||||
import com.amazonaws.services.dynamodbv2.model.ProvisionedThroughput;
|
||||
import com.amazonaws.services.dynamodbv2.model.ResourceInUseException;
|
||||
import com.baeldung.dynamodb.entity.ProductInfo;
|
||||
import com.baeldung.dynamodb.repository.ProductInfoRepository;
|
||||
import com.baeldung.dynamodb.rule.LocalDbCreationRule;
|
||||
import org.junit.Before;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.ClassRule;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.Properties;
|
||||
|
||||
import static org.hamcrest.Matchers.greaterThan;
|
||||
import static org.hamcrest.core.Is.is;
|
||||
import static org.hamcrest.core.IsEqual.equalTo;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
public class ProductInfoRepositoryIntegrationTest {
|
||||
|
||||
@ClassRule
|
||||
public static LocalDbCreationRule dynamoDB = new LocalDbCreationRule();
|
||||
|
||||
private static DynamoDBMapper dynamoDBMapper;
|
||||
private static AmazonDynamoDB amazonDynamoDB;
|
||||
|
||||
private ProductInfoRepository repository;
|
||||
|
||||
private static final String DYNAMODB_ENDPOINT = "amazon.dynamodb.endpoint";
|
||||
private static final String AWS_ACCESSKEY = "amazon.aws.accesskey";
|
||||
private static final String AWS_SECRETKEY = "amazon.aws.secretkey";
|
||||
|
||||
private static final String EXPECTED_COST = "20";
|
||||
private static final String EXPECTED_PRICE = "50";
|
||||
|
||||
@BeforeClass
|
||||
public static void setupClass() {
|
||||
Properties testProperties = loadFromFileInClasspath("test.properties")
|
||||
.filter(properties -> !isEmpty(properties.getProperty(AWS_ACCESSKEY)))
|
||||
.filter(properties -> !isEmpty(properties.getProperty(AWS_SECRETKEY)))
|
||||
.filter(properties -> !isEmpty(properties.getProperty(DYNAMODB_ENDPOINT)))
|
||||
.orElseThrow(() -> new RuntimeException("Unable to get all of the required test property values"));
|
||||
|
||||
String amazonAWSAccessKey = testProperties.getProperty(AWS_ACCESSKEY);
|
||||
String amazonAWSSecretKey = testProperties.getProperty(AWS_SECRETKEY);
|
||||
String amazonDynamoDBEndpoint = testProperties.getProperty(DYNAMODB_ENDPOINT);
|
||||
|
||||
amazonDynamoDB = new AmazonDynamoDBClient(new BasicAWSCredentials(amazonAWSAccessKey, amazonAWSSecretKey));
|
||||
amazonDynamoDB.setEndpoint(amazonDynamoDBEndpoint);
|
||||
dynamoDBMapper = new DynamoDBMapper(amazonDynamoDB);
|
||||
}
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
try {
|
||||
repository = new ProductInfoRepository();
|
||||
repository.setMapper(dynamoDBMapper);
|
||||
|
||||
CreateTableRequest tableRequest = dynamoDBMapper.generateCreateTableRequest(ProductInfo.class);
|
||||
|
||||
tableRequest.setProvisionedThroughput(new ProvisionedThroughput(1L, 1L));
|
||||
|
||||
amazonDynamoDB.createTable(tableRequest);
|
||||
} catch (ResourceInUseException e) {
|
||||
// Do nothing, table already created
|
||||
}
|
||||
|
||||
// TODO How to handle different environments. i.e. AVOID deleting all entries in ProductInfo on table
|
||||
dynamoDBMapper.batchDelete((List<ProductInfo>) repository.findAll());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenItemWithExpectedCost_whenRunFindAll_thenItemIsFound() {
|
||||
|
||||
ProductInfo productInfo = new ProductInfo(EXPECTED_COST, EXPECTED_PRICE);
|
||||
repository.save(productInfo);
|
||||
|
||||
List<ProductInfo> result = (List<ProductInfo>) repository.findAll();
|
||||
assertThat(result.size(), is(greaterThan(0)));
|
||||
assertThat(result.get(0).getCost(), is(equalTo(EXPECTED_COST)));
|
||||
}
|
||||
|
||||
private static boolean isEmpty(String inputString) {
|
||||
return inputString == null || "".equals(inputString);
|
||||
}
|
||||
|
||||
private static Optional<Properties> loadFromFileInClasspath(String fileName) {
|
||||
InputStream stream = null;
|
||||
try {
|
||||
Properties config = new Properties();
|
||||
Path configLocation = Paths.get(ClassLoader.getSystemResource(fileName).toURI());
|
||||
stream = Files.newInputStream(configLocation);
|
||||
config.load(stream);
|
||||
return Optional.of(config);
|
||||
} catch (Exception e) {
|
||||
return Optional.empty();
|
||||
} finally {
|
||||
if (stream != null) {
|
||||
try {
|
||||
stream.close();
|
||||
} catch (IOException e) {
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
+35
@@ -0,0 +1,35 @@
|
||||
package com.baeldung.dynamodb.rule;
|
||||
|
||||
import com.amazonaws.services.dynamodbv2.local.main.ServerRunner;
|
||||
import com.amazonaws.services.dynamodbv2.local.server.DynamoDBProxyServer;
|
||||
import org.junit.rules.ExternalResource;
|
||||
|
||||
public class LocalDbCreationRule extends ExternalResource {
|
||||
|
||||
protected DynamoDBProxyServer server;
|
||||
|
||||
public LocalDbCreationRule() {
|
||||
System.setProperty("sqlite4java.library.path", "native-libs");
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void before() throws Exception {
|
||||
String port = "8000";
|
||||
this.server = ServerRunner.createServerFromCommandLineArgs(new String[]{"-inMemory","-sharedDb" ,"-port", port});
|
||||
server.start();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void after() {
|
||||
this.stopUnchecked(server);
|
||||
}
|
||||
|
||||
protected void stopUnchecked(DynamoDBProxyServer dynamoDbServer) {
|
||||
try {
|
||||
dynamoDbServer.stop();
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
amazon.dynamodb.endpoint=http://localhost:8000/
|
||||
amazon.aws.accesskey=key
|
||||
amazon.aws.secretkey=key2
|
||||
@@ -0,0 +1,7 @@
|
||||
## AWS Reactive
|
||||
|
||||
This module contains articles about reactive support with AWS
|
||||
|
||||
### Relevant Articles:
|
||||
|
||||
- [AWS S3 with Java – Reactive Support](https://www.baeldung.com/java-aws-s3-reactive)
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 22 KiB |
@@ -0,0 +1,29 @@
|
||||
participant "Client 1" as C1
|
||||
participant "Client 2" as C2
|
||||
participant "Reactive Web App" as RWS
|
||||
participant "Backend" as S3
|
||||
C1 -> RWS: POST
|
||||
activate C1
|
||||
activate RWS
|
||||
RWS -> S3: Async POST
|
||||
deactivate RWS
|
||||
C2 -> RWS: POST
|
||||
activate C2
|
||||
activate RWS
|
||||
RWS -> S3: Async POST
|
||||
deactivate RWS
|
||||
S3 --> RWS: Async Result
|
||||
activate RWS
|
||||
RWS -->C2: Result
|
||||
deactivate RWS
|
||||
deactivate C2
|
||||
// First file EOF
|
||||
S3 --> RWS: Async Result
|
||||
activate RWS
|
||||
RWS -->C1: Result
|
||||
deactivate RWS
|
||||
deactivate C1
|
||||
|
||||
|
||||
|
||||
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 31 KiB |
@@ -0,0 +1,28 @@
|
||||
participant Client 1
|
||||
participant Client 2
|
||||
participant Controller
|
||||
participant Backend
|
||||
Client 1-> Controller: POST Data
|
||||
activate Client 1
|
||||
activate Controller
|
||||
Controller -> Backend: Save Data
|
||||
activate Backend
|
||||
note left of Controller #yellow: Controller blocked\nuntil result received
|
||||
Backend --> Controller: Result
|
||||
deactivate Backend
|
||||
Controller --> Client 1: Result
|
||||
deactivate Client 1
|
||||
deactivate Controller
|
||||
// 2nd Upload
|
||||
Client 2-> Controller: POST Data
|
||||
activate Client 2
|
||||
activate Controller
|
||||
Controller -> Backend: Save Data
|
||||
activate Backend
|
||||
note left of Controller #yellow: Controller blocket\nuntil result received
|
||||
Backend --> Controller: Result
|
||||
deactivate Backend
|
||||
Controller --> Client 2: Result
|
||||
deactivate Controller
|
||||
deactivate Client 2
|
||||
|
||||
@@ -0,0 +1,97 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>aws-reactive</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<name>aws-reactive</name>
|
||||
<description>AWS Reactive Sample</description>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>aws-modules</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<dependencyManagement>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<!-- Import dependency management from Spring Boot -->
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-dependencies</artifactId>
|
||||
<version>${spring.version}</version>
|
||||
<type>pom</type>
|
||||
<scope>import</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>software.amazon.awssdk</groupId>
|
||||
<artifactId>bom</artifactId>
|
||||
<version>${awssdk.version}</version>
|
||||
<type>pom</type>
|
||||
<scope>import</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</dependencyManagement>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-webflux</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>software.amazon.awssdk</groupId>
|
||||
<artifactId>s3</artifactId>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<artifactId>netty-nio-client</artifactId>
|
||||
<groupId>software.amazon.awssdk</groupId>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
<exclusions>
|
||||
<exclusion>
|
||||
<groupId>org.junit.vintage</groupId>
|
||||
<artifactId>junit-vintage-engine</artifactId>
|
||||
</exclusion>
|
||||
</exclusions>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.projectreactor</groupId>
|
||||
<artifactId>reactor-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-devtools</artifactId>
|
||||
<scope>runtime</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-configuration-processor</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
<properties>
|
||||
<spring.version>2.2.1.RELEASE</spring.version>
|
||||
<awssdk.version>2.10.27</awssdk.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
+32
@@ -0,0 +1,32 @@
|
||||
package com.baeldung.aws.reactive.s3;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
import org.springframework.http.HttpStatus;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import software.amazon.awssdk.core.SdkResponse;
|
||||
import software.amazon.awssdk.http.SdkHttpResponse;
|
||||
|
||||
@AllArgsConstructor
|
||||
public class DownloadFailedException extends RuntimeException {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
private int statusCode;
|
||||
private Optional<String> statusText;
|
||||
|
||||
public DownloadFailedException(SdkResponse response) {
|
||||
|
||||
SdkHttpResponse httpResponse = response.sdkHttpResponse();
|
||||
if (httpResponse != null) {
|
||||
this.statusCode = httpResponse.statusCode();
|
||||
this.statusText = httpResponse.statusText();
|
||||
} else {
|
||||
this.statusCode = HttpStatus.INTERNAL_SERVER_ERROR.value();
|
||||
this.statusText = Optional.of("UNKNOWN");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
+144
@@ -0,0 +1,144 @@
|
||||
/**
|
||||
*
|
||||
*/
|
||||
package com.baeldung.aws.reactive.s3;
|
||||
|
||||
import java.io.InputStream;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
|
||||
import org.springframework.core.io.buffer.DataBuffer;
|
||||
import org.springframework.core.io.buffer.DataBufferUtils;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.http.ResponseEntity.BodyBuilder;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import reactor.core.publisher.Flux;
|
||||
import reactor.core.publisher.Mono;
|
||||
import software.amazon.awssdk.core.ResponseBytes;
|
||||
import software.amazon.awssdk.core.SdkResponse;
|
||||
import software.amazon.awssdk.core.async.AsyncResponseTransformer;
|
||||
import software.amazon.awssdk.core.async.SdkPublisher;
|
||||
import software.amazon.awssdk.core.internal.async.ByteArrayAsyncResponseTransformer;
|
||||
import software.amazon.awssdk.http.SdkHttpResponse;
|
||||
import software.amazon.awssdk.services.s3.S3AsyncClient;
|
||||
import software.amazon.awssdk.services.s3.model.GetObjectRequest;
|
||||
import software.amazon.awssdk.services.s3.model.GetObjectResponse;
|
||||
|
||||
/**
|
||||
* @author Philippe
|
||||
*
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping("/inbox")
|
||||
@Slf4j
|
||||
public class DownloadResource {
|
||||
|
||||
|
||||
private final S3AsyncClient s3client;
|
||||
private final S3ClientConfigurarionProperties s3config;
|
||||
|
||||
public DownloadResource(S3AsyncClient s3client, S3ClientConfigurarionProperties s3config) {
|
||||
this.s3client = s3client;
|
||||
this.s3config = s3config;
|
||||
}
|
||||
|
||||
|
||||
@GetMapping(path="/{filekey}")
|
||||
public Mono<ResponseEntity<Flux<ByteBuffer>>> downloadFile(@PathVariable("filekey") String filekey) {
|
||||
|
||||
GetObjectRequest request = GetObjectRequest.builder()
|
||||
.bucket(s3config.getBucket())
|
||||
.key(filekey)
|
||||
.build();
|
||||
|
||||
return Mono.fromFuture(s3client.getObject(request,new FluxResponseProvider()))
|
||||
.map( (response) -> {
|
||||
checkResult(response.sdkResponse);
|
||||
String filename = getMetadataItem(response.sdkResponse,"filename",filekey);
|
||||
|
||||
log.info("[I65] filename={}, length={}",filename, response.sdkResponse.contentLength() );
|
||||
|
||||
return ResponseEntity.ok()
|
||||
.header(HttpHeaders.CONTENT_TYPE, response.sdkResponse.contentType())
|
||||
.header(HttpHeaders.CONTENT_LENGTH, Long.toString(response.sdkResponse.contentLength()))
|
||||
.header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + filename + "\"")
|
||||
.body(response.flux);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Lookup a metadata key in a case-insensitive way.
|
||||
* @param sdkResponse
|
||||
* @param key
|
||||
* @param defaultValue
|
||||
* @return
|
||||
*/
|
||||
private String getMetadataItem(GetObjectResponse sdkResponse, String key, String defaultValue) {
|
||||
for( Entry<String, String> entry : sdkResponse.metadata().entrySet()) {
|
||||
if ( entry.getKey().equalsIgnoreCase(key)) {
|
||||
return entry.getValue();
|
||||
}
|
||||
}
|
||||
return defaultValue;
|
||||
}
|
||||
|
||||
|
||||
// Helper used to check return codes from an API call
|
||||
private static void checkResult(GetObjectResponse response) {
|
||||
SdkHttpResponse sdkResponse = response.sdkHttpResponse();
|
||||
if ( sdkResponse != null && sdkResponse.isSuccessful()) {
|
||||
return;
|
||||
}
|
||||
|
||||
throw new DownloadFailedException(response);
|
||||
}
|
||||
|
||||
|
||||
static class FluxResponseProvider implements AsyncResponseTransformer<GetObjectResponse,FluxResponse> {
|
||||
|
||||
private FluxResponse response;
|
||||
|
||||
@Override
|
||||
public CompletableFuture<FluxResponse> prepare() {
|
||||
response = new FluxResponse();
|
||||
return response.cf;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onResponse(GetObjectResponse sdkResponse) {
|
||||
this.response.sdkResponse = sdkResponse;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onStream(SdkPublisher<ByteBuffer> publisher) {
|
||||
response.flux = Flux.from(publisher);
|
||||
response.cf.complete(response);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void exceptionOccurred(Throwable error) {
|
||||
response.cf.completeExceptionally(error);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Holds the API response and stream
|
||||
* @author Philippe
|
||||
*/
|
||||
static class FluxResponse {
|
||||
|
||||
final CompletableFuture<FluxResponse> cf = new CompletableFuture<>();
|
||||
GetObjectResponse sdkResponse;
|
||||
Flux<ByteBuffer> flux;
|
||||
}
|
||||
|
||||
}
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
package com.baeldung.aws.reactive.s3;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class ReactiveS3Application {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(ReactiveS3Application.class, args);
|
||||
}
|
||||
|
||||
}
|
||||
+28
@@ -0,0 +1,28 @@
|
||||
package com.baeldung.aws.reactive.s3;
|
||||
|
||||
import java.net.URI;
|
||||
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
|
||||
import lombok.Data;
|
||||
import software.amazon.awssdk.regions.Region;
|
||||
|
||||
@ConfigurationProperties(prefix = "aws.s3")
|
||||
@Data
|
||||
public class S3ClientConfigurarionProperties {
|
||||
|
||||
private Region region = Region.US_EAST_1;
|
||||
private URI endpoint = null;
|
||||
|
||||
private String accessKeyId;
|
||||
private String secretAccessKey;
|
||||
|
||||
// Bucket name we'll be using as our backend storage
|
||||
private String bucket;
|
||||
|
||||
// AWS S3 requires that file parts must have at least 5MB, except
|
||||
// for the last part. This may change for other S3-compatible services, so let't
|
||||
// define a configuration property for that
|
||||
private int multipartMinPartSize = 5*1024*1024;
|
||||
|
||||
}
|
||||
+65
@@ -0,0 +1,65 @@
|
||||
package com.baeldung.aws.reactive.s3;
|
||||
|
||||
import java.time.Duration;
|
||||
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
import software.amazon.awssdk.auth.credentials.AwsBasicCredentials;
|
||||
import software.amazon.awssdk.auth.credentials.AwsCredentials;
|
||||
import software.amazon.awssdk.auth.credentials.AwsCredentialsProvider;
|
||||
import software.amazon.awssdk.auth.credentials.DefaultCredentialsProvider;
|
||||
import software.amazon.awssdk.http.async.SdkAsyncHttpClient;
|
||||
import software.amazon.awssdk.http.nio.netty.NettyNioAsyncHttpClient;
|
||||
import software.amazon.awssdk.services.s3.S3AsyncClient;
|
||||
import software.amazon.awssdk.services.s3.S3AsyncClientBuilder;
|
||||
import software.amazon.awssdk.services.s3.S3Configuration;
|
||||
import software.amazon.awssdk.utils.StringUtils;
|
||||
|
||||
@Configuration
|
||||
@EnableConfigurationProperties(S3ClientConfigurarionProperties.class)
|
||||
public class S3ClientConfiguration {
|
||||
|
||||
@Bean
|
||||
public S3AsyncClient s3client(S3ClientConfigurarionProperties s3props, AwsCredentialsProvider credentialsProvider) {
|
||||
|
||||
SdkAsyncHttpClient httpClient = NettyNioAsyncHttpClient.builder()
|
||||
.writeTimeout(Duration.ZERO)
|
||||
.maxConcurrency(64)
|
||||
.build();
|
||||
|
||||
S3Configuration serviceConfiguration = S3Configuration.builder()
|
||||
.checksumValidationEnabled(false)
|
||||
.chunkedEncodingEnabled(true)
|
||||
.build();
|
||||
|
||||
S3AsyncClientBuilder b = S3AsyncClient.builder()
|
||||
.httpClient(httpClient)
|
||||
.region(s3props.getRegion())
|
||||
.credentialsProvider(credentialsProvider)
|
||||
.serviceConfiguration(serviceConfiguration);
|
||||
|
||||
if (s3props.getEndpoint() != null) {
|
||||
b = b.endpointOverride(s3props.getEndpoint());
|
||||
}
|
||||
|
||||
return b.build();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public AwsCredentialsProvider awsCredentialsProvider(S3ClientConfigurarionProperties s3props) {
|
||||
|
||||
if (StringUtils.isBlank(s3props.getAccessKeyId())) {
|
||||
// Return default provider
|
||||
return DefaultCredentialsProvider.create();
|
||||
}
|
||||
else {
|
||||
// Return custom credentials provider
|
||||
return () -> {
|
||||
AwsCredentials creds = AwsBasicCredentials.create(s3props.getAccessKeyId(), s3props.getSecretAccessKey());
|
||||
return creds;
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
+32
@@ -0,0 +1,32 @@
|
||||
package com.baeldung.aws.reactive.s3;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
import org.springframework.http.HttpStatus;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import software.amazon.awssdk.core.SdkResponse;
|
||||
import software.amazon.awssdk.http.SdkHttpResponse;
|
||||
|
||||
@AllArgsConstructor
|
||||
public class UploadFailedException extends RuntimeException {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
private int statusCode;
|
||||
private Optional<String> statusText;
|
||||
|
||||
public UploadFailedException(SdkResponse response) {
|
||||
|
||||
SdkHttpResponse httpResponse = response.sdkHttpResponse();
|
||||
if (httpResponse != null) {
|
||||
this.statusCode = httpResponse.statusCode();
|
||||
this.statusText = httpResponse.statusText();
|
||||
} else {
|
||||
this.statusCode = HttpStatus.INTERNAL_SERVER_ERROR.value();
|
||||
this.statusText = Optional.of("UNKNOWN");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
+308
@@ -0,0 +1,308 @@
|
||||
/**
|
||||
*
|
||||
*/
|
||||
package com.baeldung.aws.reactive.s3;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
import java.util.stream.Collector;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import org.reactivestreams.Publisher;
|
||||
import org.springframework.core.io.buffer.DataBuffer;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.http.codec.multipart.FilePart;
|
||||
import org.springframework.http.codec.multipart.Part;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.PutMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestHeader;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMethod;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import lombok.Data;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import reactor.core.publisher.Flux;
|
||||
import reactor.core.publisher.Mono;
|
||||
import software.amazon.awssdk.core.SdkResponse;
|
||||
import software.amazon.awssdk.core.async.AsyncRequestBody;
|
||||
import software.amazon.awssdk.services.s3.S3AsyncClient;
|
||||
import software.amazon.awssdk.services.s3.model.CompleteMultipartUploadRequest;
|
||||
import software.amazon.awssdk.services.s3.model.CompleteMultipartUploadResponse;
|
||||
import software.amazon.awssdk.services.s3.model.CompletedMultipartUpload;
|
||||
import software.amazon.awssdk.services.s3.model.CompletedMultipartUpload.Builder;
|
||||
import software.amazon.awssdk.services.s3.model.CompletedPart;
|
||||
import software.amazon.awssdk.services.s3.model.CreateMultipartUploadRequest;
|
||||
import software.amazon.awssdk.services.s3.model.CreateMultipartUploadResponse;
|
||||
import software.amazon.awssdk.services.s3.model.PutObjectRequest;
|
||||
import software.amazon.awssdk.services.s3.model.PutObjectResponse;
|
||||
import software.amazon.awssdk.services.s3.model.UploadPartRequest;
|
||||
import software.amazon.awssdk.services.s3.model.UploadPartResponse;
|
||||
|
||||
/**
|
||||
* @author Philippe
|
||||
*
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping("/inbox")
|
||||
@Slf4j
|
||||
public class UploadResource {
|
||||
|
||||
private final S3AsyncClient s3client;
|
||||
private final S3ClientConfigurarionProperties s3config;
|
||||
|
||||
public UploadResource(S3AsyncClient s3client, S3ClientConfigurarionProperties s3config) {
|
||||
this.s3client = s3client;
|
||||
this.s3config = s3config;
|
||||
}
|
||||
|
||||
/**
|
||||
* Standard file upload.
|
||||
*/
|
||||
@PostMapping
|
||||
public Mono<ResponseEntity<UploadResult>> uploadHandler(@RequestHeader HttpHeaders headers, @RequestBody Flux<ByteBuffer> body) {
|
||||
|
||||
long length = headers.getContentLength();
|
||||
if (length < 0) {
|
||||
throw new UploadFailedException(HttpStatus.BAD_REQUEST.value(), Optional.of("required header missing: Content-Length"));
|
||||
}
|
||||
|
||||
String fileKey = UUID.randomUUID().toString();
|
||||
Map<String, String> metadata = new HashMap<String, String>();
|
||||
MediaType mediaType = headers.getContentType();
|
||||
|
||||
if (mediaType == null) {
|
||||
mediaType = MediaType.APPLICATION_OCTET_STREAM;
|
||||
}
|
||||
|
||||
log.info("[I95] uploadHandler: mediaType{}, length={}", mediaType, length);
|
||||
CompletableFuture<PutObjectResponse> future = s3client
|
||||
.putObject(PutObjectRequest.builder()
|
||||
.bucket(s3config.getBucket())
|
||||
.contentLength(length)
|
||||
.key(fileKey.toString())
|
||||
.contentType(mediaType.toString())
|
||||
.metadata(metadata)
|
||||
.build(),
|
||||
AsyncRequestBody.fromPublisher(body));
|
||||
|
||||
return Mono.fromFuture(future)
|
||||
.map((response) -> {
|
||||
checkResult(response);
|
||||
return ResponseEntity
|
||||
.status(HttpStatus.CREATED)
|
||||
.body(new UploadResult(HttpStatus.CREATED, new String[] {fileKey}));
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Multipart file upload
|
||||
* @param bucket
|
||||
* @param parts
|
||||
* @param headers
|
||||
* @return
|
||||
*/
|
||||
@RequestMapping(consumes = MediaType.MULTIPART_FORM_DATA_VALUE, method = {RequestMethod.POST, RequestMethod.PUT})
|
||||
public Mono<ResponseEntity<UploadResult>> multipartUploadHandler(@RequestHeader HttpHeaders headers, @RequestBody Flux<Part> parts ) {
|
||||
|
||||
return parts
|
||||
.ofType(FilePart.class) // We'll ignore other data for now
|
||||
.flatMap((part) -> saveFile(headers, s3config.getBucket(), part))
|
||||
.collect(Collectors.toList())
|
||||
.map((keys) -> ResponseEntity.status(HttpStatus.CREATED)
|
||||
.body(new UploadResult(HttpStatus.CREATED,keys)));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Save file using a multipart upload. This method does not require any temporary
|
||||
* storage at the REST service
|
||||
* @param headers
|
||||
* @param bucket Bucket name
|
||||
* @param part Uploaded file
|
||||
* @return
|
||||
*/
|
||||
protected Mono<String> saveFile(HttpHeaders headers,String bucket, FilePart part) {
|
||||
|
||||
// Generate a filekey for this upload
|
||||
String filekey = UUID.randomUUID().toString();
|
||||
|
||||
log.info("[I137] saveFile: filekey={}, filename={}", filekey, part.filename());
|
||||
|
||||
// Gather metadata
|
||||
Map<String, String> metadata = new HashMap<String, String>();
|
||||
String filename = part.filename();
|
||||
if ( filename == null ) {
|
||||
filename = filekey;
|
||||
}
|
||||
|
||||
metadata.put("filename", filename);
|
||||
|
||||
MediaType mt = part.headers().getContentType();
|
||||
if ( mt == null ) {
|
||||
mt = MediaType.APPLICATION_OCTET_STREAM;
|
||||
}
|
||||
|
||||
// Create multipart upload request
|
||||
CompletableFuture<CreateMultipartUploadResponse> uploadRequest = s3client
|
||||
.createMultipartUpload(CreateMultipartUploadRequest.builder()
|
||||
.contentType(mt.toString())
|
||||
.key(filekey)
|
||||
.metadata(metadata)
|
||||
.bucket(bucket)
|
||||
.build());
|
||||
|
||||
// This variable will hold the upload state that we must keep
|
||||
// around until all uploads complete
|
||||
final UploadState uploadState = new UploadState(bucket,filekey);
|
||||
|
||||
return Mono
|
||||
.fromFuture(uploadRequest)
|
||||
.flatMapMany((response) -> {
|
||||
checkResult(response);
|
||||
uploadState.uploadId = response.uploadId();
|
||||
log.info("[I183] uploadId={}", response.uploadId());
|
||||
return part.content();
|
||||
})
|
||||
.bufferUntil((buffer) -> {
|
||||
uploadState.buffered += buffer.readableByteCount();
|
||||
if ( uploadState.buffered >= s3config.getMultipartMinPartSize() ) {
|
||||
log.info("[I173] bufferUntil: returning true, bufferedBytes={}, partCounter={}, uploadId={}", uploadState.buffered, uploadState.partCounter, uploadState.uploadId);
|
||||
uploadState.buffered = 0;
|
||||
return true;
|
||||
}
|
||||
else {
|
||||
return false;
|
||||
}
|
||||
})
|
||||
.map((buffers) -> concatBuffers(buffers))
|
||||
.flatMap((buffer) -> uploadPart(uploadState,buffer))
|
||||
.onBackpressureBuffer()
|
||||
.reduce(uploadState,(state,completedPart) -> {
|
||||
log.info("[I188] completed: partNumber={}, etag={}", completedPart.partNumber(), completedPart.eTag());
|
||||
state.completedParts.put(completedPart.partNumber(), completedPart);
|
||||
return state;
|
||||
})
|
||||
.flatMap((state) -> completeUpload(state))
|
||||
.map((response) -> {
|
||||
checkResult(response);
|
||||
return uploadState.filekey;
|
||||
});
|
||||
}
|
||||
|
||||
private static ByteBuffer concatBuffers(List<DataBuffer> buffers) {
|
||||
log.info("[I198] creating BytBuffer from {} chunks", buffers.size());
|
||||
|
||||
int partSize = 0;
|
||||
for( DataBuffer b : buffers) {
|
||||
partSize += b.readableByteCount();
|
||||
}
|
||||
|
||||
ByteBuffer partData = ByteBuffer.allocate(partSize);
|
||||
buffers.forEach((buffer) -> {
|
||||
partData.put(buffer.asByteBuffer());
|
||||
});
|
||||
|
||||
// Reset read pointer to first byte
|
||||
partData.rewind();
|
||||
|
||||
log.info("[I208] partData: size={}", partData.capacity());
|
||||
return partData;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Upload a single file part to the requested bucket
|
||||
* @param uploadState
|
||||
* @param buffer
|
||||
* @return
|
||||
*/
|
||||
private Mono<CompletedPart> uploadPart(UploadState uploadState, ByteBuffer buffer) {
|
||||
final int partNumber = ++uploadState.partCounter;
|
||||
log.info("[I218] uploadPart: partNumber={}, contentLength={}",partNumber, buffer.capacity());
|
||||
|
||||
CompletableFuture<UploadPartResponse> request = s3client.uploadPart(UploadPartRequest.builder()
|
||||
.bucket(uploadState.bucket)
|
||||
.key(uploadState.filekey)
|
||||
.partNumber(partNumber)
|
||||
.uploadId(uploadState.uploadId)
|
||||
.contentLength((long) buffer.capacity())
|
||||
.build(),
|
||||
AsyncRequestBody.fromPublisher(Mono.just(buffer)));
|
||||
|
||||
return Mono
|
||||
.fromFuture(request)
|
||||
.map((uploadPartResult) -> {
|
||||
checkResult(uploadPartResult);
|
||||
log.info("[I230] uploadPart complete: part={}, etag={}",partNumber,uploadPartResult.eTag());
|
||||
return CompletedPart.builder()
|
||||
.eTag(uploadPartResult.eTag())
|
||||
.partNumber(partNumber)
|
||||
.build();
|
||||
});
|
||||
}
|
||||
|
||||
private Mono<CompleteMultipartUploadResponse> completeUpload(UploadState state) {
|
||||
log.info("[I202] completeUpload: bucket={}, filekey={}, completedParts.size={}", state.bucket, state.filekey, state.completedParts.size());
|
||||
|
||||
CompletedMultipartUpload multipartUpload = CompletedMultipartUpload.builder()
|
||||
.parts(state.completedParts.values())
|
||||
.build();
|
||||
|
||||
return Mono.fromFuture(s3client.completeMultipartUpload(CompleteMultipartUploadRequest.builder()
|
||||
.bucket(state.bucket)
|
||||
.uploadId(state.uploadId)
|
||||
.multipartUpload(multipartUpload)
|
||||
.key(state.filekey)
|
||||
.build()));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* check result from an API call.
|
||||
* @param result Result from an API call
|
||||
*/
|
||||
private static void checkResult(SdkResponse result) {
|
||||
if (result.sdkHttpResponse() == null || !result.sdkHttpResponse().isSuccessful()) {
|
||||
throw new UploadFailedException(result);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Holds upload state during a multipart upload
|
||||
*/
|
||||
static class UploadState {
|
||||
final String bucket;
|
||||
final String filekey;
|
||||
|
||||
String uploadId;
|
||||
int partCounter;
|
||||
Map<Integer, CompletedPart> completedParts = new HashMap<>();
|
||||
int buffered = 0;
|
||||
|
||||
UploadState(String bucket, String filekey) {
|
||||
this.bucket = bucket;
|
||||
this.filekey = filekey;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
package com.baeldung.aws.reactive.s3;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.http.HttpStatus;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
@Builder
|
||||
public class UploadResult {
|
||||
HttpStatus status;
|
||||
String[] keys;
|
||||
|
||||
public UploadResult() {}
|
||||
|
||||
public UploadResult(HttpStatus status, List<String> keys) {
|
||||
this.status = status;
|
||||
this.keys = keys == null ? new String[] {}: keys.toArray(new String[] {});
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
|
||||
#
|
||||
# Minio profile
|
||||
#
|
||||
aws:
|
||||
s3:
|
||||
region: sa-east-1
|
||||
endpoint: http://localhost:9000
|
||||
accessKeyId: 8KLF8U60JER4AP23H0A6
|
||||
secretAccessKey: vX4uM7e7nNGPqjcXycVVhceNR7NQkiMQkR9Hoctf
|
||||
bucket: bucket1
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
|
||||
#
|
||||
# Configurações de acesso ao Minio
|
||||
#
|
||||
aws:
|
||||
s3:
|
||||
region: sa-east-1
|
||||
# When using AWS, the library will use one of the available
|
||||
# credential sources described in the documentation.
|
||||
# accessKeyId: ****
|
||||
# secretAccessKey: ****
|
||||
bucket: dev1.token.com.br
|
||||
|
||||
|
||||
|
||||
|
||||
+85
@@ -0,0 +1,85 @@
|
||||
package com.baeldung.aws.reactive.s3;
|
||||
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.io.File;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Paths;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
|
||||
import org.springframework.boot.test.web.client.TestRestTemplate;
|
||||
import org.springframework.boot.web.server.LocalServerPort;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.http.ContentDisposition;
|
||||
import org.springframework.http.HttpEntity;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.test.context.ActiveProfiles;
|
||||
import org.springframework.util.LinkedMultiValueMap;
|
||||
import org.springframework.util.MultiValueMap;
|
||||
import org.springframework.util.ResourceUtils;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
|
||||
@ActiveProfiles("minio")
|
||||
class ReactiveS3ApplicationLiveTest {
|
||||
|
||||
@Autowired
|
||||
private TestRestTemplate restTemplate;
|
||||
|
||||
@LocalServerPort
|
||||
private int serverPort;
|
||||
|
||||
|
||||
@Test
|
||||
void whenUploadSingleFile_thenSuccess() throws Exception {
|
||||
|
||||
String url = "http://localhost:" + serverPort + "/inbox";
|
||||
byte[] data = Files.readAllBytes(Paths.get("src/test/resources/testimage1.png"));
|
||||
UploadResult result = restTemplate.postForObject(url, data , UploadResult.class);
|
||||
|
||||
assertEquals("Expected CREATED (202)", result.getStatus(), HttpStatus.CREATED );
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
void whenUploadMultipleFiles_thenSuccess() throws Exception {
|
||||
|
||||
|
||||
MultiValueMap<String, Object> body = new LinkedMultiValueMap<>();
|
||||
addFileEntity("f1", body, new File("src/test/resources/testimage1.png"));
|
||||
addFileEntity("f2", body, new File("src/test/resources/testimage2.png"));
|
||||
|
||||
HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(body);
|
||||
String url = "http://localhost:" + serverPort + "/inbox";
|
||||
|
||||
ResponseEntity<UploadResult> result = restTemplate.postForEntity(url, requestEntity, UploadResult.class);
|
||||
|
||||
assertEquals("Http Code",HttpStatus.CREATED, result.getStatusCode() );
|
||||
assertEquals("File keys",2, result.getBody().getKeys().length);
|
||||
|
||||
}
|
||||
|
||||
private void addFileEntity(String name, MultiValueMap<String, Object> body, File file) throws Exception {
|
||||
|
||||
byte[] data = Files.readAllBytes(file.toPath());
|
||||
MultiValueMap<String, String> headers = new LinkedMultiValueMap<>();
|
||||
ContentDisposition contentDispositionHeader = ContentDisposition.builder("form-data")
|
||||
.name(name)
|
||||
.filename(file.getName())
|
||||
.build();
|
||||
|
||||
headers.add(HttpHeaders.CONTENT_DISPOSITION, contentDispositionHeader.toString());
|
||||
|
||||
HttpEntity<byte[]> fileEntity = new HttpEntity<>(data, headers);
|
||||
body.add(name, fileEntity);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 4.2 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 24 KiB |
@@ -0,0 +1,2 @@
|
||||
/target/
|
||||
.idea/
|
||||
@@ -0,0 +1,9 @@
|
||||
## AWS S3
|
||||
|
||||
This module contains articles about Simple Storage Service (S3) on AWS
|
||||
|
||||
### Relevant articles
|
||||
|
||||
- [AWS S3 with Java](https://www.baeldung.com/aws-s3-java)
|
||||
- [Multipart Uploads in Amazon S3 with Java](https://www.baeldung.com/aws-s3-multipart-upload)
|
||||
- [Using the JetS3t Java Client With Amazon S3](https://www.baeldung.com/jets3t-amazon-s3)
|
||||
@@ -0,0 +1,70 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<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>
|
||||
<artifactId>aws-s3</artifactId>
|
||||
<version>0.1.0-SNAPSHOT</version>
|
||||
<name>aws-s3</name>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>aws-modules</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.amazonaws</groupId>
|
||||
<artifactId>aws-java-sdk</artifactId>
|
||||
<version>${aws-java-sdk.version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>commons-io</groupId>
|
||||
<artifactId>commons-io</artifactId>
|
||||
<version>${commons-io.version}</version>
|
||||
</dependency>
|
||||
<!-- JetS3t -->
|
||||
<dependency>
|
||||
<groupId>org.lucee</groupId>
|
||||
<artifactId>jets3t</artifactId>
|
||||
<version>${jets3t-version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.lucee</groupId>
|
||||
<artifactId>commons-codec</artifactId>
|
||||
<version>${commons-codec-version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-shade-plugin</artifactId>
|
||||
<version>${maven-shade-plugin.version}</version>
|
||||
<configuration>
|
||||
<createDependencyReducedPom>false</createDependencyReducedPom>
|
||||
</configuration>
|
||||
<executions>
|
||||
<execution>
|
||||
<phase>package</phase>
|
||||
<goals>
|
||||
<goal>shade</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
<properties>
|
||||
<aws-java-sdk.version>1.11.290</aws-java-sdk.version>
|
||||
<commons-codec-version>1.10.L001</commons-codec-version>
|
||||
<jets3t-version>0.9.4.0006L</jets3t-version>
|
||||
<maven-shade-plugin.version>3.0.0</maven-shade-plugin.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
@@ -0,0 +1,87 @@
|
||||
package com.baeldung.s3;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.List;
|
||||
|
||||
import com.amazonaws.services.s3.AmazonS3;
|
||||
import com.amazonaws.services.s3.AmazonS3Client;
|
||||
import com.amazonaws.services.s3.model.Bucket;
|
||||
import com.amazonaws.services.s3.model.CopyObjectResult;
|
||||
import com.amazonaws.services.s3.model.DeleteObjectsRequest;
|
||||
import com.amazonaws.services.s3.model.DeleteObjectsResult;
|
||||
import com.amazonaws.services.s3.model.ObjectListing;
|
||||
import com.amazonaws.services.s3.model.PutObjectResult;
|
||||
import com.amazonaws.services.s3.model.S3Object;
|
||||
|
||||
public class AWSS3Service {
|
||||
private final AmazonS3 s3client;
|
||||
|
||||
public AWSS3Service() {
|
||||
this(new AmazonS3Client() {
|
||||
});
|
||||
}
|
||||
|
||||
public AWSS3Service(AmazonS3 s3client) {
|
||||
this.s3client = s3client;
|
||||
}
|
||||
|
||||
//is bucket exist?
|
||||
public boolean doesBucketExist(String bucketName) {
|
||||
return s3client.doesBucketExist(bucketName);
|
||||
}
|
||||
|
||||
//create a bucket
|
||||
public Bucket createBucket(String bucketName) {
|
||||
return s3client.createBucket(bucketName);
|
||||
}
|
||||
|
||||
//list all buckets
|
||||
public List<Bucket> listBuckets() {
|
||||
return s3client.listBuckets();
|
||||
}
|
||||
|
||||
//delete a bucket
|
||||
public void deleteBucket(String bucketName) {
|
||||
s3client.deleteBucket(bucketName);
|
||||
}
|
||||
|
||||
//uploading object
|
||||
public PutObjectResult putObject(String bucketName, String key, File file) {
|
||||
return s3client.putObject(bucketName, key, file);
|
||||
}
|
||||
|
||||
//listing objects
|
||||
public ObjectListing listObjects(String bucketName) {
|
||||
return s3client.listObjects(bucketName);
|
||||
}
|
||||
|
||||
//get an object
|
||||
public S3Object getObject(String bucketName, String objectKey) {
|
||||
return s3client.getObject(bucketName, objectKey);
|
||||
}
|
||||
|
||||
//copying an object
|
||||
public CopyObjectResult copyObject(
|
||||
String sourceBucketName,
|
||||
String sourceKey,
|
||||
String destinationBucketName,
|
||||
String destinationKey
|
||||
) {
|
||||
return s3client.copyObject(
|
||||
sourceBucketName,
|
||||
sourceKey,
|
||||
destinationBucketName,
|
||||
destinationKey
|
||||
);
|
||||
}
|
||||
|
||||
//deleting an object
|
||||
public void deleteObject(String bucketName, String objectKey) {
|
||||
s3client.deleteObject(bucketName, objectKey);
|
||||
}
|
||||
|
||||
//deleting multiple Objects
|
||||
public DeleteObjectsResult deleteObjects(DeleteObjectsRequest delObjReq) {
|
||||
return s3client.deleteObjects(delObjReq);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
package com.baeldung.s3;
|
||||
|
||||
import com.amazonaws.AmazonClientException;
|
||||
import com.amazonaws.auth.DefaultAWSCredentialsProviderChain;
|
||||
import com.amazonaws.event.ProgressListener;
|
||||
import com.amazonaws.regions.Regions;
|
||||
import com.amazonaws.services.s3.AmazonS3;
|
||||
import com.amazonaws.services.s3.AmazonS3ClientBuilder;
|
||||
import com.amazonaws.services.s3.model.PutObjectRequest;
|
||||
import com.amazonaws.services.s3.transfer.TransferManager;
|
||||
import com.amazonaws.services.s3.transfer.TransferManagerBuilder;
|
||||
import com.amazonaws.services.s3.transfer.Upload;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.concurrent.Executors;
|
||||
|
||||
public class MultipartUpload {
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
String existingBucketName = "baeldung-bucket";
|
||||
String keyName = "my-picture.jpg";
|
||||
String filePath = "documents/my-picture.jpg";
|
||||
|
||||
AmazonS3 amazonS3 = AmazonS3ClientBuilder
|
||||
.standard()
|
||||
.withCredentials(new DefaultAWSCredentialsProviderChain())
|
||||
.withRegion(Regions.DEFAULT_REGION)
|
||||
.build();
|
||||
|
||||
int maxUploadThreads = 5;
|
||||
|
||||
TransferManager tm = TransferManagerBuilder
|
||||
.standard()
|
||||
.withS3Client(amazonS3)
|
||||
.withMultipartUploadThreshold((long) (5 * 1024 * 1024))
|
||||
.withExecutorFactory(() -> Executors.newFixedThreadPool(maxUploadThreads))
|
||||
.build();
|
||||
|
||||
ProgressListener progressListener =
|
||||
progressEvent -> System.out.println("Transferred bytes: " + progressEvent.getBytesTransferred());
|
||||
|
||||
PutObjectRequest request = new PutObjectRequest(existingBucketName, keyName, new File(filePath));
|
||||
|
||||
request.setGeneralProgressListener(progressListener);
|
||||
|
||||
Upload upload = tm.upload(request);
|
||||
|
||||
try {
|
||||
upload.waitForCompletion();
|
||||
System.out.println("Upload complete.");
|
||||
} catch (AmazonClientException e) {
|
||||
System.out.println("Error occurred while uploading file");
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,101 @@
|
||||
package com.baeldung.s3;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
||||
import org.apache.commons.io.FileUtils;
|
||||
|
||||
import com.amazonaws.auth.AWSCredentials;
|
||||
import com.amazonaws.auth.AWSStaticCredentialsProvider;
|
||||
import com.amazonaws.auth.BasicAWSCredentials;
|
||||
import com.amazonaws.regions.Regions;
|
||||
import com.amazonaws.services.s3.AmazonS3;
|
||||
import com.amazonaws.services.s3.AmazonS3ClientBuilder;
|
||||
import com.amazonaws.services.s3.model.Bucket;
|
||||
import com.amazonaws.services.s3.model.DeleteObjectsRequest;
|
||||
import com.amazonaws.services.s3.model.ObjectListing;
|
||||
import com.amazonaws.services.s3.model.S3Object;
|
||||
import com.amazonaws.services.s3.model.S3ObjectInputStream;
|
||||
import com.amazonaws.services.s3.model.S3ObjectSummary;
|
||||
|
||||
public class S3Application {
|
||||
|
||||
private static final AWSCredentials credentials;
|
||||
private static String bucketName;
|
||||
|
||||
static {
|
||||
//put your accesskey and secretkey here
|
||||
credentials = new BasicAWSCredentials(
|
||||
"<AWS accesskey>",
|
||||
"<AWS secretkey>"
|
||||
);
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws IOException {
|
||||
//set-up the client
|
||||
AmazonS3 s3client = AmazonS3ClientBuilder
|
||||
.standard()
|
||||
.withCredentials(new AWSStaticCredentialsProvider(credentials))
|
||||
.withRegion(Regions.US_EAST_2)
|
||||
.build();
|
||||
|
||||
AWSS3Service awsService = new AWSS3Service(s3client);
|
||||
|
||||
bucketName = "baeldung-bucket";
|
||||
|
||||
//creating a bucket
|
||||
if(awsService.doesBucketExist(bucketName)) {
|
||||
System.out.println("Bucket name is not available."
|
||||
+ " Try again with a different Bucket name.");
|
||||
return;
|
||||
}
|
||||
awsService.createBucket(bucketName);
|
||||
|
||||
//list all the buckets
|
||||
for(Bucket s : awsService.listBuckets() ) {
|
||||
System.out.println(s.getName());
|
||||
}
|
||||
|
||||
//deleting bucket
|
||||
awsService.deleteBucket("baeldung-bucket-test2");
|
||||
|
||||
//uploading object
|
||||
awsService.putObject(
|
||||
bucketName,
|
||||
"Document/hello.txt",
|
||||
new File("/Users/user/Document/hello.txt")
|
||||
);
|
||||
|
||||
//listing objects
|
||||
ObjectListing objectListing = awsService.listObjects(bucketName);
|
||||
for(S3ObjectSummary os : objectListing.getObjectSummaries()) {
|
||||
System.out.println(os.getKey());
|
||||
}
|
||||
|
||||
//downloading an object
|
||||
S3Object s3object = awsService.getObject(bucketName, "Document/hello.txt");
|
||||
S3ObjectInputStream inputStream = s3object.getObjectContent();
|
||||
FileUtils.copyInputStreamToFile(inputStream, new File("/Users/user/Desktop/hello.txt"));
|
||||
|
||||
//copying an object
|
||||
awsService.copyObject(
|
||||
"baeldung-bucket",
|
||||
"picture/pic.png",
|
||||
"baeldung-bucket2",
|
||||
"Document/picture.png"
|
||||
);
|
||||
|
||||
//deleting an object
|
||||
awsService.deleteObject(bucketName, "Document/hello.txt");
|
||||
|
||||
//deleting multiple objects
|
||||
String objkeyArr[] = {
|
||||
"Document/hello2.txt",
|
||||
"Document/picture.png"
|
||||
};
|
||||
|
||||
DeleteObjectsRequest delObjReq = new DeleteObjectsRequest("baeldung-bucket")
|
||||
.withKeys(objkeyArr);
|
||||
awsService.deleteObjects(delObjReq);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<configuration>
|
||||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<encoder>
|
||||
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
|
||||
</pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<root level="INFO">
|
||||
<appender-ref ref="STDOUT" />
|
||||
</root>
|
||||
</configuration>
|
||||
@@ -0,0 +1,351 @@
|
||||
package com.baeldung.jets3t;
|
||||
|
||||
import org.apache.commons.codec.digest.DigestUtils;
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.jets3t.service.S3Service;
|
||||
import org.jets3t.service.ServiceException;
|
||||
import org.jets3t.service.impl.rest.httpclient.RestS3Service;
|
||||
import org.jets3t.service.model.S3Bucket;
|
||||
import org.jets3t.service.model.S3Object;
|
||||
import org.jets3t.service.model.StorageObject;
|
||||
import org.jets3t.service.security.AWSCredentials;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.*;
|
||||
import java.nio.file.Files;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import static java.nio.file.StandardCopyOption.REPLACE_EXISTING;
|
||||
import static junit.framework.TestCase.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
public class JetS3tLiveTest {
|
||||
|
||||
private Log log = LogFactory.getLog(JetS3tLiveTest.class);
|
||||
|
||||
private static final String BucketName = "baeldung-barfoo";
|
||||
private static final String TestString = "test string";
|
||||
private static final String TestStringName = "string object";
|
||||
private static final String TgtBucket = "baeldung-tgtbucket";
|
||||
|
||||
private static S3Service s3Service;
|
||||
|
||||
@BeforeClass
|
||||
public static void connectS3() throws Exception {
|
||||
|
||||
// Replace with your keys
|
||||
String awsAccessKey = "your access key";
|
||||
String awsSecretKey = "your secret key";
|
||||
|
||||
// Create credentials
|
||||
AWSCredentials awsCredentials = new AWSCredentials(awsAccessKey, awsSecretKey);
|
||||
|
||||
// Create service
|
||||
s3Service = new RestS3Service(awsCredentials);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenCreate_AndDeleteBucket_CountGoesUpThenDown() throws Exception {
|
||||
|
||||
// List buckets, get a count
|
||||
S3Bucket[] myBuckets = s3Service.listAllBuckets();
|
||||
int count = Arrays.stream(myBuckets).map(S3Bucket::getName).collect(Collectors.toList()).size();
|
||||
|
||||
// Create a bucket
|
||||
S3Bucket bucket = createBucket();
|
||||
assertNotNull(bucket);
|
||||
|
||||
// List again
|
||||
myBuckets = s3Service.listAllBuckets();
|
||||
int newCount = Arrays.stream(myBuckets).map(S3Bucket::getName).collect(Collectors.toList()).size();
|
||||
|
||||
// We should have one more
|
||||
assertEquals((count + 1), newCount);
|
||||
|
||||
// Delete so next test doesn't fail
|
||||
deleteBucket();
|
||||
|
||||
// Check the count again, just for laughs
|
||||
myBuckets = s3Service.listAllBuckets();
|
||||
newCount = Arrays.stream(myBuckets).map(S3Bucket::getName).collect(Collectors.toList()).size();
|
||||
assertEquals(count, newCount);
|
||||
|
||||
}
|
||||
|
||||
private S3Bucket createBucket() throws Exception {
|
||||
S3Bucket bucket = s3Service.createBucket(BucketName);
|
||||
log.info(bucket);
|
||||
return bucket;
|
||||
}
|
||||
|
||||
|
||||
private void deleteBucket() throws ServiceException {
|
||||
s3Service.deleteBucket(BucketName);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenString_Uploaded_StringInfoIsAvailable() throws Exception {
|
||||
|
||||
// Create a bucket
|
||||
S3Bucket bucket = createBucket();
|
||||
assertNotNull(bucket);
|
||||
|
||||
// Upload a string
|
||||
uploadStringData();
|
||||
|
||||
// Get the details
|
||||
StorageObject objectDetailsOnly = s3Service.getObjectDetails(BucketName, TestStringName);
|
||||
log.info("Content type: " + objectDetailsOnly.getContentType() + " length: " + objectDetailsOnly.getContentLength());
|
||||
|
||||
// Delete it
|
||||
deleteObject(TestStringName);
|
||||
|
||||
// For next test
|
||||
deleteBucket();
|
||||
}
|
||||
|
||||
private void uploadStringData() throws Exception {
|
||||
S3Object stringObject = new S3Object(TestStringName, TestString);
|
||||
s3Service.putObject(BucketName, stringObject);
|
||||
log.info("Content type:" + stringObject.getContentType());
|
||||
}
|
||||
|
||||
private void deleteObject(String objectName) throws ServiceException {
|
||||
s3Service.deleteObject(BucketName, objectName);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenStringUploaded_StringIsDownloaded() throws Exception {
|
||||
|
||||
// Get a bucket
|
||||
S3Bucket bucket = createBucket();
|
||||
assertNotNull(bucket);
|
||||
|
||||
uploadStringData();
|
||||
|
||||
// Download
|
||||
S3Object stringObject = s3Service.getObject(BucketName, TestStringName);
|
||||
|
||||
// Process stream into a string
|
||||
String downloadedString = new BufferedReader(new InputStreamReader(stringObject.getDataInputStream())).lines().collect(Collectors.joining("\n"));
|
||||
|
||||
// Verify
|
||||
assertTrue(TestString.equals(downloadedString));
|
||||
|
||||
|
||||
// Clean up for next test
|
||||
deleteObject(TestStringName);
|
||||
deleteBucket();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenBinaryFileUploaded_FileIsDownloaded() throws Exception {
|
||||
|
||||
// get a bucket
|
||||
S3Bucket bucket = createBucket();
|
||||
assertNotNull(bucket);
|
||||
|
||||
// Put a binary file
|
||||
S3Object fileObject = new S3Object(new File("src/test/resources/test.jpg"));
|
||||
s3Service.putObject(BucketName, fileObject);
|
||||
|
||||
// Print info about type and name
|
||||
log.info("Content type:" + fileObject.getContentType());
|
||||
log.info("File object name is " + fileObject.getName());
|
||||
|
||||
// Download
|
||||
S3Object newFileObject = s3Service.getObject(BucketName, "test.jpg");
|
||||
|
||||
// Save to a different name
|
||||
File newFile = new File("src/test/resources/newtest.jpg");
|
||||
Files.copy(newFileObject.getDataInputStream(), newFile.toPath(), REPLACE_EXISTING);
|
||||
|
||||
|
||||
// Get hashes and compare
|
||||
String origMD5 = getFileMD5("src/test/resources/test.jpg");
|
||||
String newMD5 = getFileMD5("src/test/resources/newtest.jpg");
|
||||
assertTrue(origMD5.equals(newMD5));
|
||||
|
||||
// Clean up
|
||||
deleteObject("test.jpg");
|
||||
deleteBucket();
|
||||
}
|
||||
|
||||
// Get MD5 hash for a file
|
||||
private String getFileMD5(String filename) throws IOException {
|
||||
try (FileInputStream fis = new FileInputStream(new File(filename))) {
|
||||
return DigestUtils.md5Hex(fis);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
@Test
|
||||
public void givenStreamDataUploaded_StreamDataIsDownloaded() throws Exception {
|
||||
|
||||
// get a bucket
|
||||
S3Bucket bucket = createBucket();
|
||||
assertNotNull(bucket);
|
||||
|
||||
ArrayList<Integer> numbers = new ArrayList<>();
|
||||
numbers.add(2);
|
||||
numbers.add(3);
|
||||
numbers.add(5);
|
||||
numbers.add(7);
|
||||
|
||||
// Serialize ArrayList
|
||||
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
|
||||
ObjectOutputStream objectOutputStream = new ObjectOutputStream(bytes);
|
||||
objectOutputStream.writeObject(numbers);
|
||||
|
||||
// Wrap bytes
|
||||
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bytes.toByteArray());
|
||||
|
||||
// Create and populate object
|
||||
S3Object streamObject = new S3Object("stream");
|
||||
streamObject.setDataInputStream(byteArrayInputStream);
|
||||
streamObject.setContentLength(byteArrayInputStream.available());
|
||||
streamObject.setContentType("binary/octet-stream");
|
||||
|
||||
// Put it
|
||||
s3Service.putObject(BucketName, streamObject);
|
||||
|
||||
// Get it
|
||||
S3Object newStreamObject = s3Service.getObject(BucketName, "stream");
|
||||
|
||||
// Convert back to ArrayList
|
||||
ObjectInputStream objectInputStream = new ObjectInputStream(newStreamObject.getDataInputStream());
|
||||
ArrayList<Integer> newNumbers = (ArrayList<Integer>)objectInputStream.readObject();
|
||||
|
||||
assertEquals(2, (int)newNumbers.get(0));
|
||||
assertEquals(3, (int)newNumbers.get(1));
|
||||
assertEquals(5, (int)newNumbers.get(2));
|
||||
assertEquals(7, (int)newNumbers.get(3));
|
||||
|
||||
// Clean up
|
||||
deleteObject("stream");
|
||||
deleteBucket();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenFileCopied_CopyIsSame() throws Exception {
|
||||
|
||||
// get a bucket
|
||||
S3Bucket bucket = createBucket();
|
||||
assertNotNull(bucket);
|
||||
|
||||
// Put a binary file
|
||||
S3Object fileObject = new S3Object(new File("src/test/resources/test.jpg"));
|
||||
s3Service.putObject(BucketName, fileObject);
|
||||
|
||||
|
||||
// Copy it
|
||||
S3Object targetObject = new S3Object("testcopy.jpg");
|
||||
s3Service.copyObject(BucketName, "test.jpg", BucketName, targetObject, false);
|
||||
|
||||
|
||||
// Download
|
||||
S3Object newFileObject = s3Service.getObject(BucketName, "testcopy.jpg");
|
||||
|
||||
// Save to a different name
|
||||
File newFile = new File("src/test/resources/testcopy.jpg");
|
||||
Files.copy(newFileObject.getDataInputStream(), newFile.toPath(), REPLACE_EXISTING);
|
||||
|
||||
|
||||
// Get hashes and compare
|
||||
String origMD5 = getFileMD5("src/test/resources/test.jpg");
|
||||
String newMD5 = getFileMD5("src/test/resources/testcopy.jpg");
|
||||
assertTrue(origMD5.equals(newMD5));
|
||||
|
||||
// Clean up
|
||||
deleteObject("test.jpg");
|
||||
deleteObject("testcopy.jpg");
|
||||
deleteBucket();
|
||||
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void whenFileRenamed_NewNameIsSame() throws Exception {
|
||||
|
||||
// get a bucket
|
||||
S3Bucket bucket = createBucket();
|
||||
assertNotNull(bucket);
|
||||
|
||||
// Put a binary file
|
||||
S3Object fileObject = new S3Object(new File("src/test/resources/test.jpg"));
|
||||
s3Service.putObject(BucketName, fileObject);
|
||||
|
||||
|
||||
// Copy it
|
||||
s3Service.renameObject(BucketName, "test.jpg", new S3Object("spidey.jpg"));
|
||||
|
||||
|
||||
// Download
|
||||
S3Object newFileObject = s3Service.getObject(BucketName, "spidey.jpg");
|
||||
|
||||
// Save to a different name
|
||||
File newFile = new File("src/test/resources/spidey.jpg");
|
||||
Files.copy(newFileObject.getDataInputStream(), newFile.toPath(), REPLACE_EXISTING);
|
||||
|
||||
|
||||
// Get hashes and compare
|
||||
String origMD5 = getFileMD5("src/test/resources/test.jpg");
|
||||
String newMD5 = getFileMD5("src/test/resources/spidey.jpg");
|
||||
assertTrue(origMD5.equals(newMD5));
|
||||
|
||||
// Clean up
|
||||
deleteObject("test.jpg");
|
||||
deleteObject("spidey.jpg");
|
||||
deleteBucket();
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenFileMoved_NewInstanceIsSame() throws Exception {
|
||||
|
||||
// get a bucket
|
||||
S3Bucket bucket = createBucket();
|
||||
assertNotNull(bucket);
|
||||
|
||||
// create another bucket
|
||||
S3Bucket tgtBucket = s3Service.createBucket(TgtBucket);
|
||||
|
||||
|
||||
// Put a binary file
|
||||
S3Object fileObject = new S3Object(new File("src/test/resources/test.jpg"));
|
||||
s3Service.putObject(BucketName, fileObject);
|
||||
|
||||
|
||||
// Copy it
|
||||
s3Service.moveObject(BucketName, "test.jpg", TgtBucket,
|
||||
new S3Object("spidey.jpg"), false);
|
||||
|
||||
|
||||
// Download
|
||||
S3Object newFileObject = s3Service.getObject(TgtBucket, "spidey.jpg");
|
||||
|
||||
// Save to a different name
|
||||
File newFile = new File("src/test/resources/spidey.jpg");
|
||||
Files.copy(newFileObject.getDataInputStream(), newFile.toPath(), REPLACE_EXISTING);
|
||||
|
||||
|
||||
// Get hashes and compare
|
||||
String origMD5 = getFileMD5("src/test/resources/test.jpg");
|
||||
String newMD5 = getFileMD5("src/test/resources/spidey.jpg");
|
||||
assertTrue(origMD5.equals(newMD5));
|
||||
|
||||
// Clean up
|
||||
deleteBucket();
|
||||
|
||||
s3Service.deleteObject(TgtBucket, "spidey.jpg");
|
||||
s3Service.deleteBucket(TgtBucket);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,113 @@
|
||||
package com.baeldung.s3;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.anyString;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import com.amazonaws.services.s3.AmazonS3;
|
||||
import com.amazonaws.services.s3.model.CopyObjectResult;
|
||||
import com.amazonaws.services.s3.model.DeleteObjectsRequest;
|
||||
import com.amazonaws.services.s3.model.DeleteObjectsResult;
|
||||
import com.amazonaws.services.s3.model.PutObjectResult;
|
||||
|
||||
public class AWSS3ServiceIntegrationTest {
|
||||
|
||||
private static final String BUCKET_NAME = "bucket_name";
|
||||
private static final String KEY_NAME = "key_name";
|
||||
private static final String BUCKET_NAME2 = "bucket_name2";
|
||||
private static final String KEY_NAME2 = "key_name2";
|
||||
|
||||
private AmazonS3 s3;
|
||||
private AWSS3Service service;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
s3 = mock(AmazonS3.class);
|
||||
service = new AWSS3Service(s3);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenInitializingAWSS3Service_thenNotNull() {
|
||||
assertThat(new AWSS3Service()).isNotNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenVerifyingIfS3BucketExist_thenCorrect() {
|
||||
service.doesBucketExist(BUCKET_NAME);
|
||||
verify(s3).doesBucketExist(BUCKET_NAME);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenVerifyingCreationOfS3Bucket_thenCorrect() {
|
||||
service.createBucket(BUCKET_NAME);
|
||||
verify(s3).createBucket(BUCKET_NAME);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenVerifyingListBuckets_thenCorrect() {
|
||||
service.listBuckets();
|
||||
verify(s3).listBuckets();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenDeletingBucket_thenCorrect() {
|
||||
service.deleteBucket(BUCKET_NAME);
|
||||
verify(s3).deleteBucket(BUCKET_NAME);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenVerifyingPutObject_thenCorrect() {
|
||||
File file = mock(File.class);
|
||||
PutObjectResult result = mock(PutObjectResult.class);
|
||||
when(s3.putObject(anyString(), anyString(), (File) any())).thenReturn(result);
|
||||
|
||||
assertThat(service.putObject(BUCKET_NAME, KEY_NAME, file)).isEqualTo(result);
|
||||
verify(s3).putObject(BUCKET_NAME, KEY_NAME, file);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenVerifyingListObjects_thenCorrect() {
|
||||
service.listObjects(BUCKET_NAME);
|
||||
verify(s3).listObjects(BUCKET_NAME);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenVerifyingGetObject_thenCorrect() {
|
||||
service.getObject(BUCKET_NAME, KEY_NAME);
|
||||
verify(s3).getObject(BUCKET_NAME, KEY_NAME);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenVerifyingCopyObject_thenCorrect() {
|
||||
CopyObjectResult result = mock(CopyObjectResult.class);
|
||||
when(s3.copyObject(anyString(), anyString(), anyString(), anyString())).thenReturn(result);
|
||||
|
||||
assertThat(service.copyObject(BUCKET_NAME, KEY_NAME, BUCKET_NAME2, KEY_NAME2)).isEqualTo(result);
|
||||
verify(s3).copyObject(BUCKET_NAME, KEY_NAME, BUCKET_NAME2, KEY_NAME2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenVerifyingDeleteObject_thenCorrect() {
|
||||
service.deleteObject(BUCKET_NAME, KEY_NAME);
|
||||
verify(s3).deleteObject(BUCKET_NAME, KEY_NAME);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenVerifyingDeleteObjects_thenCorrect() {
|
||||
DeleteObjectsRequest request = mock(DeleteObjectsRequest.class);
|
||||
DeleteObjectsResult result = mock(DeleteObjectsResult.class);
|
||||
when(s3.deleteObjects((DeleteObjectsRequest)any())).thenReturn(result);
|
||||
|
||||
assertThat(service.deleteObjects(request)).isEqualTo(result);
|
||||
verify(s3).deleteObjects(request);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
package com.baeldung.s3;
|
||||
|
||||
import com.amazonaws.event.ProgressListener;
|
||||
import com.amazonaws.services.s3.AmazonS3;
|
||||
import com.amazonaws.services.s3.model.PutObjectRequest;
|
||||
import com.amazonaws.services.s3.model.PutObjectResult;
|
||||
import com.amazonaws.services.s3.transfer.TransferManager;
|
||||
import com.amazonaws.services.s3.transfer.TransferManagerBuilder;
|
||||
import com.amazonaws.services.s3.transfer.Upload;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.concurrent.Executors;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.anyString;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
public class MultipartUploadLiveTest {
|
||||
|
||||
private static final String BUCKET_NAME = "bucket_name";
|
||||
private static final String KEY_NAME = "picture.jpg";
|
||||
|
||||
private AmazonS3 amazonS3;
|
||||
private TransferManager tm;
|
||||
private ProgressListener progressListener;
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
amazonS3 = mock(AmazonS3.class);
|
||||
tm = TransferManagerBuilder
|
||||
.standard()
|
||||
.withS3Client(amazonS3)
|
||||
.withMultipartUploadThreshold((long) (5 * 1024 * 1025))
|
||||
.withExecutorFactory(() -> Executors.newFixedThreadPool(5))
|
||||
.build();
|
||||
progressListener =
|
||||
progressEvent -> System.out.println("Transferred bytes: " + progressEvent.getBytesTransferred());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenUploadingFileWithTransferManager_thenVerifyUploadRequested() {
|
||||
File file = mock(File.class);
|
||||
PutObjectResult s3Result = mock(PutObjectResult.class);
|
||||
|
||||
when(amazonS3.putObject(anyString(), anyString(), (File) any())).thenReturn(s3Result);
|
||||
when(file.getName()).thenReturn(KEY_NAME);
|
||||
|
||||
PutObjectRequest request = new PutObjectRequest(BUCKET_NAME, KEY_NAME, file);
|
||||
request.setGeneralProgressListener(progressListener);
|
||||
|
||||
Upload upload = tm.upload(request);
|
||||
|
||||
assertThat(upload).isNotNull();
|
||||
verify(amazonS3).putObject(request);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
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>
|
||||
<artifactId>aws-modules</artifactId>
|
||||
<name>aws-modules</name>
|
||||
<packaging>pom</packaging>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-modules</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<modules>
|
||||
<module>aws-app-sync</module>
|
||||
<module>aws-lambda</module>
|
||||
<module>aws-miscellaneous</module>
|
||||
<module>aws-reactive</module>
|
||||
<module>aws-s3</module>
|
||||
</modules>
|
||||
|
||||
</project>
|
||||
Reference in New Issue
Block a user