Added S3Manager.load() method.

This commit is contained in:
Jesse Brault 2024-07-22 17:52:48 -05:00
parent 4f46fce70a
commit bb65ec7d24
3 changed files with 45 additions and 2 deletions

View File

@ -36,14 +36,36 @@ public class MinioS3ManagerTests {
@Test
public void simpleStore() {
try (final InputStream hal9000 = MinioS3ManagerTests.class.getResourceAsStream("HAL9000.svg")) {
final String result = this.s3Manager.store(
final String objectName = this.s3Manager.store(
"test-images",
"HAL9000.svg",
"image/svg+xml",
hal9000,
27881L
);
assertEquals("HAL9000.svg", result);
assertEquals("HAL9000.svg", objectName);
} catch (IOException ioException) {
throw new RuntimeException(ioException);
}
}
@Test
public void simpleLoad() {
try (final InputStream hal9000 = MinioS3ManagerTests.class.getResourceAsStream("HAL9000.svg")) {
if (hal9000 == null) {
throw new RuntimeException("HAL9000.svg could not be found");
}
final String objectName = this.s3Manager.store(
"test-images",
"HAL9000.svg",
"image/svg+xml",
hal9000,
27881L
);
try (final InputStream loadedObject = this.s3Manager.load("test-images", objectName)) {
final byte[] stored = loadedObject.readAllBytes();
assertEquals(27881L, stored.length);
}
} catch (IOException ioException) {
throw new RuntimeException(ioException);
}

View File

@ -22,6 +22,22 @@ public class MinioS3Manager implements S3Manager {
@Value("${app.mealsmadeeasy.api.minio.secretKey}")
private String secretKey;
@Override
public InputStream load(String bucket, String objectName) throws IOException {
try (final MinioClient client = MinioClient.builder()
.endpoint(this.endpoint)
.credentials(this.accessKey, this.secretKey)
.build()
) {
return client.getObject(GetObjectArgs.builder()
.bucket(bucket)
.object(objectName)
.build());
} catch (Exception e) {
throw new IOException(e);
}
}
@Override
public String store(
String bucketName,

View File

@ -5,6 +5,11 @@ import java.io.InputStream;
public interface S3Manager {
InputStream load(
String bucket,
String objectName
) throws IOException;
/**
* @param bucket the target bucket in which to store the content
* @param filename the filename to store, usually a uuid + appropriate extension