赞
踩
MinIO 是高性能的对象存储,是为海量数据存储、人工智能、大数据分析而设计的,它完全兼容Amazon S3接口,单个对象最大可达5TB,适合存储海量图片、视频、日志文件、备份数据和容器/虚拟机镜像等。MinIO主要采用Golang语言实现,,客户端与存储服务器之间采用http/https通信协议。
官网: https://min.io/
中文文档:http://docs.minio.org.cn/docs/
下载地址:https://min.io/download
github: https://github.com/minio/minio
cd /opt
mkdir minio
mkdir data
touch minio.log
wget https://dl.minio.io/server/minio/release/linux-amd64/minio
chmod 777 minio
vim /etc/profile
# set minio environment
export MINIO_ROOT_USER=fileadmin
export MINIO_ROOT_PASSWORD=fileadmin
nohup /opt/minio/minio server /opt/minio/data --console-address ":35555" > /opt/minio/minio.log 2>&1 &
nohup 和 末尾& :后台启动
/opt/minio/data:文件存储路径
–console-address:控制台固定端口
2>&1:在标准输出直接输入到/opt/minio/minio.log 中
http://172.169.3.251:62222/buckets 帐号密码:fileadmin fileadmin
<dependency>
<groupId>io.minio</groupId>
<artifactId>minio</artifactId>
<version>8.2.1</version>
</dependency>
spring:
servlet:
multipart:
max-file-size: 10MB
max-request-size: 10MB
#minio配置
minio:
access-key: 控制台创建的用户名(test)
secret-key: 密码 (12345678)
url: http://minio服务器IP:9000 (http://127.0.0.1:9000)
bucket-name: 控制台创建的bucket (test)
import io.minio.MinioClient; import lombok.Data; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration @ConfigurationProperties(prefix = "spring.minio") @Data public class MinioConfiguration { private String accessKey; private String secretKey; private String url; private String bucketName; @Bean public MinioClient minioClient() { return MinioClient.builder() .endpoint(url) .credentials(accessKey, secretKey) .build(); } }
import com.wdhcr.minioupload.config.MinioConfiguration; import io.minio.*; import io.minio.http.Method; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import org.springframework.web.multipart.MultipartFile; import java.io.InputStream; import java.time.ZonedDateTime; import java.util.HashMap; import java.util.Map; import java.util.concurrent.TimeUnit; @Component public class MinioComp { @Autowired private MinioClient minioClient; @Autowired private MinioConfiguration configuration; /** * @description: 获取上传临时签名 */ public Map getPolicy(String fileName, ZonedDateTime time) { PostPolicy postPolicy = new PostPolicy(configuration.getBucketName(), time); postPolicy.addEqualsCondition("key", fileName); try { Map<String, String> map = minioClient.getPresignedPostFormData(postPolicy); HashMap<String, String> map1 = new HashMap<>(); map.forEach((k, v) -> { map1.put(k.replaceAll("-", ""), v); }); map1.put("host", configuration.getUrl() + "/" + configuration.getBucketName()); return map1; } catch (Exception e) { e.printStackTrace(); } return null; } /** * @description: 获取上传文件的url */ public String getPolicyUrl(String objectName, Method method, int time, TimeUnit timeUnit) { try { return minioClient.getPresignedObjectUrl(GetPresignedObjectUrlArgs.builder() .method(method) .bucket(configuration.getBucketName()) .object(objectName) .expiry(time, timeUnit).build()); } catch (Exception e) { e.printStackTrace(); } return null; } /** * @description: 上传文件 */ public void upload(MultipartFile file, String fileName) { // 使用putObject上传一个文件到存储桶中。 try { InputStream inputStream = file.getInputStream(); minioClient.putObject( PutObjectArgs.builder() .bucket(configuration.getBucketName()) .object(fileName) .stream(inputStream, file.getSize(), -1) .contentType(file.getContentType()) .build() ); } catch (Exception e) { e.printStackTrace(); } } /** * 删除文件 */ public void remove(String objectName) { try { minioClient.removeObject( RemoveObjectArgs.builder() .bucket(configuration.getBucketName()) .object(objectName) .build() ); } catch (Exception e) { e.printStackTrace(); } } /** * 获取文件访问地址 */ public String getUrl(String objectName, int time, TimeUnit timeUnit) { String url = null; try { url = minioClient.getPresignedObjectUrl( GetPresignedObjectUrlArgs.builder() .method(Method.GET) .bucket(configuration.getBucketName()) .object(objectName) .expiry(time, timeUnit).build() ); } catch (Exception e) { e.printStackTrace(); } return url; } }
import com.wdhcr.minioupload.component.MinioComp; import com.wdhcr.minioupload.domain.R; import io.minio.http.Method; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.multipart.MultipartFile; import java.time.ZonedDateTime; import java.util.Map; import java.util.concurrent.TimeUnit; @RestController public class UploadController { @Autowired private MinioComp minioComp; @PostMapping("/upload") public R upload(@RequestParam("file") MultipartFile file, @RequestParam("fileName") String fileName) { minioComp.upload(file, fileName); String url = minioComp.getUrl(fileName, 7, TimeUnit.DAYS); return R.success(url); } @GetMapping("/policy") public R policy(@RequestParam("fileName") String fileName) { Map policy = minioComp.getPolicy(fileName, ZonedDateTime.now().plusMinutes(10)); return R.success(policy); } @GetMapping("/uploadUrl") public R uploadUrl(@RequestParam("fileName") String fileName) { String url = minioComp.getPolicyUrl(fileName, Method.PUT, 2, TimeUnit.MINUTES); return R.success(url); } @GetMapping("/url") public R getUrl(@RequestParam("fileName") String fileName) { String url = minioComp.getUrl(fileName, 7, TimeUnit.DAYS); return R.success(url); } }
import com.wdhcr.minioupload.component.MinioComp; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.mock.web.MockMultipartFile; import org.springframework.web.multipart.MultipartFile; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.util.UUID; import java.util.concurrent.TimeUnit; @SpringBootTest class MinioUploadApplicationTests { @Autowired private MinioComp minioComp; @Test void upload() throws IOException { File pdfFile = new File("D://application.properties"); FileInputStream fileInputStream = new FileInputStream(pdfFile); MultipartFile multipartFile = new MockMultipartFile(pdfFile.getName(), pdfFile.getName(), "application/octet-stream", fileInputStream); String filename = UUID.randomUUID().toString() + "-" + multipartFile.getOriginalFilename(); minioComp.upload(multipartFile, filename); String url = minioComp.getUrl(filename, 7, TimeUnit.DAYS); System.err.println(url); } @Test public void remove() { minioComp.remove("application.properties"); } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。