Skip to content

Commit 3813bac

Browse files
author
AncaGhenade
committed
improvements + docker compose file
1 parent c535e24 commit 3813bac

File tree

14 files changed

+165
-81
lines changed

14 files changed

+165
-81
lines changed

.pre-commit-config.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
- repo: https://github.com/pre-commit/pre-commit-hooks
2+
rev: v4.4.0
3+
hooks:
4+
- id: detect-aws-credentials

docker-compose.yml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
version: "3.9"
2+
3+
services:
4+
localstack:
5+
image: localstack/localstack
6+
container_name: localstack
7+
ports:
8+
- "4566:4566" # port of to where localstack can be addressed to
9+
- "9000:9000"
10+
environment:
11+
- SERVICES=s3,dynamodb # a list of desired services you want to use.
12+
- DEFAULT_REGION=eu-central-1 # where the localstack mocks to be running
13+
- AWS_ACCESS_KEY_ID=test_access_key
14+
- AWS_SECRET_ACCESS_KEY=test_secret_access_key
15+
- DATA_DIR=/tmp/localstack/data
16+
- PORT_WEB_UI=9000
17+
- LAMBDA_EXECUTOR=local
18+
- DOCKER_HOST=unix:///var/run/docker.sock
19+
- START_WEB=1

scripts/new-bucket-local.sh

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#!/bin/bash
2+
3+
# Set the name of the S3 bucket
4+
bucket_name="shipment-list-demo-bucket"
5+
aws_region="eu-central-1"
6+
7+
# Create the S3 bucket
8+
awslocal s3api create-bucket --bucket $bucket_name

new-bucket.sh renamed to scripts/new-bucket.sh

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
#!/bin/bash
22

33
# For demo purpose only;
4-
# Access key and secret key are set and using `aws configure` to manage the aws cli on your local machine
4+
# Access key and secret key are set and using `aws configure`
5+
# to manage the aws cli on your local machine
56

67
# Set the name of the S3 bucket
78
bucket_name="shipment-list-demo-bucket"
@@ -14,4 +15,4 @@ aws s3 mb s3://$bucket_name
1415
aws s3api create-bucket --bucket $bucket_name --region $aws_region
1516

1617
# Set the bucket policy
17-
aws s3api put-bucket-acl --bucket $bucket_name --acl private
18+
aws s3api put-bucket-acl --bucket $bucket_name --acl private

src/main/java/dev/ancaghenade/shipmentlistdemo/config/AmazonS3Config.java

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.amazonaws.auth.AWSCredentials;
44
import com.amazonaws.auth.AWSStaticCredentialsProvider;
55
import com.amazonaws.auth.BasicAWSCredentials;
6+
import com.amazonaws.client.builder.AwsClientBuilder.EndpointConfiguration;
67
import com.amazonaws.services.s3.AmazonS3;
78
import com.amazonaws.services.s3.AmazonS3ClientBuilder;
89
import org.springframework.beans.factory.annotation.Value;
@@ -12,25 +13,30 @@
1213
@Configuration
1314
public class AmazonS3Config {
1415

15-
@Value("${aws.access.key}")
16+
@Value("${aws.credentials.access-key}")
1617
private String awsAccessKey;
1718

18-
@Value("${aws.access.secret-key}")
19+
@Value("${aws.credentials.secret-key}")
1920
private String awsSecretKey;
2021

2122
@Value("${aws.region}")
2223
private String awsRegion;
2324

25+
@Value("${aws.s3.endpoint}")
26+
private String awsS3EndPoint;
27+
2428
@Bean
2529
public AmazonS3 s3() {
2630
AWSCredentials awsCredentials = new BasicAWSCredentials(
2731
awsAccessKey,
2832
awsSecretKey
2933
);
30-
return AmazonS3ClientBuilder
34+
AmazonS3ClientBuilder amazonS3ClientBuilder = AmazonS3ClientBuilder
3135
.standard()
32-
.withCredentials(new AWSStaticCredentialsProvider(awsCredentials))
33-
.withRegion(awsRegion)
36+
.withEndpointConfiguration(new EndpointConfiguration(awsS3EndPoint,
37+
awsRegion));
38+
39+
return amazonS3ClientBuilder.withCredentials(new AWSStaticCredentialsProvider(awsCredentials))
3440
.build();
3541
}
3642

src/main/java/dev/ancaghenade/shipmentlistdemo/config/DynamoDBConfiguration.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@
1616
@Configuration
1717
public class DynamoDBConfiguration {
1818

19-
@Value("${aws.access.key}")
19+
@Value("${aws.credentials.access-key}")
2020
private String awsAccessKey;
2121

22-
@Value("${aws.access.secret-key}")
22+
@Value("${aws.credentials.secret-key}")
2323
private String awsSecretKey;
2424

2525
@Value("${aws.dynamodb.endpoint}")
@@ -48,8 +48,7 @@ AmazonDynamoDB buildAmazonDynamoDB() {
4848
awsRegion
4949
)
5050
)
51-
.withCredentials(
52-
(awsAccessKey == null && awsSecretKey == null) ? null : amazonAWSCredentialsProvider())
51+
.withCredentials(amazonAWSCredentialsProvider())
5352
.build();
5453
}
5554

