Skip to content

Commit

Permalink
Fixing an encoding issue for S3
Browse files Browse the repository at this point in the history
  • Loading branch information
straumat committed Jan 4, 2024
1 parent c215c8f commit 5a1054e
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,13 @@
import io.minio.MinioClient;
import io.minio.PutObjectArgs;
import lombok.NonNull;
import org.apache.commons.io.IOUtils;
import org.apache.tika.Tika;
import org.royllo.explorer.core.util.base.BaseService;
import org.royllo.explorer.core.util.parameters.S3Parameters;
import org.springframework.context.annotation.Profile;
import org.springframework.stereotype.Service;

import static java.nio.charset.StandardCharsets.UTF_8;
import java.io.ByteArrayInputStream;


/**
Expand Down Expand Up @@ -57,7 +56,7 @@ public void storeFile(final byte[] fileContent, @NonNull final String fileName)
try {
minioClient.putObject(PutObjectArgs.builder()
.bucket(s3Parameters.getBucketName())
.object(fileName).stream(IOUtils.toInputStream(new String(fileContent), UTF_8), -1, PART_SIZE)
.object(fileName).stream(new ByteArrayInputStream(fileContent), fileContent.length, -1)
.contentType(new Tika().detect(fileContent))
.build());
} catch (Exception e) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.royllo.explorer.core.test.integration.storage;

import io.minio.BucketExistsArgs;
import io.minio.GetObjectArgs;
import io.minio.MakeBucketArgs;
import io.minio.MinioClient;
import io.minio.StatObjectArgs;
Expand All @@ -11,6 +12,7 @@
import io.minio.errors.MinioException;
import io.minio.errors.ServerException;
import io.minio.errors.XmlParserException;
import org.apache.commons.codec.DecoderException;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.DisplayName;
Expand All @@ -26,9 +28,12 @@
import org.testcontainers.containers.MinIOContainer;

import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;

Expand Down Expand Up @@ -61,7 +66,7 @@ public class S3ServiceImplementationTest extends TestWithMockServers {

@Test
@DisplayName("S3 implementation test")
public void s3ImplementationTest() {
public void s3ImplementationTest() throws DecoderException {
// Getting a connexion to do some verification.
MinioClient minioClient = MinioClient.builder()
.endpoint(s3MockServer.getS3URL())
Expand All @@ -84,6 +89,7 @@ public void s3ImplementationTest() {
final S3ServiceImplementation s3ServiceImplementation = (S3ServiceImplementation) contentService;
s3ServiceImplementation.updateS3Parameters(parameters);

// =============================================================================================================
// Checking that a file doesn't exist in minio.
try {
minioClient.statObject(StatObjectArgs.builder().bucket(S3_BUCKET_NAME).object("test.txt").build());
Expand All @@ -95,15 +101,26 @@ public void s3ImplementationTest() {
// Adding the file.
contentService.storeFile("test".getBytes(), "test.txt");

// Adding the file (again) - Checking there is no exception.
contentService.storeFile("test".getBytes(), "test.txt");

// Checking that the file now exists.
try {
// File exists ?
minioClient.statObject(StatObjectArgs.builder().bucket(S3_BUCKET_NAME).object("test.txt").build());

// Retrieving the file.
InputStream stream = minioClient.getObject(
GetObjectArgs.builder()
.bucket(S3_BUCKET_NAME)
.object("test.txt")
.build());
String content = new String(stream.readAllBytes(), StandardCharsets.UTF_8);
assertEquals("test", content);
} catch (MinioException | InvalidKeyException | IOException | NoSuchAlgorithmException e) {
fail("The file should now exist");
}

// Adding the file (again) - Checking there is no exception.
contentService.storeFile("test".getBytes(), "test.txt");
}

@BeforeAll
Expand Down
8 changes: 5 additions & 3 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ services:
- royllo-explorer-network
environment:
- TZ=Europe/Paris
- ROYLLO_EXPLORER_CONTENT_BASE_URL=http://localhost:9000/royllo-explorer
- SPRING_DATASOURCE_DRIVER_CLASS_NAME=org.postgresql.Driver
- SPRING_DATASOURCE_URL=jdbc:postgresql://royllo-explorer-database-server/royllo_explorer_database
- SPRING_DATASOURCE_USERNAME=royllo_explorer_username
Expand All @@ -95,9 +96,8 @@ services:
- S3_SECRET_KEY=tTX4LD7gYaQbhZ
- S3_BUCKET_NAME=royllo-explorer
- S3_ENDPOINTURL=http://royllo-explorer-storage-server:9000

# ====================================================================================================================
# Royllo explorer API server ( https://hub.docker.com/r/royllo/explorer-api ).
- # ====================================================================================================================
# Royllo explorer API server ( https://hub.docker.com/r/royllo/explorer-api ).
royllo-explorer-api-server:
image: royllo/explorer-api:latest
depends_on:
Expand All @@ -108,6 +108,7 @@ services:
- "9090:8080"
environment:
- TZ=Europe/Paris
- ROYLLO_EXPLORER_CONTENT_BASE_URL=http://localhost:9000/royllo-explorer
- SPRING_DATASOURCE_DRIVER_CLASS_NAME=org.postgresql.Driver
- SPRING_DATASOURCE_URL=jdbc:postgresql://royllo-explorer-database-server/royllo_explorer_database
- SPRING_DATASOURCE_USERNAME=royllo_explorer_username
Expand All @@ -131,6 +132,7 @@ services:
- "8080:8080"
environment:
- TZ=Europe/Paris
- ROYLLO_EXPLORER_CONTENT_BASE_URL=http://localhost:9000/royllo-explorer
- SPRING_DATASOURCE_DRIVER_CLASS_NAME=org.postgresql.Driver
- SPRING_DATASOURCE_URL=jdbc:postgresql://royllo-explorer-database-server/royllo_explorer_database
- SPRING_DATASOURCE_USERNAME=royllo_explorer_username
Expand Down

0 comments on commit 5a1054e

Please sign in to comment.