|
@@ -0,0 +1,118 @@
|
|
|
+package com.nokia.financeapi.service.common.file;
|
|
|
+
|
|
|
+import com.nokia.financeapi.common.exception.MyRuntimeException;
|
|
|
+import com.nokia.financeapi.config.MinioConfig;
|
|
|
+import io.minio.GetPresignedObjectUrlArgs;
|
|
|
+import io.minio.MinioClient;
|
|
|
+import io.minio.ObjectWriteResponse;
|
|
|
+import io.minio.PutObjectArgs;
|
|
|
+import io.minio.errors.ErrorResponseException;
|
|
|
+import io.minio.errors.InsufficientDataException;
|
|
|
+import io.minio.errors.InternalException;
|
|
|
+import io.minio.errors.InvalidResponseException;
|
|
|
+import io.minio.errors.ServerException;
|
|
|
+import io.minio.errors.XmlParserException;
|
|
|
+import io.minio.http.Method;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.springframework.context.annotation.Primary;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.util.StringUtils;
|
|
|
+
|
|
|
+import java.io.IOException;
|
|
|
+import java.io.InputStream;
|
|
|
+import java.security.InvalidKeyException;
|
|
|
+import java.security.NoSuchAlgorithmException;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.Map;
|
|
|
+import java.util.concurrent.TimeUnit;
|
|
|
+
|
|
|
+@Primary
|
|
|
+@Slf4j
|
|
|
+@Service
|
|
|
+public class MinioServiceImpl implements FileService {
|
|
|
+
|
|
|
+ private final MinioClient minioClient;
|
|
|
+ private final MinioConfig minioConfig;
|
|
|
+
|
|
|
+ public MinioServiceImpl(MinioClient minioClient, MinioConfig minioConfig) {
|
|
|
+ this.minioClient = minioClient;
|
|
|
+ this.minioConfig = minioConfig;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取下载地址
|
|
|
+ *
|
|
|
+ * @param object 对象名称
|
|
|
+ * @return {@link String}
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public String getDownloadUrl(String object) {
|
|
|
+ return getDownloadUrl(object, minioConfig.getExpiry(), TimeUnit.MINUTES);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取下载地址
|
|
|
+ *
|
|
|
+ * @param object 对象名称
|
|
|
+ * @param duration 过期时间
|
|
|
+ * @param unit 单位
|
|
|
+ * @return {@link String}
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public String getDownloadUrl(String object, int duration, TimeUnit unit) {
|
|
|
+ try {
|
|
|
+ return StringUtils.delete(minioClient.getPresignedObjectUrl(GetPresignedObjectUrlArgs.builder()
|
|
|
+ .expiry(duration, unit)
|
|
|
+ .bucket(minioConfig.getBucket())
|
|
|
+ .object(object)
|
|
|
+ .method(Method.GET)
|
|
|
+ .build()), minioConfig.getEndpoint());
|
|
|
+ } catch (ErrorResponseException | InsufficientDataException | InternalException | InvalidKeyException |
|
|
|
+ InvalidResponseException | IOException | NoSuchAlgorithmException | XmlParserException |
|
|
|
+ ServerException e) {
|
|
|
+ throw new MyRuntimeException(e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 从流中上传数据到对象
|
|
|
+ *
|
|
|
+ * @param object 对象名称
|
|
|
+ * @param stream 流
|
|
|
+ * @param objectSize 对象大小
|
|
|
+ * @return {@link Map}<{@link String}, {@link Object}>
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public Map<String, Object> putObject(String object, InputStream stream, long objectSize, Map<String, String> tags) {
|
|
|
+ try {
|
|
|
+ ObjectWriteResponse response = minioClient.putObject(PutObjectArgs.builder()
|
|
|
+ .bucket(minioConfig.getBucket())
|
|
|
+ .object(object)
|
|
|
+ .stream(stream, objectSize, -1)
|
|
|
+ .tags(tags)
|
|
|
+ .build());
|
|
|
+ Map<String, Object> map = new HashMap<>();
|
|
|
+ map.put("object", response.object());
|
|
|
+ map.put("bucket", response.bucket());
|
|
|
+ map.put("etag", response.etag());
|
|
|
+ map.put("versionId", response.versionId());
|
|
|
+ map.put("headers", response.headers());
|
|
|
+ map.put("region", response.region());
|
|
|
+ return map;
|
|
|
+ } catch (ErrorResponseException | InsufficientDataException | InternalException | InvalidKeyException |
|
|
|
+ InvalidResponseException | IOException | NoSuchAlgorithmException | ServerException |
|
|
|
+ XmlParserException e) {
|
|
|
+ throw new MyRuntimeException(e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取存储桶名称
|
|
|
+ *
|
|
|
+ * @return {@link String}
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public String getBucket() {
|
|
|
+ return minioConfig.getBucket();
|
|
|
+ }
|
|
|
+}
|