Last active
August 23, 2018 08:03
-
-
Save faulomi/ecd3552645626c64e564e75bfd7e719f to your computer and use it in GitHub Desktop.
Minio Service
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| minio.accessKey=finwizminio | |
| minio.secretKey=finwizminio | |
| minio.endpoint=http://finwiz-development.grouplease.co.th:9000/ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package com.gl.finwiz.common.services; | |
| import java.io.Serializable; | |
| import lombok.Value; | |
| @Value | |
| public class DocumentDescriptor implements Serializable { | |
| private String bucketId; | |
| private String documentId; | |
| private String uri; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package com.gl.finwiz.common.services; | |
| import com.gl.finwiz.common.services.DocumentDescriptor; | |
| import io.minio.MinioClient; | |
| import io.minio.errors.ErrorResponseException; | |
| import io.minio.errors.InsufficientDataException; | |
| import io.minio.errors.InternalException; | |
| import io.minio.errors.InvalidArgumentException; | |
| import io.minio.errors.InvalidBucketNameException; | |
| import io.minio.errors.NoResponseException; | |
| import io.minio.errors.RegionConflictException; | |
| import java.io.IOException; | |
| import java.io.InputStream; | |
| import java.security.InvalidKeyException; | |
| import java.security.NoSuchAlgorithmException; | |
| import java.util.Objects; | |
| import java.util.UUID; | |
| import java.util.stream.Stream; | |
| import java.util.stream.StreamSupport; | |
| import javax.inject.Inject; | |
| import org.apache.commons.lang.StringUtils; | |
| import org.springframework.stereotype.Component; | |
| import org.xmlpull.v1.XmlPullParserException; | |
| /** | |
| * Created by jerome on 9/18/17. | |
| */ | |
| @Component | |
| public class DocumentService { | |
| private final MinioClient minioClient; | |
| @Inject | |
| public DocumentService(MinioClient minioClient) { | |
| this.minioClient = minioClient; | |
| } | |
| public DocumentDescriptor uploadDocument(String bucketId, String parentFolderName, | |
| InputStream inputStream, | |
| String contentType) throws IOException, InvalidKeyException, NoSuchAlgorithmException, | |
| InsufficientDataException, InvalidArgumentException, InternalException, NoResponseException, InvalidBucketNameException, | |
| XmlPullParserException, ErrorResponseException, RegionConflictException { | |
| Objects.requireNonNull(bucketId); | |
| if (!minioClient.bucketExists(bucketId)) { | |
| minioClient.makeBucket(bucketId); | |
| } | |
| final String documentId = UUID.randomUUID().toString(); | |
| String objectName = StringUtils.isEmpty(parentFolderName) ? documentId | |
| : parentFolderName + "/" + documentId; | |
| minioClient.putObject(bucketId, | |
| objectName, | |
| inputStream, inputStream.available(), contentType); | |
| return new DocumentDescriptor(bucketId, objectName, | |
| minioClient.getObjectUrl(bucketId, objectName)); | |
| } | |
| public InputStream downloadDocument(DocumentDescriptor descriptor) | |
| throws Exception { | |
| return minioClient.getObject(descriptor.getBucketId(), descriptor.getDocumentId()); | |
| } | |
| public void deleteDocument(DocumentDescriptor documentDescriptor) throws Exception { | |
| minioClient | |
| .removeObject(documentDescriptor.getBucketId(), documentDescriptor.getDocumentId()); | |
| } | |
| public Stream<DocumentDescriptor> listDocuments(String bucketId) throws Exception { | |
| return StreamSupport.stream(minioClient.listObjects(bucketId).spliterator(), false) | |
| .map(itemResult -> { | |
| try { | |
| return new DocumentDescriptor(bucketId, | |
| itemResult.get().objectName(), | |
| minioClient.getObjectUrl(bucketId, itemResult.get().objectName())); | |
| } catch (Exception e) { | |
| throw new RuntimeException(e); | |
| } | |
| }); | |
| } | |
| void deleteAllDocuments(String bucketId, boolean keepBucket) throws Exception { | |
| if (!minioClient.bucketExists(bucketId)) { | |
| return; | |
| } | |
| minioClient.listObjects(bucketId).forEach(itemResult -> { | |
| try { | |
| minioClient.removeObject(bucketId, itemResult.get().objectName()); | |
| } catch (Exception e) { | |
| throw new RuntimeException(e); | |
| } | |
| }); | |
| if (!keepBucket) { | |
| minioClient.removeBucket(bucketId); | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package com.gl.finwiz.common.config; | |
| import io.minio.MinioClient; | |
| import io.minio.errors.InvalidEndpointException; | |
| import io.minio.errors.InvalidPortException; | |
| import java.util.List; | |
| import javax.inject.Inject; | |
| import org.springframework.context.annotation.Bean; | |
| import org.springframework.context.annotation.Configuration; | |
| /** | |
| * Created by jerome on 4/1/16. | |
| */ | |
| @Configuration | |
| public class MinioConfiguration { | |
| @Bean | |
| public MinioClient minioClient(@Value("${minio.endpoint}") String endpoint,@Value("${minio.accessKey}") String accessKey, @Value("${minio.secretKey}") String secretKey) | |
| throws InvalidPortException, InvalidEndpointException { | |
| return new MinioClient(endpoint, accessKey, secretKey); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment