Java操作Minio文件实体工具类

1、首先导入minio依赖

        
            io.minio
            minio
            8.2.1
        

2、实体操作工具类

@Value("${minio.endpoint}")
    private String endpoint;

private MinioClient minioClient;

@PostConstruct
    public void init() {
        minioClient = MinioClient
                        .builder()
                        .endpoint("http://" + endPoint)
                        .credentials(ak, sk)
                        .build();
            log.info("创建完毕 MinioClient...");

    }

    /**
     * 上传流文件
     * @param inputStream
     * @param key
     * @return
     */
    public String putThumbInputStream(InputStream inputStream, String key) {
        try {
            this.uploadFile(ossBucketName,key,inputStream);
            String url = "http://" + ossEndpoint + "/" + ossBucketName + "/" + key;
            return url;
        } catch (Exception e) {
            log.error("上传文件,异常为{}", e.getMessage());
            return null;
        }
    }

    /**
     * 获取文件流
     * @param bucketName 存储桶
     * @param objectName 文件名
     * @return 二进制流
     */
    public InputStream getObjectMinio(String bucketName, String objectName) throws Exception {
        return minioClient.getObject(
                GetObjectArgs.builder()
                        .bucket(bucketName)
                        .object(objectName)
                        .build());
    }
    /**
     * 流上传文件
     * @param bucketName
     * @param objectName
     * @param inputStream
     * @return
     * @throws Exception
     */
    public ObjectWriteResponse uploadFile(String bucketName, String objectName, InputStream inputStream) throws Exception {
        return minioClient.putObject(
                PutObjectArgs.builder()
                        .bucket(bucketName)
                        .object(objectName)
                        .stream(inputStream, inputStream.available(), -1)
                        .build());
    }

你可能感兴趣的:(java,python,开发语言,minio)