All Downloads are FREE. Search and download functionalities are using the official Maven repository.

cn.structure.starter.minio.service.MinioTemplate Maven / Gradle / Ivy

The newest version!
/*
 *    Copyright (c) 2018-2025, freelance All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the freelance.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author:
 */

package cn.structure.starter.minio.service;

import cn.structure.starter.minio.vo.MinioItem;
import io.minio.*;
import io.minio.messages.Bucket;
import io.minio.messages.Item;
import lombok.AllArgsConstructor;
import lombok.NoArgsConstructor;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

/**
 * 

* minio 交互类 *

* * @author chuck * @version 1.0.1 * @since 2021/7/17 14:06 */ @AllArgsConstructor @NoArgsConstructor public class MinioTemplate { @Autowired private MinioClient minioClient; /** * 创建bucket * * @param bucketName bucket名称 */ @SneakyThrows public void createBucket(String bucketName) { if (!minioClient.bucketExists(BucketExistsArgs.builder().bucket(bucketName).build())) { minioClient.makeBucket(MakeBucketArgs.builder().bucket(bucketName).build()); } } /** * 获取全部bucket *

* https://docs.minio.io/cn/java-client-api-reference.html#listBuckets *

*/ @SneakyThrows public List getAllBuckets() { return minioClient.listBuckets(); } /** * @param bucketName bucket名称 */ @SneakyThrows public Optional getBucket(String bucketName) { return minioClient.listBuckets().stream().filter(b -> b.name().equals(bucketName)).findFirst(); } /** * @param bucketName bucket名称 */ @SneakyThrows public void removeBucket(String bucketName) { minioClient.removeBucket(RemoveBucketArgs.builder().bucket(bucketName).build()); } /** * 根据文件前置查询文件 * * @param bucketName bucket名称 * @param prefix 前缀 * @param recursive 是否递归查询 * @return MinioItem 列表 */ @SneakyThrows public List getAllObjectsByPrefix(String bucketName, String prefix, boolean recursive) { List objectList = new ArrayList<>(); Iterable> objectsIterator = minioClient.listObjects(ListObjectsArgs.builder().bucket(bucketName).prefix(prefix).recursive(recursive).build()); while (objectsIterator.iterator().hasNext()) { objectList.add(new MinioItem(objectsIterator.iterator().next().get())); } return objectList; } /** * 获取文件外链 * * @param bucketName bucket名称 * @param objectName 文件名称 * @param expires 失效时间(以秒为单位),默认是7天,不得大于七天。 * @return url */ @SneakyThrows public String getObjectURL(String bucketName, String objectName, Integer expires) { return minioClient.getPresignedObjectUrl(GetPresignedObjectUrlArgs .builder() .bucket(bucketName) .object(objectName) .expiry(expires) .build()); } /** * 获取文件 * * @param bucketName bucket名称 * @param objectName 文件名称 * @return 二进制流 */ @SneakyThrows public InputStream getObject(String bucketName, String objectName) { return minioClient.getObject(GetObjectArgs.builder() .bucket(bucketName) .object(objectName) .build()); } /** * 上传文件 * * @param bucketName bucket名称 * @param objectName 文件名称 * @param stream 文件流 * @throws Exception https://docs.minio.io/cn/java-client-api-reference.html#putObject */ public void putObject(String bucketName, String objectName, InputStream stream) throws Exception { minioClient.putObject(PutObjectArgs.builder() .object(objectName) .bucket(bucketName) .stream(stream, stream.available(), -1) .build()); } /** * 上传文件 * * @param bucketName bucket名称 * @param objectName 文件名称 * @param stream 文件流 * @param size 大小 * @param contextType 类型 * @throws Exception https://docs.minio.io/cn/java-client-api-reference.html#putObject */ public void putObject(String bucketName, String objectName, InputStream stream, long size, String contextType) throws Exception { minioClient.putObject(PutObjectArgs.builder() .object(objectName) .bucket(bucketName) .stream(stream, stream.available(), -1) .build()); } /** * 获取文件信息 * * @param bucketName bucket名称 * @param objectName 文件名称 * @throws Exception https://docs.minio.io/cn/java-client-api-reference.html#statObject */ public StatObjectResponse getObjectInfo(String bucketName, String objectName) throws Exception { return minioClient.statObject(StatObjectArgs.builder().bucket(bucketName).object(objectName).build()); } /** * Your method description *

* * @param bucketName bucket名称 * @param objectName 文件名称 * @throws Exception https://docs.minio.io/cn/java-client-api-reference.html#removeObject * @since createTime 2021/7/17 14:06 */ public void removeObject(String bucketName, String objectName) throws Exception { minioClient.removeObject(RemoveObjectArgs.builder().bucket(bucketName).object(objectName).build()); } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy