Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add exist method on BlobStoreRepository #312

Merged
merged 1 commit into from
Dec 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@
<hazelcast-kubernetes.version>2.2.3</hazelcast-kubernetes.version>
<httpclient5.version>5.4.1</httpclient5.version>
<hamcrest.version>3.0</hamcrest.version>

<jakarta.annotation-api.version>3.0.0</jakarta.annotation-api.version>
<maven-gpg-plugin.version>1.6</maven-gpg-plugin.version>


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import org.rutebanken.helper.storage.BlobStoreException;
import org.rutebanken.helper.storage.model.BlobDescriptor;
import org.rutebanken.helper.storage.repository.BlobStoreRepository;
import software.amazon.awssdk.awscore.exception.AwsServiceException;
import software.amazon.awssdk.core.exception.SdkClientException;
import software.amazon.awssdk.core.sync.RequestBody;
import software.amazon.awssdk.core.sync.ResponseTransformer;
import software.amazon.awssdk.services.s3.S3Client;
Expand Down Expand Up @@ -49,10 +51,14 @@ public S3BlobStoreRepository(S3Client s3Client) {

@Override
public InputStream getBlob(String objectName) {
return s3Client.getObject(
GetObjectRequest.builder().bucket(containerName).key(objectName).build(),
ResponseTransformer.toInputStream()
);
try {
return s3Client.getObject(
GetObjectRequest.builder().bucket(containerName).key(objectName).build(),
ResponseTransformer.toInputStream()
);
} catch (NoSuchKeyException e) {
return null;
}
Comment on lines +59 to +61
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks fine to me 👍 This is a bit of a wart in AWS S3 client - using exceptions for flow control, that is - so handling it like this is fine.

Of course returning an Optional<InputStream> could express the semantics of the return value better, but the current implementation and the other changes in the PR are less intrusive and won't cause issues downstream, so I'm fine with this.

}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ public void setContainerName(String containerName) {
this.containerName = containerName;
}

@Override
public boolean exist(String objectName) {
return BlobStoreHelper.existBlob(storage, containerName, objectName);
}

@Override
public InputStream getBlob(String name) {
return BlobStoreHelper.getBlob(storage, containerName, name);
Expand Down
5 changes: 5 additions & 0 deletions storage/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ Inspired by: https://github.com/fabric8io/ipaas-quickstarts/
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
</dependency>
<dependency>
<groupId>jakarta.annotation</groupId>
<artifactId>jakarta.annotation-api</artifactId>
<version>${jakarta.annotation-api.version}</version>
</dependency>

<!-- test dependencies -->
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package org.rutebanken.helper.storage.repository;

import jakarta.annotation.Nullable;
import org.rutebanken.helper.storage.BlobAlreadyExistsException;
import org.rutebanken.helper.storage.model.BlobDescriptor;

Expand All @@ -31,12 +32,22 @@
public interface BlobStoreRepository {


/**
* Return true if the given blob exists in the repository.
* The default implementation retrieves the object and test for nullity.
* Specific implementations can provide an optimized algorithm.
*/
default boolean exist(String objectName) {
return getBlob(objectName) != null;
}

/**
* Download a blob from storage.
*
* @param objectName the name of the blob
* @return an InputStream on the file content.
* @return an InputStream on the file content or null if the object does not exist.
*/
@Nullable
InputStream getBlob(String objectName);

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package org.rutebanken.helper.storage.repository;

import org.junit.jupiter.api.Test;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;

import static org.junit.jupiter.api.Assertions.*;
class InMemoryBlobStoreRepositoryTest {

public static final String BLOB_NAME = "blobName";
public static final byte[] BLOB_CONTENT = "content".getBytes();

@Test
void testUploadExistAndGet() throws IOException {
InMemoryBlobStoreRepository repository = new InMemoryBlobStoreRepository(new HashMap<>());
repository.uploadBlob(BLOB_NAME, new ByteArrayInputStream(BLOB_CONTENT));
assertTrue(repository.exist(BLOB_NAME));
InputStream blob = repository.getBlob(BLOB_NAME);
assertNotNull(blob);
assertArrayEquals(BLOB_CONTENT, blob.readAllBytes());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package org.rutebanken.helper.storage.repository;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;

import static org.junit.jupiter.api.Assertions.*;

class LocalDiskBlobStoreRepositoryTest {

public static final String BLOB_NAME = "blobName";
public static final byte[] BLOB_CONTENT = "content".getBytes();

@TempDir
File tempDirectory;

@Test
void testUploadExistAndGet() throws IOException {
LocalDiskBlobStoreRepository repository = new LocalDiskBlobStoreRepository(tempDirectory.getAbsolutePath());
repository.uploadBlob(BLOB_NAME, new ByteArrayInputStream(BLOB_CONTENT));
assertTrue(repository.exist(BLOB_NAME));
InputStream blob = repository.getBlob(BLOB_NAME);
assertNotNull(blob);
assertArrayEquals(BLOB_CONTENT, blob.readAllBytes());
}

}
Loading