minio与java使用

1、linux配置minio

1、linux直接执行以下命令

     后台启动minio

wget https://dl.min.io/server/minio/release/linux-amd64/minio
chmod +x minio
nohup ./minio server /opt/minio/data &

2、配置永久链接

     1、下载mc

     wget https://dl.min.io/client/mc/release/linux-amd64/mc

     2、执行以下命令

      alias mc="./mc"

      sudo chmod -R 777 mc  给权限

    3、mc alias set minio http://121.36.69.211:9000 minioadmin minioadmin --api s3v4

    4、mc policy set public minio/桶名称

      之后就可以直接用ip+文件路径下载了

2、java下载 

       1、添加依赖

        
        
            io.minio
            minio
            3.0.10
        

      2、添加工具类 

public class MinIoUtils {

    static final String URL = ProjectProperties.fileCtx;
    static final String USER_NAME = "minioadmin";
    static final String PASSWORD = "minioadmin";


    /**
     * 用户的存储空间(bucket)名称
     */
    static final String BUCKET_NAME = "yong-bei";

    /**
     * 指定项目文件夹
     */
    private static final String PIC_LOCATION = "images";


    private static MinioClient minioClient;

    /**
     *
     * @create 2017/11/25 12:44
     **/
    static {
        try {
            minioClient = new MinioClient(URL, USER_NAME, PASSWORD);
        } catch (InvalidEndpointException e) {
            e.printStackTrace();
        } catch (InvalidPortException e) {
            e.printStackTrace();
        }
    }

    /**
     * 上传文件
     */
    public static String uploadFile(InputStream is,String directory,  String fileName) {
        String key = PIC_LOCATION + "/" + fileName ;
        String url = "";
        if (Objects.isNull(directory)) {
            key = fileName;
        }
        try {
            boolean isExist = minioClient.bucketExists(BUCKET_NAME);
            if(!isExist) {
                // 创建一个新的bucket
                minioClient.makeBucket(BUCKET_NAME);
            }
            minioClient.putObject(BUCKET_NAME, key, is, getContentType(fileName));

            url = minioClient.getObjectUrl(BUCKET_NAME, key);
//            url = minioClient.presignedGetObject(BUCKET_NAME, key);
            System.out.println("key:" + key);
            System.out.println("url:" + url);
            if(ObjectUtils.isNotEmpty(url)){
                url = url.replace(URL,"");
            }
            System.out.println("after url:" + url);

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

        }
        return url;
    }

    /**
     * 获取文件的ContentType
     * @param fileUrl
     */
    public static String getContentType(String fileUrl) {
        String contentType = null;
        try {
            contentType = new MimetypesFileTypeMap().getContentType(new File(fileUrl));
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("getContentType, File ContentType is : " + contentType);
        return contentType;
    }
}

 

你可能感兴趣的:(工具)