src/main/java/dev/ancaghenade/shipmentlistdemo/controller/ShipmentController.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public void uploadShipmentImage(@PathVariable("shipmentId") String shipmentId,
4141

4242

4343
@GetMapping(
44-
path = "{shipmentId}/image/download")
44+
path = "{shipmentId}/image/download", produces = MediaType.IMAGE_JPEG_VALUE)
4545
public byte[] downloadShipmentImage(@PathVariable("shipmentId") String shipmentId) {
4646
return shipmentService.downloadShipmentImage(shipmentId);
4747
}

src/main/java/dev/ancaghenade/shipmentlistdemo/repository/S3StorageService.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public byte[] download(String path, String key) {
4343
} catch (AmazonServiceException e) {
4444
throw new IllegalStateException("Failed to download file.", e);
4545
} catch (IOException e) {
46-
throw new RuntimeException(e);
46+
return new byte[0];
4747
}
4848
}
4949
}

src/main/java/dev/ancaghenade/shipmentlistdemo/service/ShipmentService.java

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
import java.util.Map;
1414
import java.util.Optional;
1515
import java.util.UUID;
16-
import org.apache.http.entity.ContentType;
1716
import org.springframework.beans.factory.annotation.Autowired;
1817
import org.springframework.stereotype.Service;
1918
import org.springframework.web.multipart.MultipartFile;
@@ -44,8 +43,6 @@ public void uploadShipmentImage(String shipmentId, MultipartFile file) {
4443

4544
checkIfFileIsEmpty(file);
4645

47-
checkIfFileIsImage(file);
48-
4946
Shipment shipment = getShipment(shipmentId);
5047

5148
Map<String, String> metadata = getMetadata(file);
@@ -92,17 +89,6 @@ private Shipment getShipment(String shipmentId) {
9289
() -> new IllegalStateException(format("Shipment %s was not found.", shipmentId)));
9390
}
9491

95-
private void checkIfFileIsImage(MultipartFile file) {
96-
if (!List.of(ContentType.IMAGE_JPEG.getMimeType(),
97-
ContentType.IMAGE_PNG.getMimeType(),
98-
ContentType.IMAGE_BMP.getMimeType(),
99-
ContentType.IMAGE_GIF.getMimeType())
100-
.contains(file.getContentType())) {
101-
throw new IllegalStateException(
102-
"File format not accepted.");
103-
}
104-
}
105-
10692
private void checkIfFileIsEmpty(MultipartFile file) {
10793
if (file.isEmpty()) {
10894
throw new IllegalStateException(
Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,12 @@
11
server:
22
port: 8081
33

4-
// Localstack TODO + add Lambda
4+
aws:
5+
credentials:
6+
access-key: test_access_key
7+
secret-key: test_secret_access_key
8+
s3:
9+
endpoint: https://s3.localhost.localstack.cloud:4566/
10+
dynamodb:
11+
endpoint: https://dynamodb.localhost.localstack.cloud:4566/
12+
region: eu-central-1

0 commit comments

Comments
 (0)