sách gpt4 ai đã đi

springboot:整合minio

In lại 作者:知者 更新时间:2024-03-13 01:33:01 26 4
mua khóa gpt4 Nike

springboot:整合minio

一、安装minio

docker安装minio

minIo文档

安装上面这篇博客进入minio控制台,创建bucket

二、集成

phụ thuộc

  io.minio minio 8.0.3 

yaml配置

spring: # 配置文件上传大小限制 servlet: multipart: max-file-size: 200MB max-request-size: 200MB minio: endpoint: http://127.0.0.1:9000 accessKey: admin secretKey: admin bucketName: test

配置类

package com.yolo.springbootminio.config; import io.minio.MinioClient; import lombok.Data; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.stereotype.Component; @Data @Component public class MinIoClientConfig { @Value("${minio.endpoint}") private String endpoint; @Value("${minio.accessKey}") private String accessKey; @Value("${minio.secretKey}") private String secretKey; /** * 注入minio 客户端 * @return */ @Bean public MinioClient minioClient(){ return MinioClient.builder() .endpoint(endpoint) .credentials(accessKey, secretKey) .build(); } }

工具类

package com.yolo.springbootminio.util; import io.minio.*; import io.minio.messages.DeleteError; import io.minio.messages.DeleteObject; import io.minio.messages.Item; import lombok.Data; import org.apache.tomcat.util.http.fileupload.IOUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpStatus; import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Component; import org.springframework.web.multipart.MultipartFile; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.UnsupportedEncodingException; import java.net.URLEncoder; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.stream.Collectors; /** * @description: minio工具类 * @version:3.0 */ @Component public class MinIoUtil { @Autowired private MinioClient minioClient; @Value("${minio.bucketName}") private String bucketName; /** * description: 判断bucket是否存在,不存在则创建 * * @return: void */ public void existBucket(String name) { try { boolean exists = minioClient.bucketExists(BucketExistsArgs.builder().bucket(name).build()); if (!exists) { minioClient.makeBucket(MakeBucketArgs.builder().bucket(name).build()); } } catch (Exception e) { e.printStackTrace(); } } /** * 创建存储bucket * @param bucketName 存储bucket名称 * @return Boolean */ public Boolean makeBucket(String bucketName) { try { minioClient.makeBucket(MakeBucketArgs.builder() .bucket(bucketName) .build()); } catch (Exception e) { e.printStackTrace(); return false; } return true; } /** * 删除存储bucket * @param bucketName 存储bucket名称 * @return Boolean */ public Boolean removeBucket(String bucketName) { try { minioClient.removeBucket(RemoveBucketArgs.builder() .bucket(bucketName) .build()); } catch (Exception e) { e.printStackTrace(); return false; } return true; } /** * description: 上传文件 * * @param multipartFile * @return: java.lang.String */ public List upload(MultipartFile[] multipartFile) { List names = new ArrayList<>(multipartFile.length); for (MultipartFile file : multipartFile) { String fileName = file.getOriginalFilename(); String[] split = fileName.split("\\."); if (split.length > 1) { fileName = split[0] + "_" + System.currentTimeMillis() + "." + split[1]; } else { fileName = fileName + System.currentTimeMillis(); } InputStream in = null; try { in = file.getInputStream(); minioClient.putObject(PutObjectArgs.builder() .bucket(bucketName) .object(fileName) .stream(in, in.available(), -1) .contentType(file.getContentType()) .build() ); } catch (Exception e) { e.printStackTrace(); } finally { if (in != null) { try { in.close(); } catch (IOException e) { e.printStackTrace(); } } } names.add(fileName); } return names; } /** * description: 下载文件 * * @param fileName * @return: org.springframework.http.ResponseEntity */ public ResponseEntity download(String fileName) { ResponseEntity responseEntity = null; InputStream in = null; ByteArrayOutputStream out = null; try { in = minioClient.getObject(GetObjectArgs.builder().bucket(bucketName).object(fileName).build()); out = new ByteArrayOutputStream(); IOUtils.copy(in, out); //封装返回值 byte[] bytes = out.toByteArray(); HttpHeaders headers = new HttpHeaders(); try { headers.add("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8")); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } headers.setContentLength(bytes.length); headers.setContentType(MediaType.APPLICATION_OCTET_STREAM); headers.setAccessControlExposeHeaders(Arrays.asList("*")); responseEntity = new ResponseEntity(bytes, headers, HttpStatus.OK); } catch (Exception e) { e.printStackTrace(); } finally { try { if (in != null) { try { in.close(); } catch (IOException e) { e.printStackTrace(); } } if (out != null) { out.close(); } } catch (IOException e) { e.printStackTrace(); } } return responseEntity; } /** * 查看文件对象 * @param bucketName 存储bucket名称 * @return 存储bucket内文件对象信息 */ public List listObjects(String bucketName) { Iterable<>> results = minioClient.listObjects( ListObjectsArgs.builder().bucket(bucketName).build()); List objectItems = new ArrayList<>(); try { for (Result result : results) { Item item = result.get(); ObjectItem objectItem = new ObjectItem(); objectItem.setObjectName(item.objectName()); objectItem.setSize(item.size()); objectItems.add(objectItem); } } catch (Exception e) { e.printStackTrace(); return null; } return objectItems; } /** * 批量删除文件对象 * @param bucketName 存储bucket名称 * @param objects 对象名称集合 */ public Iterable<>> removeObjects(String bucketName, List objects) { List dos = objects.stream().map(DeleteObject::new).collect(Collectors.toList()); return minioClient.removeObjects(RemoveObjectsArgs.builder().bucket(bucketName).objects(dos).build()); } @Data public static class ObjectItem { private String objectName; private Long size; } }

控制器

@RestController @Slf4j public class MinioController { @Autowired private MinIoUtil minioUtil; @Value("${minio.endpoint}") private String address; @Value("${minio.bucketName}") private String bucketName; @PostMapping("/upload") public Object upload(MultipartFile file) { List upload = minioUtil.upload(new MultipartFile[]{file}); return address+"/"+bucketName+"/"+upload.get(0); } }

然后把这个地址输入到浏览器,这里看不到图片,是因为没有设置buckets的策略

26 4 0
Bài viết được đề xuất: 源码解析Synchronous Queue 这种特立独行的队列
Bài viết được đề xuất: Typescript的优缺点?
Bài viết được đề xuất: 一键清理Maven失效依赖-脚本+软件
Bài viết được đề xuất: element如何修改原样式及无法修改原样式等问题
Giấy chứng nhận ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com