Skip to content
This repository has been archived by the owner on Mar 5, 2021. It is now read-only.

Commit

Permalink
add minio store, remove superfluous constructors
Browse files Browse the repository at this point in the history
  • Loading branch information
wowselim committed Jan 29, 2019
1 parent 5a5211d commit e4dd235
Show file tree
Hide file tree
Showing 4 changed files with 150 additions and 11 deletions.
3 changes: 2 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
group 'co.selim'
version '2.0.0'
version '2.1.0'

apply plugin: 'java'

Expand All @@ -13,6 +13,7 @@ repositories {
dependencies {
compileOnly('com.aliyun.oss:aliyun-sdk-oss:3.4.0')
compileOnly('com.amazonaws:aws-java-sdk-s3:1.11.483')
compileOnly('io.minio:minio:6.0.0')
testImplementation('junit:junit:4.12')
testImplementation('com.github.marschall:memoryfilesystem:1.2.0')
}
142 changes: 142 additions & 0 deletions src/main/java/co/selim/gimbap/MinioStore.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
package co.selim.gimbap;

import co.selim.gimbap.api.StreamingStore;
import co.selim.gimbap.util.IDGenerator;
import co.selim.gimbap.util.IOStreamUtils;
import io.minio.MinioClient;
import io.minio.Result;
import io.minio.messages.Item;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashSet;
import java.util.Set;
import java.util.function.Supplier;
import java.util.stream.Collectors;

/**
* A Minio-backed StreamingStore implementation.
* This implementation does not allow updates on objects that do not exist.
*/
public class MinioStore implements StreamingStore<byte[]> {
private final MinioClient minioClient;
private final String bucketName;
private final Supplier<String> idSupplier = () -> IDGenerator.generate(24);

public MinioStore(MinioClient minioClient, String bucketName) {
this.minioClient = minioClient;
this.bucketName = bucketName;
}

@Override
public String putStream(InputStream inputStream) {
String id = getNewId();
return doPutStream(id, inputStream);
}

private String getNewId() {
String id = idSupplier.get();
while (exists(id)) {
id = idSupplier.get();
}
return id;
}

private String doPutStream(String id, InputStream inputStream) {
try {
minioClient.putObject(bucketName, id, inputStream, "application/octet-stream");
} catch (Exception e) {
throw new RuntimeException(e);
}
return id;
}

@Override
public InputStream getStream(String id) {
try {
return minioClient.getObject(bucketName, id);
} catch (Exception e) {
throw new RuntimeException(e);
}
}

@Override
public Iterable<Supplier<InputStream>> getAllStream() {
return keySet().stream()
.map(key -> (Supplier<InputStream>) () -> MinioStore.this.getStream(key))
.collect(Collectors.toList());
}

@Override
public void updateStream(String id, InputStream inputStream) throws IllegalStateException {
if (!exists(id)) {
throw new IllegalStateException("The target object does not exist");
}
doPutStream(id, inputStream);
}

@Override
public String put(byte[] object) {
String id = getNewId();
return doPutStream(id, new ByteArrayInputStream(object));
}

@Override
public byte[] get(String id) {
try (InputStream objectInputStream = getStream(id)) {
return IOStreamUtils.getBytesFromInputStream(objectInputStream);
} catch (IOException e) {
throw new RuntimeException(e);
}
}

@Override
public Iterable<Supplier<byte[]>> getAll() {
return keySet().stream()
.map(key -> (Supplier<byte[]>) () -> MinioStore.this.get(key))
.collect(Collectors.toList());
}

@Override
public void update(String id, byte[] object) throws IllegalStateException {
if (!exists(id)) {
throw new IllegalStateException("The target object does not exist");
}
doPutStream(id, new ByteArrayInputStream(object));
}

@Override
public void delete(String id) {
try {
minioClient.removeObject(bucketName, id);
} catch (Exception e) {
throw new RuntimeException(e);
}
}

@Override
public Set<String> keySet() {
try {
Set<String> keySet = new HashSet<>();
for (Result<Item> itemResult : minioClient.listObjects(bucketName)) {
keySet.add(itemResult.get().objectName());
}
return keySet;
} catch (Exception e) {
throw new RuntimeException(e);
}
}

@Override
public boolean exists(String id) {
return keySet().contains(id);
}

/**
* Does nothing.
*/
@Override
public void close() {
}
}
7 changes: 4 additions & 3 deletions src/main/java/co/selim/gimbap/OSSStore.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,16 @@
import java.util.stream.Collectors;

/**
* An OSS-backed Store implementation. This implementation does not allow updates on objects that do not exist.
* An OSS-backed StreamingStore implementation.
* This implementation does not allow updates on objects that do not exist.
*/
public class OSSStore implements StreamingStore<byte[]> {
private final OSS client;
private final String bucketName;
private final int idLength = 24;

public OSSStore(String endpoint, String bucketName, DefaultCredentialProvider credentialProvider) {
this.client = new OSSClientBuilder().build(endpoint, credentialProvider);
public OSSStore(OSS client, String bucketName) {
this.client = client;
this.bucketName = bucketName;
}

Expand Down
9 changes: 2 additions & 7 deletions src/main/java/co/selim/gimbap/S3Store.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,8 @@ public class S3Store implements Store<byte[]> {
private final String bucketName;
private final int idLength = 24;

public S3Store(AwsClientBuilder.EndpointConfiguration endpointConfiguration,
AWSStaticCredentialsProvider credentialsProvider,
String bucketName) {
this.s3Client = AmazonS3ClientBuilder.standard()
.withEndpointConfiguration(endpointConfiguration)
.withCredentials(credentialsProvider)
.build();
public S3Store(AmazonS3 s3Client, String bucketName) {
this.s3Client = s3Client;
this.bucketName = bucketName;
}

Expand Down

0 comments on commit e4dd235

Please sign in to comment.