feat: 修改模块名称
This commit is contained in:
123
qing-yu-club-oss/pom.xml
Normal file
123
qing-yu-club-oss/pom.xml
Normal file
@@ -0,0 +1,123 @@
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<groupId>com.landaiqing</groupId>
|
||||
<artifactId>qing-yu-club-oss</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<name>qing-yu-club-oss</name>
|
||||
<url>http://maven.apache.org</url>
|
||||
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source>8</maven.compiler.source>
|
||||
<maven.compiler.target>8</maven.compiler.target>
|
||||
<java.version>1.8</java.version>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
|
||||
<spring-boot.version>2.4.2</spring-boot.version>
|
||||
<spring-cloud-alibaba.version>2021.1</spring-cloud-alibaba.version>
|
||||
<spring-cloud.version>2020.0.6</spring-cloud.version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
<exclusions>
|
||||
<exclusion>
|
||||
<artifactId>spring-boot-starter-logging</artifactId>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
</exclusion>
|
||||
</exclusions>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.minio</groupId>
|
||||
<artifactId>minio</artifactId>
|
||||
<version>8.2.0</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
<version>1.18.20</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.alibaba.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-log4j2</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-bootstrap</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.alibaba.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<dependencyManagement>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-dependencies</artifactId>
|
||||
<version>${spring-cloud.version}</version>
|
||||
<type>pom</type>
|
||||
<scope>import</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-dependencies</artifactId>
|
||||
<version>${spring-boot.version}</version>
|
||||
<type>pom</type>
|
||||
<scope>import</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.alibaba.cloud</groupId>
|
||||
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
|
||||
<version>${spring-cloud-alibaba.version}</version>
|
||||
<type>pom</type>
|
||||
<scope>import</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</dependencyManagement>
|
||||
|
||||
<build>
|
||||
<finalName>${project.artifactId}</finalName>
|
||||
<!--打包成jar包时的名字-->
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
<version>2.3.0.RELEASE</version>
|
||||
<executions>
|
||||
<execution>
|
||||
<goals>
|
||||
<goal>repackage</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
<repositories>
|
||||
<repository>
|
||||
<id>central</id>
|
||||
<name>aliyun maven</name>
|
||||
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
|
||||
<layout>default</layout>
|
||||
<releases>
|
||||
<enabled>true</enabled>
|
||||
</releases>
|
||||
<snapshots>
|
||||
<enabled>true</enabled>
|
||||
</snapshots>
|
||||
</repository>
|
||||
</repositories>
|
||||
</project>
|
@@ -0,0 +1,20 @@
|
||||
package com.landaiqing.oss;
|
||||
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
|
||||
/**
|
||||
* oss 服务启动类
|
||||
*
|
||||
* @author: landaiqing
|
||||
* @date: 2024/2/17
|
||||
*/
|
||||
@SpringBootApplication
|
||||
@ComponentScan("com.landaiqing")
|
||||
public class OssApplication {
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(OssApplication.class);
|
||||
}
|
||||
}
|
@@ -0,0 +1,60 @@
|
||||
package com.landaiqing.oss.adapter;
|
||||
|
||||
import com.landaiqing.oss.entity.FileInfo;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import java.io.InputStream;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @Classname AliStorageServiceImpl
|
||||
* @BelongsProject: qing-yu-club
|
||||
* @BelongsPackage: com.landaiqing.oss.service.impl
|
||||
* @Author: landaiqing
|
||||
* @CreateTime: 2024-02-17 16:55
|
||||
* @Description: TODO
|
||||
* @Version: 1.0
|
||||
*/
|
||||
public class AliStorageAdapter implements StorageAdapter {
|
||||
@Override
|
||||
public void createBucket(String bucket) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void uploadFile(MultipartFile uploadFile, String bucket, String objectName) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> getAllBucket() {
|
||||
List<String> bucketNameList = new LinkedList<>();
|
||||
bucketNameList.add("aliyun");
|
||||
return bucketNameList;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<FileInfo> getAllFile(String bucket) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public InputStream download(String bucket, String objectName) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void deleteBucket(String bucket) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void deleteObject(String bucket, String objectName) {
|
||||
|
||||
}
|
||||
@Override
|
||||
public String getUrl(String bucket, String objectName) {
|
||||
return null;
|
||||
}
|
||||
}
|
@@ -0,0 +1,85 @@
|
||||
package com.landaiqing.oss.adapter;
|
||||
|
||||
import com.landaiqing.oss.entity.FileInfo;
|
||||
import com.landaiqing.oss.util.MinioUtil;
|
||||
import lombok.SneakyThrows;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.io.InputStream;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @Classname MinioStorageServiceImpl
|
||||
* @BelongsProject: qing-yu-club
|
||||
* @BelongsPackage: com.landaiqing.oss.service.impl
|
||||
* @Author: landaiqing
|
||||
* @CreateTime: 2024-02-17 16:35
|
||||
* @Description: TODO
|
||||
* @Version: 1.0
|
||||
*/
|
||||
public class MinioStorageAdapter implements StorageAdapter {
|
||||
@Resource
|
||||
private MinioUtil minioUtil;
|
||||
|
||||
/**
|
||||
* minioUrl
|
||||
*/
|
||||
@Value("${minio.url}")
|
||||
private String url;
|
||||
|
||||
@Override
|
||||
@SneakyThrows
|
||||
public void createBucket(String bucket) {
|
||||
minioUtil.createBucket(bucket);
|
||||
}
|
||||
|
||||
@Override
|
||||
@SneakyThrows
|
||||
public void uploadFile(MultipartFile uploadFile, String bucket, String objectName) {
|
||||
minioUtil.createBucket(bucket);
|
||||
if (objectName != null) {
|
||||
minioUtil.uploadFile(uploadFile.getInputStream(), bucket, objectName + "/" + uploadFile.getName());
|
||||
} else {
|
||||
minioUtil.uploadFile(uploadFile.getInputStream(), bucket, uploadFile.getName());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
@SneakyThrows
|
||||
public List<String> getAllBucket() {
|
||||
return minioUtil.getAllBucket();
|
||||
}
|
||||
|
||||
@Override
|
||||
@SneakyThrows
|
||||
public List<FileInfo> getAllFile(String bucket) {
|
||||
return minioUtil.getAllFile(bucket);
|
||||
}
|
||||
|
||||
@Override
|
||||
@SneakyThrows
|
||||
public InputStream download(String bucket, String objectName) {
|
||||
return minioUtil.download(bucket, objectName);
|
||||
}
|
||||
|
||||
@Override
|
||||
@SneakyThrows
|
||||
public void deleteBucket(String bucket) {
|
||||
minioUtil.deleteBucket(bucket);
|
||||
}
|
||||
|
||||
@Override
|
||||
@SneakyThrows
|
||||
public void deleteObject(String bucket, String objectName) {
|
||||
minioUtil.deleteObject(bucket, objectName);
|
||||
}
|
||||
|
||||
@Override
|
||||
@SneakyThrows
|
||||
public String getUrl(String bucket, String objectName) {
|
||||
return url + "/" + bucket + "/" + objectName;
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,83 @@
|
||||
package com.landaiqing.oss.adapter;
|
||||
|
||||
import com.landaiqing.oss.entity.FileInfo;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import java.io.InputStream;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @Classname StorageService
|
||||
* @BelongsProject: qing-yu-club
|
||||
* @BelongsPackage: com.landaiqing.oss.service
|
||||
* @Author: landaiqing
|
||||
* @CreateTime: 2024-02-17 16:32
|
||||
* @Description: 文件存储适配器
|
||||
* @Version: 1.0
|
||||
*/
|
||||
public interface StorageAdapter {
|
||||
/**
|
||||
* @description: 创建Bucket桶
|
||||
* @param: [bucket]
|
||||
* @return: void
|
||||
* @author landaiqing
|
||||
* @date: 2024/2/17 15:53
|
||||
*/
|
||||
void createBucket(String bucket);
|
||||
|
||||
/**
|
||||
* @description: 上传文件
|
||||
* @param: [inputStream, bucket, objectName]
|
||||
* @return: void
|
||||
* @author landaiqing
|
||||
* @date: 2024/2/17 15:58
|
||||
*/
|
||||
void uploadFile(MultipartFile uploadFile, String bucket, String objectName);
|
||||
|
||||
/**
|
||||
* @description: 列出所有桶
|
||||
* @param: []
|
||||
* @return: java.util.List<java.lang.String>
|
||||
* @author landaiqing
|
||||
* @date: 2024/2/17 16:00
|
||||
*/
|
||||
List<String> getAllBucket();
|
||||
|
||||
/**
|
||||
* @description: 列出当前桶及文件
|
||||
* @param: [bucket]
|
||||
* @return: java.util.List<com.landaiqing.oss.entity.FileInfo>
|
||||
* @author landaiqing
|
||||
* @date: 2024/2/17 16:06
|
||||
*/
|
||||
List<FileInfo> getAllFile(String bucket);
|
||||
|
||||
/**
|
||||
* @description: 下载文件
|
||||
* @param: [bucket, objectName]
|
||||
* @return: java.io.InputStream
|
||||
* @author landaiqing
|
||||
* @date: 2024/2/17 16:11
|
||||
*/
|
||||
InputStream download(String bucket, String objectName);
|
||||
|
||||
/**
|
||||
* @description: 删除桶
|
||||
* @param: [bucket]
|
||||
* @return: void
|
||||
* @author landaiqing
|
||||
* @date: 2024/2/17 16:14
|
||||
*/
|
||||
void deleteBucket(String bucket);
|
||||
|
||||
/**
|
||||
* @description: 删除文件
|
||||
* @param: [bucket, objectName]
|
||||
* @return: void
|
||||
* @author landaiqing
|
||||
* @date: 2024/2/17 16:14
|
||||
*/
|
||||
void deleteObject(String bucket, String objectName);
|
||||
|
||||
String getUrl(String bucket, String objectName);
|
||||
}
|
@@ -0,0 +1,43 @@
|
||||
package com.landaiqing.oss.config;
|
||||
|
||||
import io.minio.MinioClient;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
/**
|
||||
* @Classname MinioConfig
|
||||
* @BelongsProject: qing-yu-club
|
||||
* @BelongsPackage: com.landaiqing.oss.config
|
||||
* @Author: landaiqing
|
||||
* @CreateTime: 2024-02-17 15:44
|
||||
* @Description: Minio配置管理
|
||||
* @Version: 1.0
|
||||
*/
|
||||
@Configuration
|
||||
public class MinioConfig {
|
||||
/**
|
||||
* minioUrl
|
||||
*/
|
||||
@Value("${minio.url}")
|
||||
private String url;
|
||||
/**
|
||||
* minio账户
|
||||
*/
|
||||
@Value("${minio.accessKey}")
|
||||
private String accessKey;
|
||||
/**
|
||||
* minio密码
|
||||
*/
|
||||
@Value("${minio.secretKey}")
|
||||
private String secretKey;
|
||||
|
||||
/**
|
||||
* 构造minioClient
|
||||
*/
|
||||
@Bean
|
||||
public MinioClient getMinioClient(){
|
||||
return MinioClient.builder().endpoint(url).credentials(accessKey,secretKey ).build();
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,40 @@
|
||||
package com.landaiqing.oss.config;
|
||||
|
||||
import com.landaiqing.oss.adapter.StorageAdapter;
|
||||
import com.landaiqing.oss.adapter.AliStorageAdapter;
|
||||
import com.landaiqing.oss.adapter.MinioStorageAdapter;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.cloud.context.config.annotation.RefreshScope;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
/**
|
||||
* @Classname StorageConfig
|
||||
* @BelongsProject: qing-yu-club
|
||||
* @BelongsPackage: com.landaiqing.oss.config
|
||||
* @Author: landaiqing
|
||||
* @CreateTime: 2024-02-17 16:52
|
||||
* @Description: TODO
|
||||
* @Version: 1.0
|
||||
*/
|
||||
@Configuration
|
||||
@RefreshScope
|
||||
public class StorageConfig {
|
||||
|
||||
@Value("${storage.service.type}")
|
||||
private String storageType;
|
||||
|
||||
|
||||
@Bean
|
||||
@RefreshScope
|
||||
public StorageAdapter storageService() {
|
||||
if ("minio".equals(storageType)) {
|
||||
return new MinioStorageAdapter();
|
||||
} else if ("aliyun".equals(storageType)) {
|
||||
return new AliStorageAdapter();
|
||||
} else {
|
||||
throw new IllegalArgumentException("未找到对应的文件处理");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,45 @@
|
||||
package com.landaiqing.oss.controller;
|
||||
|
||||
import com.landaiqing.oss.entity.Result;
|
||||
import com.landaiqing.oss.service.FileService;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @Classname FileController
|
||||
* @BelongsProject: qing-yu-club
|
||||
* @BelongsPackage: com.landaiqing.oss.controller
|
||||
* @Author: landaiqing
|
||||
* @CreateTime: 2024-02-17 16:18
|
||||
* @Description: TODO
|
||||
* @Version: 1.0
|
||||
*/
|
||||
@RestController
|
||||
public class FileController {
|
||||
@Resource
|
||||
private FileService fileService;
|
||||
|
||||
@RequestMapping("/testGetAllBuckets")
|
||||
public String testGetAllBuckets() throws Exception {
|
||||
List<String> allBucket = fileService.getAllBucket();
|
||||
return allBucket.get(0);
|
||||
}
|
||||
|
||||
@RequestMapping("/getUrl")
|
||||
public String getUrl(String bucketName, String objectName) throws Exception {
|
||||
return fileService.getUrl(bucketName, objectName);
|
||||
}
|
||||
|
||||
/**
|
||||
* 上传文件
|
||||
*/
|
||||
@RequestMapping("/upload")
|
||||
public Result upload(MultipartFile uploadFile, String bucket, String objectName) throws Exception {
|
||||
String url = fileService.uploadFile(uploadFile, bucket, objectName);
|
||||
return Result.ok(url);
|
||||
}
|
||||
}
|
@@ -0,0 +1,42 @@
|
||||
package com.landaiqing.oss.entity;
|
||||
|
||||
/**
|
||||
* @Classname FileInfo
|
||||
* @BelongsProject: qing-yu-club
|
||||
* @BelongsPackage: com.landaiqing.oss.entity
|
||||
* @Author: landaiqing
|
||||
* @CreateTime: 2024-02-17 16:04
|
||||
* @Description: 文件类
|
||||
* @Version: 1.0
|
||||
*/
|
||||
public class FileInfo {
|
||||
|
||||
|
||||
private String fileName;
|
||||
private Boolean directoryFlag;
|
||||
private String etag;
|
||||
|
||||
public String getFileName() {
|
||||
return fileName;
|
||||
}
|
||||
|
||||
public void setFileName(String fileName) {
|
||||
this.fileName = fileName;
|
||||
}
|
||||
|
||||
public Boolean getDirectoryFlag() {
|
||||
return directoryFlag;
|
||||
}
|
||||
|
||||
public void setDirectoryFlag(Boolean directoryFlag) {
|
||||
this.directoryFlag = directoryFlag;
|
||||
}
|
||||
|
||||
public String getEtag() {
|
||||
return etag;
|
||||
}
|
||||
|
||||
public void setEtag(String etag) {
|
||||
this.etag = etag;
|
||||
}
|
||||
}
|
@@ -0,0 +1,50 @@
|
||||
package com.landaiqing.oss.entity;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class Result<T> {
|
||||
|
||||
private Boolean success;
|
||||
|
||||
private Integer code;
|
||||
|
||||
private String message;
|
||||
|
||||
private T data;
|
||||
|
||||
public static Result ok(){
|
||||
Result result = new Result();
|
||||
result.setSuccess(true);
|
||||
result.setCode(ResultCodeEnum.SUCCESS.getCode());
|
||||
result.setMessage(ResultCodeEnum.SUCCESS.getDesc());
|
||||
return result;
|
||||
}
|
||||
|
||||
public static <T> Result ok(T data){
|
||||
Result result = new Result();
|
||||
result.setSuccess(true);
|
||||
result.setCode(ResultCodeEnum.SUCCESS.getCode());
|
||||
result.setMessage(ResultCodeEnum.SUCCESS.getDesc());
|
||||
result.setData(data);
|
||||
return result;
|
||||
}
|
||||
|
||||
public static Result fail(){
|
||||
Result result = new Result();
|
||||
result.setSuccess(false);
|
||||
result.setCode(ResultCodeEnum.FAIL.getCode());
|
||||
result.setMessage(ResultCodeEnum.FAIL.getDesc());
|
||||
return result;
|
||||
}
|
||||
|
||||
public static <T> Result fail(T data){
|
||||
Result result = new Result();
|
||||
result.setSuccess(false);
|
||||
result.setCode(ResultCodeEnum.FAIL.getCode());
|
||||
result.setMessage(ResultCodeEnum.FAIL.getDesc());
|
||||
result.setData(data);
|
||||
return result;
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,29 @@
|
||||
package com.landaiqing.oss.entity;
|
||||
|
||||
import lombok.Getter;
|
||||
|
||||
@Getter
|
||||
public enum ResultCodeEnum {
|
||||
|
||||
SUCCESS(200,"成功"),
|
||||
FAIL(500,"失败");
|
||||
|
||||
public int code;
|
||||
|
||||
public String desc;
|
||||
|
||||
ResultCodeEnum(int code,String desc){
|
||||
this.code = code;
|
||||
this.desc = desc;
|
||||
}
|
||||
|
||||
public static ResultCodeEnum getByCode(int codeVal){
|
||||
for(ResultCodeEnum resultCodeEnum : ResultCodeEnum.values()){
|
||||
if(resultCodeEnum.code == codeVal){
|
||||
return resultCodeEnum;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,49 @@
|
||||
package com.landaiqing.oss.service;
|
||||
|
||||
import com.landaiqing.oss.adapter.StorageAdapter;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @Classname FileService
|
||||
* @BelongsProject: qing-yu-club
|
||||
* @BelongsPackage: com.landaiqing.oss.service
|
||||
* @Author: landaiqing
|
||||
* @CreateTime: 2024-02-17 16:50
|
||||
* @Description: TODO
|
||||
* @Version: 1.0
|
||||
*/
|
||||
@Service
|
||||
public class FileService {
|
||||
private final StorageAdapter storageAdapter;
|
||||
|
||||
public FileService(StorageAdapter storageAdapter) {
|
||||
this.storageAdapter = storageAdapter;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 列出所有桶
|
||||
*/
|
||||
public List<String> getAllBucket() {
|
||||
return storageAdapter.getAllBucket();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取文件路径
|
||||
*/
|
||||
public String getUrl(String bucketName,String objectName) {
|
||||
return storageAdapter.getUrl(bucketName,objectName);
|
||||
}
|
||||
|
||||
/**
|
||||
* 上传文件
|
||||
*/
|
||||
public String uploadFile(MultipartFile uploadFile, String bucket, String objectName){
|
||||
storageAdapter.uploadFile(uploadFile,bucket,objectName);
|
||||
objectName = objectName + "/" + uploadFile.getOriginalFilename();
|
||||
return storageAdapter.getUrl(bucket, objectName);
|
||||
}
|
||||
}
|
@@ -0,0 +1,126 @@
|
||||
package com.landaiqing.oss.util;
|
||||
|
||||
import com.landaiqing.oss.entity.FileInfo;
|
||||
import io.minio.*;
|
||||
import io.minio.errors.*;
|
||||
import io.minio.messages.Bucket;
|
||||
import io.minio.messages.Item;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.security.InvalidKeyException;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* @Classname MinioUtil
|
||||
* @BelongsProject: qing-yu-club
|
||||
* @BelongsPackage: com.landaiqing.oss.util
|
||||
* @Author: landaiqing
|
||||
* @CreateTime: 2024-02-17 15:50
|
||||
* @Description: minio文件操作工具
|
||||
* @Version: 1.0
|
||||
*/
|
||||
@Component
|
||||
public class MinioUtil {
|
||||
@Resource
|
||||
private MinioClient minioClient;
|
||||
|
||||
/**
|
||||
* @description: 创建Bucket桶
|
||||
* @param: [bucket]
|
||||
* @return: void
|
||||
* @author landaiqing
|
||||
* @date: 2024/2/17 15:53
|
||||
*/
|
||||
public void createBucket(String bucket) throws ServerException, InsufficientDataException, ErrorResponseException, IOException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException {
|
||||
boolean exists = minioClient.bucketExists(BucketExistsArgs.builder().bucket(bucket).build());
|
||||
if (!exists) {
|
||||
minioClient.makeBucket(MakeBucketArgs.builder().bucket(bucket).build());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @description: 上传文件
|
||||
* @param: [inputStream, bucket, objectName]
|
||||
* @return: void
|
||||
* @author landaiqing
|
||||
* @date: 2024/2/17 15:58
|
||||
*/
|
||||
public void uploadFile(InputStream inputStream, String bucket, String objectName) throws ServerException, InsufficientDataException, ErrorResponseException, IOException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException {
|
||||
minioClient.putObject(PutObjectArgs.builder().bucket(bucket).object(objectName)
|
||||
.stream(inputStream, -1, Integer.MAX_VALUE).build());
|
||||
}
|
||||
|
||||
/**
|
||||
* @description: 列出所有桶
|
||||
* @param: []
|
||||
* @return: java.util.List<java.lang.String>
|
||||
* @author landaiqing
|
||||
* @date: 2024/2/17 16:00
|
||||
*/
|
||||
public List<String> getAllBucket() throws ServerException, InsufficientDataException, ErrorResponseException, IOException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException {
|
||||
List<Bucket> buckets = minioClient.listBuckets();
|
||||
return buckets.stream().map(Bucket::name).collect(Collectors.toList());
|
||||
}
|
||||
|
||||
/**
|
||||
* @description: 列出当前桶及文件
|
||||
* @param: [bucket]
|
||||
* @return: java.util.List<com.landaiqing.oss.entity.FileInfo>
|
||||
* @author landaiqing
|
||||
* @date: 2024/2/17 16:06
|
||||
*/
|
||||
public List<FileInfo> getAllFile(String bucket) throws ServerException, InsufficientDataException, ErrorResponseException, IOException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException {
|
||||
Iterable<Result<Item>> results = minioClient.listObjects(
|
||||
ListObjectsArgs.builder().bucket(bucket).build());
|
||||
List<FileInfo> fileInfoList = new LinkedList<>();
|
||||
for (Result<Item> result : results) {
|
||||
FileInfo fileInfo = new FileInfo();
|
||||
Item item = result.get();
|
||||
fileInfo.setFileName(item.objectName());
|
||||
fileInfo.setDirectoryFlag(item.isDir());
|
||||
fileInfo.setEtag(item.etag());
|
||||
fileInfoList.add(fileInfo);
|
||||
}
|
||||
return fileInfoList;
|
||||
}
|
||||
|
||||
/**
|
||||
* @description: 下载文件
|
||||
* @param: [bucket, objectName]
|
||||
* @return: java.io.InputStream
|
||||
* @author landaiqing
|
||||
* @date: 2024/2/17 16:11
|
||||
*/
|
||||
public InputStream download(String bucket, String objectName) throws ServerException, InsufficientDataException, ErrorResponseException, IOException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException {
|
||||
return minioClient.getObject(GetObjectArgs.builder().bucket(bucket).object(objectName).build());
|
||||
}
|
||||
|
||||
/**
|
||||
* @description: 删除桶
|
||||
* @param: [bucket]
|
||||
* @return: void
|
||||
* @author landaiqing
|
||||
* @date: 2024/2/17 16:14
|
||||
*/
|
||||
public void deleteBucket(String bucket) throws ServerException, InsufficientDataException, ErrorResponseException, IOException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException {
|
||||
minioClient.removeBucket(RemoveBucketArgs.builder().bucket(bucket).build());
|
||||
}
|
||||
|
||||
/**
|
||||
* @description: 删除文件
|
||||
* @param: [bucket, objectName]
|
||||
* @return: void
|
||||
* @author landaiqing
|
||||
* @date: 2024/2/17 16:14
|
||||
*/
|
||||
public void deleteObject(String bucket, String objectName) throws ServerException, InsufficientDataException, ErrorResponseException, IOException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException {
|
||||
minioClient.removeObject(
|
||||
RemoveObjectArgs.builder().bucket(bucket).object(objectName).build());
|
||||
}
|
||||
}
|
6
qing-yu-club-oss/src/main/resources/application.yml
Normal file
6
qing-yu-club-oss/src/main/resources/application.yml
Normal file
@@ -0,0 +1,6 @@
|
||||
server:
|
||||
port: 4000
|
||||
minio:
|
||||
url: http://116.196.80.239:9000/
|
||||
accessKey: landaiqing
|
||||
secretKey: LDQ20020618xxx
|
17
qing-yu-club-oss/src/main/resources/bootstrap.yml
Normal file
17
qing-yu-club-oss/src/main/resources/bootstrap.yml
Normal file
@@ -0,0 +1,17 @@
|
||||
spring:
|
||||
application:
|
||||
name: qing-yu-club-oss
|
||||
profiles:
|
||||
active: dev
|
||||
cloud:
|
||||
nacos:
|
||||
config:
|
||||
server-addr: 116.196.80.239:8848
|
||||
prefix: ${spring.application.name}
|
||||
group: DEFAULT_GROUP
|
||||
namespace:
|
||||
file-extension: yaml
|
||||
discovery:
|
||||
enabled: true
|
||||
server-addr: 116.196.80.239:8848
|
||||
|
Reference in New Issue
Block a user