Skip to content

SeniorityMeter/sm-spring-storage

Repository files navigation

logo

Seniority Meter

Spring Storage

Description

This is a simple storage SDK for Spring Boot applications. It provides a simple way to store and retrieve files from the file system.


How to use

1. Add the following parent to your pom.xml file:

<parent>
    <groupId>br.com.senioritymeter</groupId>
    <artifactId>parent</artifactId>
    <version>1.0.1</version>
</parent>

2. add scanBasePackages to your SpringBootApplication

@SpringBootApplication(scanBasePackages = {"br.com.senioritymeter", "your.package.name.here"})

3. Add the following dependency to your pom.xml file:

<dependencies>
    <dependency>
        <groupId>br.com.senioritymeter</groupId>
        <artifactId>storage</artifactId>
        <version>1.0.3</version>
    </dependency>
</dependencies>

4. Add the following properties to your application.yaml file:

a - Configuration for AWS S3:
spring:
  cloud:
    aws:
      credentials:
        access-key: ${AWS_ACCESS_KEY:sm-spring-storage-access-key}
        secret-key: ${AWS_SECRET_KEY:sm-spring-storage-secret-key}
  storage:
    aws-s3:
      enabled: true
      region: ${AWS_REGION:sa-east-1}
      bucket:
        name: ${AWS_S3_BUCKET_NAME:sm-spring-storage-bucket-name}

5. Use the StorageCreation to save files:

Inject the StorageCreation bean in your class and use it to save files.

private final StorageCreation storageCreation;

Prepare your payload and call the execute method.

final var input = StorageCreation.Input.builder()
    .fileArray(new byte[0])
    .filename("filename.extension")
    .domain("path/on/aws") // without "/" at the beginning and at the end
    .type(StorageType.AWS_S3)
    .build();

var output = storageCreation.execute(input);

6. Use the StorageRetrieval to get files:

Inject the StorageRetrieval bean in your class and use it to get files.

private final StorageRetrieval storageRetrieval;

Prepare your payload and call the execute method.

final var input = StorageRetrieval.Input.builder()
    .key("path/on/aws/filename.extension")
    .type(StorageType.AWS_S3)
    .build();

final var output = storageRetrieval.execute(input);

The response will contain the URI of the file stored. You can get it using the following code:

output.getUri();

7. Use the StorageRemoval to delete files:

Inject the StorageRemoval bean in your class and use it to delete files.

private final StorageRemoval storageRemoval;

Prepare your payload and call the execute method.

final var input = StorageRemoval.Input.builder()
    .key("path/on/aws/filename.extension")
    .type(StorageType.AWS_S3)
    .build();
    
storageRemoval.execute(input);

About

Seniority Meter - Spring Storage

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published