MinIO应用类


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;
  }


}


2.应用类

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.MinioClient;
import io.minio.RemoveObjectArgs;
import io.minio.RemoveObjectsArgs;
import io.minio.Result;
import io.minio.StatObjectArgs;
import io.minio.StatObjectResponse;
import io.minio.UploadObjectArgs;
import io.minio.errors.MinioException;
import io.minio.http.Method;
import io.minio.messages.DeleteError;
import io.minio.messages.DeleteObject;

@Component
public class MinioTemplate {



  private static MinioConfig minioConfig;

  @Autowired
  public void setMinioConfig(MinioConfig minioConfig) {
    MinioUtils.minioConfig = minioConfig;
  }

  private static MinioClient minioClient;

  public static void getMinioClient() {
    try {
      if (minioClient == null) {
        // 创建一个MinioClient对象
        minioClient = MinioClient.builder().endpoint(minioConfig.getEndpoint()).credentials(minioConfig.getAccessKey(), minioConfig.getSecretKey()).build();
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
  }

  // 上传文件
  public void uploadFile(String bucketName, String objectName, String fileName) throws Exception {
    try {
      getMinioClient();
      // 上传文件
      minioClient.uploadObject(UploadObjectArgs.builder().bucket(bucketName).object(objectName).filename(fileName).build());     
    } catch (MinioException e) {
      e.printStackTrace();
    }
  }

  // 下载文件
  public void downloadFile(String bucketName, String objectName, String fileName) {
    try {
      getMinioClient();
      // 下载对象
      minioClient.downloadObject(DownloadObjectArgs.builder().bucket(bucketName).object(objectName).filename(fileName).build());
    } catch (Exception e) {
      e.printStackTrace();
    }
  }

  // 删除文件
  public void deleteFile(String bucketName, String objectName) {
    try {
      getMinioClient();
      // 判断对象是否存在
      StatObjectResponse statObjectResponse = minioClient.statObject(StatObjectArgs.builder().bucket(bucketName).object(objectName).build());
    } catch (Exception e) {
      e.printStackTrace();
      return;
    }
    try {
      // 删除文件
      minioClient.removeObject(RemoveObjectArgs.builder().bucket(bucketName).object(objectName).build());
    } catch (Exception e) {
      e.printStackTrace();
    }
  }

  // 批量删除文件
  public void deleteFileBatch(String bucketName, Iterable<DeleteObject> objects) {
    try {
      getMinioClient();
      // 批量删除文件
      Iterable<Result<DeleteError>> results = minioClient.removeObjects(RemoveObjectsArgs.builder().bucket(bucketName).objects(objects).build());
      for (Result<DeleteError> result : results) {
        DeleteError error = result.get();
        System.out.println("Error in deleting object " + error.objectName() + "; " + error.message());
      }

    } catch (Exception e) {
      e.printStackTrace();
    }
  }

  // 生成文件分享URL
  public String shareFilePath(String bucketName, String objectName) {
    String url = "";
    try {
      getMinioClient();
      // 生成文件分享URL
      url = minioClient.getPresignedObjectUrl(GetPresignedObjectUrlArgs.builder().method(Method.GET).bucket(bucketName).object(objectName).expiry(60 * 60 * 24).build());
    } catch (Exception e) {
      e.printStackTrace();
    }
    return url;
  }

  // 生成文件分享URL
  public String shareFilePath(String objectName) {
    String url = "";
    try {
      getMinioClient();
      // 生成文件分享URL
      url = minioClient.getPresignedObjectUrl(GetPresignedObjectUrlArgs.builder().method(Method.GET).bucket(minioConfig.getBucketName()).object(objectName).expiry(60 * 60 * 24).build());
    } catch (Exception e) {
      e.printStackTrace();
    }
    return url;
  }

  // 获取对象元数据
  public StatObjectResponse getStatObject(String objectName) {
    try {
      getMinioClient();
      // 获取对象元数据
      StatObjectResponse statObjectResponse = minioClient.statObject(StatObjectArgs.builder().bucket(minioConfig.getBucketName()).object(objectName).build());
      return statObjectResponse;
    } catch (Exception e) {
      e.printStackTrace();
    }
    return null;
  }

  // 判断bucket是否存在
  public boolean isExistsBucket(String bucketName) {
    try {
      getMinioClient();
      // 检查存储桶是否已经存在
      boolean found = minioClient.bucketExists(BucketExistsArgs.builder().bucket(bucketName).build());
      if (!found) {
        return false;
      }
    } catch (Exception e) {
      e.printStackTrace();
      return false;
    }
    return true;
  }

}


------------------------

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