集成MIO简单教程示例

在yml文件中 添加:

#MinIo
minio:
  url: http://192.168.x.x:9000
  accessKey: xxxxxxx
  secretKey: xxxxxxx
  bucketName: cms-test

创建MIO实体类:

@Component
@ConfigurationProperties(prefix = "minio")
public class MinIoConfig {
    private String url;

    private String accessKey;

    private String secretKey;

    private String bucketName;

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    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(url).credentials(accessKey, secretKey).build();
    }
}

相应的service:

public interface IMinioFileService {
    JSONArray readFile(String filePath);

  

    InputStream getObject(String bucketName, String objectName) throws Exception;
    /**
     * 将文件保存到指定bucket
     * 创建存储桶并将文件保存到该存储桶中
     */
    void   saveFileToBucket(String bucketName, String filePath, byte[] fileData) throws Exception;
}

相应的实现类Impl:

public JSONArray readFile(String filePath) {
    Reader reader = null;
    InputStream in = null;
    try {
       // 检查存储桶是否已经存在
       boolean found = client.bucketExists(BucketExistsArgs.builder().bucket(minIoConfig.getBucketName()).build());
       if (found) {
          String extension = FilenameUtils.getExtension(filePath);
          in = getObject(minIoConfig.getBucketName(), filePath);
          byte[] bytes = IOUtils.toByteArray(in);
          if (JSON_EXTENSION.equals(extension)) {
             String jsonStr = new String(bytes, StandardCharsets.UTF_8);
             return JSON.parseArray(jsonStr);
          } else {
             byte[] decompress = CompressionUtil.decompress(bytes);
             String jsonStr = new String(decompress, StandardCharsets.UTF_8);
             return JSON.parseArray(jsonStr);
          }
       }
    } catch (Exception e) {
       e.printStackTrace();
    } finally {
       try {
          if (reader != null) {
             reader.close();
          }
          if (in != null) {
             in.close();
          }
       } catch (IOException e) {
          e.printStackTrace();
       }
    }
    return null;
}
/**
 * 创建存储桶并将文件保存到该存储桶中
 *
 * @param filePath   文件路径
 * @param bucketName 存储桶名称
 * @param fileData   文件数据的字节流
 * @throws Exception
 */
public void saveFileToBucket(String bucketName, String filePath, byte[] fileData) throws Exception {
    // 检查存储桶是否存在
    boolean bucketExists = client.bucketExists(BucketExistsArgs.builder().bucket(bucketName).build());
    if (!bucketExists) {
       // 如果不存在,则创建存储桶
       client.makeBucket(MakeBucketArgs.builder().bucket(bucketName).build());
    }
    // 将文件数据保存到存储桶
    try (InputStream inputStream = new ByteArrayInputStream(fileData)) {
       PutObjectArgs putObjectArgs = PutObjectArgs.builder()
             .bucket(bucketName)
             .object(filePath)
             .stream(inputStream, fileData.length, -1)
             .build();
       client.putObject(putObjectArgs);
    }
}

你可能感兴趣的:(java学习,笔记,java)