|
@@ -0,0 +1,113 @@
|
|
|
+package com.futu.course.minio.service.impl;
|
|
|
+
|
|
|
+import com.futu.course.minio.config.MinioConfig;
|
|
|
+import com.futu.course.minio.config.MinioConfigProperties;
|
|
|
+import io.minio.MinioClient;
|
|
|
+import io.minio.ObjectWriteResponse;
|
|
|
+import io.minio.PutObjectArgs;
|
|
|
+import io.minio.errors.*;
|
|
|
+import lombok.SneakyThrows;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
|
|
+import org.springframework.context.annotation.Import;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.web.multipart.MultipartFile;
|
|
|
+
|
|
|
+import java.io.IOException;
|
|
|
+import java.io.InputStream;
|
|
|
+import java.security.InvalidKeyException;
|
|
|
+import java.security.NoSuchAlgorithmException;
|
|
|
+import java.util.UUID;
|
|
|
+
|
|
|
+@Service
|
|
|
+@Import(MinioConfig.class)
|
|
|
+@EnableConfigurationProperties(MinioConfigProperties.class)
|
|
|
+public class FileServiceImpl {
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private MinioClient minioClient;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private MinioConfigProperties minioConfigProperties;
|
|
|
+
|
|
|
+
|
|
|
+ @SneakyThrows
|
|
|
+ public String uploadHtml(MultipartFile multipartFile) {
|
|
|
+ InputStream inputStream = multipartFile.getInputStream();
|
|
|
+ if (multipartFile.getSize()<=0 || multipartFile==null){
|
|
|
+ return "文件为空";
|
|
|
+ }
|
|
|
+ String originalFilename = multipartFile.getOriginalFilename();
|
|
|
+ String filename = UUID.randomUUID().toString().replace("-", "") +originalFilename;
|
|
|
+
|
|
|
+ PutObjectArgs putObjectArgs = PutObjectArgs.builder()
|
|
|
+ .bucket(minioConfigProperties.getBucketName())
|
|
|
+ .contentType("text/html")
|
|
|
+ .object(filename)
|
|
|
+ .stream(inputStream, inputStream.available(), -1)
|
|
|
+ .build();
|
|
|
+ ObjectWriteResponse response = minioClient.putObject(putObjectArgs);
|
|
|
+ //获取地址
|
|
|
+ String path = minioClient.getObjectUrl(putObjectArgs.bucket(), putObjectArgs.object());
|
|
|
+
|
|
|
+ return path;
|
|
|
+ }
|
|
|
+ @SneakyThrows
|
|
|
+ public String uploadHtml(String fileName, InputStream inputStream){
|
|
|
+ PutObjectArgs putObjectArgs = PutObjectArgs.builder()
|
|
|
+ .bucket(minioConfigProperties.getBucketName())
|
|
|
+ .contentType("text/html")
|
|
|
+ .object(fileName)
|
|
|
+ .stream(inputStream, inputStream.available(), -1)
|
|
|
+ .build();
|
|
|
+
|
|
|
+ ObjectWriteResponse response = minioClient.putObject(putObjectArgs);
|
|
|
+
|
|
|
+ //获取地址
|
|
|
+ String path = minioClient.getObjectUrl(putObjectArgs.bucket(), putObjectArgs.object());
|
|
|
+
|
|
|
+ return path;
|
|
|
+ }
|
|
|
+
|
|
|
+ public String uploadImage(MultipartFile multipartFile) throws IOException, ServerException, InvalidBucketNameException, InsufficientDataException, ErrorResponseException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException, IOException, ServerException, InvalidBucketNameException, InsufficientDataException, ErrorResponseException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException {
|
|
|
+ InputStream inputStream = multipartFile.getInputStream();
|
|
|
+
|
|
|
+ if (multipartFile.getSize()<=0 || multipartFile==null){
|
|
|
+ return "文件为空";
|
|
|
+ }
|
|
|
+// 获取原始文件名
|
|
|
+ String originalFilename = multipartFile.getOriginalFilename();
|
|
|
+// 重新生成文件名
|
|
|
+ String filename = UUID.randomUUID().toString().replace("-", "") +originalFilename;
|
|
|
+
|
|
|
+ PutObjectArgs putObjectArgs=PutObjectArgs.builder()
|
|
|
+ .contentType("image/jpg").bucket(minioConfigProperties.getBucketName()).object(filename)
|
|
|
+ .stream(inputStream,inputStream.available(),-1)
|
|
|
+ .build();
|
|
|
+
|
|
|
+ ObjectWriteResponse response = minioClient.putObject(putObjectArgs);
|
|
|
+
|
|
|
+ //获取地址
|
|
|
+ String path = minioClient.getObjectUrl(putObjectArgs.bucket(), putObjectArgs.object());
|
|
|
+
|
|
|
+ return path;
|
|
|
+ }
|
|
|
+ public String uploadImage(String fileName, InputStream inputStream) throws IOException, ServerException, InvalidBucketNameException, InsufficientDataException, ErrorResponseException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException {
|
|
|
+ PutObjectArgs putObjectArgs = PutObjectArgs.builder()
|
|
|
+ .contentType("image/jpg").bucket(minioConfigProperties.getBucketName()).object(fileName)
|
|
|
+ .stream(inputStream, inputStream.available(), -1)
|
|
|
+ .build();
|
|
|
+
|
|
|
+ ObjectWriteResponse response = minioClient.putObject(putObjectArgs);
|
|
|
+
|
|
|
+
|
|
|
+ //获取地址
|
|
|
+ String path = minioClient.getObjectUrl(putObjectArgs.bucket(), putObjectArgs.object());
|
|
|
+
|
|
|
+ return path;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+}
|