Java中的MinIO应用类--版本2


1. 配置类

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import io.minio.MinioClient;


@Configuration
@ConfigurationProperties(prefix = "minio")
public class MinioConfig {
  /**
   * 服务地址
   */
  private String endpoint;

  /**
   * 用户名
   */
  private String accessKey;

  /**
   * 密码
   */
  private String secretKey;

  /**
   * 存储桶名称
   */
  private String bucketName;

  public String getEndpoint() {
    return endpoint;
  }

  public void setEndpoint(String endpoint) {
    this.endpoint = endpoint;
  }

  public String getAccessKey() {
    return accessKey;
  }

  public void setAccessKey(String accessKey) {
    this.accessKey = accessKey;
  }

  public String getSecretKey() {
    return secretKey;
  }

  public void setSecretKey(String secretKey) {
    this.secretKey = secretKey;
  }

  public String getBucketName() {
    return bucketName;
  }

  public void setBucketName(String bucketName) {
    this.bucketName = bucketName;
  }

  @Bean
  public MinioClient getMinioClient() {
    return MinioClient.builder().endpoint(endpoint).credentials(accessKey, secretKey).build();
  }

}



2.应用类

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import io.minio.BucketExistsArgs;
import io.minio.DownloadObjectArgs;
import io.minio.GetPresignedObjectUrlArgs;
import io.minio.ListObjectsArgs;
import io.minio.MakeBucketArgs;
import io.minio.MinioClient;
import io.minio.PutObjectArgs;
import io.minio.RemoveBucketArgs;
import io.minio.RemoveObjectArgs;
import io.minio.Result;
import io.minio.StatObjectArgs;
import io.minio.StatObjectResponse;
import io.minio.http.Method;
import io.minio.messages.Item;

@Component
public class MinioTemplate {

  @Autowired
  private MinioClient minioClient;

  public MinioTemplate() {}

  // bucket是否存在
  public Boolean bucketExists(String bucketName) {
    Boolean found;
    try {
      found = minioClient.bucketExists(BucketExistsArgs.builder().bucket(bucketName).build());
    } catch (Exception e) {
      e.printStackTrace();
      return false;
    }
    return found;
  }

  // 创建存储bucket
  public Boolean createBucket(String bucketName) throws Exception {
    try {
      minioClient.makeBucket(MakeBucketArgs.builder().bucket(bucketName).build());
    } catch (Exception e) {
      e.printStackTrace();
      return false;
    }
    return true;
  }

  // 删除存储bucket
  public Boolean removeBucket(String bucketName) {
    try {
      minioClient.removeBucket(RemoveBucketArgs.builder().bucket(bucketName).build());
    } catch (Exception e) {
      e.printStackTrace();
      return false;
    }
    return true;
  }

  // 获取全部bucket
  public List<String> getAllBuckets() {
    try {
      List<String> buckets = minioClient.listBuckets().stream().map(item -> item.name()).collect(Collectors.toList());
      return buckets;
    } catch (Exception e) {
      e.printStackTrace();
    }
    return null;
  }

  // 文件上传
  public String putObject(String bucketName, String objectName, File file, String contextType) {
    FileInputStream fileInputStream = null;
    try {
      if (!file.exists() || !file.isFile()) {
        throw new Exception(" file is invalid");
      }
      fileInputStream = new FileInputStream(file);
      contextType = StringUtils.isNotBlank(contextType) ? contextType : "application/octet-stream";
      PutObjectArgs objectArgs = PutObjectArgs.builder().bucket(bucketName).object(objectName).stream(fileInputStream, file.getTotalSpace(), -1).contentType(contextType).build();
      // 文件名称相同覆盖
      minioClient.putObject(objectArgs);
    } catch (Exception e) {
      e.printStackTrace();
      return null;
    }
    return objectName;
  }

  // 文件上传
  public String putObject(String bucketName, String objectName, InputStream stream) {

    try {
      PutObjectArgs objectArgs = PutObjectArgs.builder().bucket(bucketName).object(objectName).stream(stream, stream.available(), -1).contentType("application/octet-stream").build();
      // 文件名称相同覆盖
      minioClient.putObject(objectArgs);
    } catch (Exception e) {
      e.printStackTrace();
      return null;
    }
    return objectName;
  }

  // 生成远程访问URL
  public String getObjectURL(String bucketName, String fileName, int expires) { //
    // 查看文件地址
    GetPresignedObjectUrlArgs presignedObject = new GetPresignedObjectUrlArgs().builder().bucket(bucketName).object(fileName).expiry(expires).method(Method.GET).build();
    try {
      String url = minioClient.getPresignedObjectUrl(presignedObject);
      return url;
    } catch (Exception e) {
      e.printStackTrace();
    }
    return null;
  }
  

  // 下载到本地
  public void localDownload(String bucketName, String objectName, String filePath) {
    try {
      StatObjectResponse response = minioClient.statObject(StatObjectArgs.builder().bucket(bucketName).object(objectName).build());
      if (response != null) {
        minioClient.downloadObject(DownloadObjectArgs.builder().bucket(bucketName).object(objectName).filename(filePath).build());
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
  }

  // 查看文件对象
  public List<String> listObjects(String bucketName) {
    Iterable<Result<Item>> results = minioClient.listObjects(ListObjectsArgs.builder().bucket(bucketName).build());
    List<String> objectList = new ArrayList<>();
    try {
      for (Result<Item> result : results) {
        Item item = result.get();
        if (!item.isDeleteMarker() && !item.isDir()) {
          objectList.add(item.objectName());
        }
      }
    } catch (Exception e) {
      e.printStackTrace();
      return null;
    }
    return objectList;
  }

  // 删除文件
  public boolean remove(String bucketName, String objectName) {
    try {
      minioClient.removeObject(RemoveObjectArgs.builder().bucket(bucketName).object(objectName).build());
    } catch (Exception e) {
      e.printStackTrace();
      return false;
    }
    return true;
  }

}
posted @ 2024-01-24 10:55  ParamousGIS  阅读(97)  评论(0编辑  收藏  举报