专门做优选的网站论坛推广的特点
文章目录
- 前置工作
- Minio依赖
- MinioClient 配置类
- yml配置
- Client配置类
- Minio 的基本操作
- OSSFileService
本篇文章只讲解MinIo的基本使用,下面有一些案例代码拿来即用,不过要注意桶和对象这两个概念,对桶和对象这两个概念下面有讲解。
前置工作
需要在某个服务器安装一个Minio的Server服务。
Minio依赖
<dependency><groupId>io.minio</groupId><artifactId>minio</artifactId><version>8.3.0</version>
</dependency>
MinioClient 配置类
yml配置
# minio
minio:# 服务地址endpoint: http://localhost:9000# 账号accessKey: minioadmin# 密码secretKey: minioadmin
Client配置类
这里把MinioClient注入到Bean中,后续使用MinioClient调用Minio的API。
import io.minio.MinioClient;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;@Configuration
@Slf4j
public class MinIoClientConfig {@Value("${minio.endpoint}")private String endpoint;@Value("${minio.accessKey}")private String accessKey;@Value("${minio.secretKey}")private String secretKey;/*** 注入minio 客户端* @return*/@Beanpublic MinioClient minioClient(){return MinioClient.builder().endpoint(endpoint).credentials(accessKey, secretKey).build();}}
Minio 的基本操作
在这里定义一个OSSFileService接口,用来抽象出Minio的具体实现。
在这里要引入一个 桶(bucket)
的概念,这个桶是Minio用来划分不同数据的,你可以理解成文件夹,不过桶在这里有公开和私密的属性,公开的桶可以随便浏览,而私密的则需要给定一个浏览的时间,超过这个浏览的时间就不能再浏览了。
objectName
对象名称:对象名称可以理解成哪个文件夹下,哪个文件,对象名称例如: public/images/111.jpg
OSSFileService
public interface FileService {/*** 上传文件** @param bucketName 存储桶名称* @param input 文件流* @param objectName 对象名称* @param contentType contentType* @return*/void uploadFile(String bucketName, InputStream input, String objectName, String contentType);/*** 根据对象名称获取公共文件访问路径** @param bucketName 存储桶名称* @param objectName 对象名称*/String getUrlByBucketNameAndObjectName(String bucketName, String objectName);/*** 根据对象名称获取文件访问路径** @param bucketName 存储桶名称* @param objectName 对象名称* @param getUrlTime 访问文件路径时间* @param timeUnit 访问时间格式* @return*/String getUrlByBucketNameAndObjectNameAndTime(String bucketName, String objectName, int getUrlTime, TimeUnit timeUnit);/*** 根据对象名称和桶名称删除对象** @param bucketName 桶名称* @param objectName 对象名称* @return*/void remove(String bucketName, String objectName);}
@Service
public class OssFileServiceImpl implements OssFileService {@Autowiredprivate MinioClient minioClient;@Overridepublic void uploadFile(String bucketName, InputStream input, String objectName, String contentType) {try {minioClient.putObject(PutObjectArgs.builder().bucket(bucketName).object(objectName).contentType(contentType).stream(input, input.available(), -1).build());} catch (Exception e) {e.printStackTrace();} finally {try {input.close();} catch (IOException e) {e.printStackTrace();}}}@Overridepublic String getUrlByBucketNameAndObjectName(String bucketName, String objectName) {return this.getUrl(bucketName, objectName, 1, TimeUnit.DAYS);}@Overridepublic String getUrlByBucketNameAndObjectNameAndTime(String bucketName, String objectName, int getUrlTime, TimeUnit timeUnit) {return this.getUrl(bucketName, objectName, getUrlTime, timeUnit);}@Overridepublic void remove(String bucketName, String objectName) {try {minioClient.removeObject(RemoveObjectArgs.builder().bucket(bucketName).object(objectName).build());} catch (Exception e) {e.printStackTrace();}}/*** 根据 桶、object、时间 获取文件路径** @param bucketName 桶名称* @param objectName object* @param getUrlTime 时间* @param timeUnit 时间格式* @return*/private String getUrl(String bucketName, String objectName, int getUrlTime, TimeUnit timeUnit) {try {return minioClient.getPresignedObjectUrl(GetPresignedObjectUrlArgs.builder().bucket(bucketName).object(objectName).expiry(getUrlTime, timeUnit).method(Method.GET).build());} catch (Exception e) {e.printStackTrace();}return null;}}