BAEL-6417: MinIO Java SDK examples (#14048)
* BAEL-6417: MinIO Java SDK examples * BAEL-6417 Add Live test * BAEL-6417 Rename live test class * BAEL-6417 Create property for minio version
This commit is contained in:
parent
17cdc7c745
commit
11469c7c85
|
@ -116,6 +116,11 @@
|
|||
<artifactId>guava</artifactId>
|
||||
<version>${guava.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.minio</groupId>
|
||||
<artifactId>minio</artifactId>
|
||||
<version>${minio.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.awaitility</groupId>
|
||||
<artifactId>awaitility</artifactId>
|
||||
|
@ -177,6 +182,7 @@
|
|||
<awaitility.version>3.0.0</awaitility.version>
|
||||
<univocity.version>2.8.4</univocity.version>
|
||||
<swagger-parser.version>2.1.13</swagger-parser.version>
|
||||
<minio.version>8.5.2</minio.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
</project>
|
||||
|
|
|
@ -0,0 +1,35 @@
|
|||
package com.baeldung.minio;
|
||||
|
||||
import io.minio.GetObjectArgs;
|
||||
import io.minio.MakeBucketArgs;
|
||||
import io.minio.MinioClient;
|
||||
import io.minio.UploadObjectArgs;
|
||||
|
||||
public class MinIOService {
|
||||
private MinioClient minioClient =
|
||||
MinioClient.builder()
|
||||
.endpoint("http://127.0.0.1:9000")
|
||||
.credentials("minioadmin", "minioadmin")
|
||||
.build();
|
||||
|
||||
public void doSomething() throws Exception {
|
||||
minioClient.makeBucket(
|
||||
MakeBucketArgs
|
||||
.builder()
|
||||
.bucket("user2")
|
||||
.build());
|
||||
|
||||
minioClient.uploadObject(
|
||||
UploadObjectArgs
|
||||
.builder()
|
||||
.bucket("user2")
|
||||
.filename("/tmp/Resume.pdf")
|
||||
.build());
|
||||
|
||||
minioClient.getObject(
|
||||
GetObjectArgs.builder()
|
||||
.bucket("user2")
|
||||
.object("Resume.pdf")
|
||||
.build());
|
||||
}
|
||||
}
|
|
@ -0,0 +1,73 @@
|
|||
package com.baeldung.minio;
|
||||
|
||||
import io.minio.*;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.*;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
public class MinIOServiceLiveTest {
|
||||
|
||||
private MinioClient minioClient;
|
||||
|
||||
private final String BUCKET_NAME = "livetest";
|
||||
|
||||
private final String OBJECT_NAME = "minio_sample.txt";
|
||||
|
||||
private final String CONTENTS = "hello";
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
minioClient =
|
||||
MinioClient.builder()
|
||||
.endpoint("http://127.0.0.1:9000")
|
||||
.credentials("minioadmin", "minioadmin")
|
||||
.build();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenFile_whenUploadAndRetrive_thenContentsAreCorrect() throws Exception {
|
||||
|
||||
if(!minioClient.bucketExists(BucketExistsArgs
|
||||
.builder()
|
||||
.bucket(BUCKET_NAME)
|
||||
.build())) {
|
||||
|
||||
minioClient.makeBucket(MakeBucketArgs
|
||||
.builder()
|
||||
.bucket(BUCKET_NAME)
|
||||
.build());
|
||||
}
|
||||
|
||||
minioClient.putObject(PutObjectArgs
|
||||
.builder()
|
||||
.bucket(BUCKET_NAME)
|
||||
.object(OBJECT_NAME)
|
||||
.stream(new ByteArrayInputStream(CONTENTS.getBytes()), CONTENTS.length(),-1)
|
||||
.build());
|
||||
|
||||
try (InputStream stream =
|
||||
minioClient.getObject(GetObjectArgs
|
||||
.builder()
|
||||
.bucket(BUCKET_NAME)
|
||||
.object(OBJECT_NAME)
|
||||
.build())) {
|
||||
|
||||
byte[] obj = new byte[CONTENTS.length()];
|
||||
stream.read(obj);
|
||||
assertEquals(CONTENTS, new String(obj));
|
||||
}
|
||||
catch (IOException e) {
|
||||
|
||||
}
|
||||
finally {
|
||||
minioClient.removeObject(RemoveObjectArgs
|
||||
.builder()
|
||||
.bucket(BUCKET_NAME)
|
||||
.object(OBJECT_NAME)
|
||||
.build());
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue