import ink.wgink.util.request.StaticResourceRequestUtil;
import io.minio.MinioClient;
import io.minio.PutObjectOptions;
import io.minio.errors.MinioException;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;

/**
 * @ClassName: MinIOTest
 * @Description:
 * @Author: wanggeng
 * @Date: 2021/10/17 12:05 上午
 * @Version: 1.0
 */
public class MinIOTest {

    public static void main(String[] args) throws Exception {
        try {
            // 使用MinIO服务的URL，端口，Access key和Secret key创建一个MinioClient对象
            MinioClient minioClient = new MinioClient("http://192.168.0.188:19001", "demotest", "demotest", false);
            // 检查存储桶是否已经存在
            boolean isExist = minioClient.bucketExists("images");
            if (isExist) {
                System.out.println("Bucket already exists.");
            } else {
                // 创建一个名为asiatrip的存储桶，用于存储照片的zip文件。
                minioClient.makeBucket("images");
            }
            // 使用putObject上传一个文件到存储桶中。
            File file = new File("/Users/wanggeng/Desktop/UploadFiles/images/20210820/1.jpeg");
            InputStream inputStream = new FileInputStream(file);
            PutObjectOptions putObjectOptions = new PutObjectOptions(file.length(), PutObjectOptions.MAX_PART_SIZE);
            // 下载
            // putObjectOptions.setContentType("application/octet-stream");
            // 直接打开
            String contentType = StaticResourceRequestUtil.getContentType("jpeg");
            putObjectOptions.setContentType(contentType);
            minioClient.putObject("images", "demo.jpeg", inputStream, putObjectOptions);
            System.out.println("successfully uploaded");
        } catch (MinioException e) {
            System.out.println("Error occurred: " + e);
        }
    }

}
