minio

注入minio客户端,其中endpoint(endpoint): 这里设置Minio服务器的URL;credentials(accessKey, secretKey): 这里设置用于认证的accessKeysecretKey。类似于用户名和密码。

 @Bean
    public MinioClient minioClient() {

        return MinioClient.builder().endpoint(endpoint).credentials(accessKey, secretKey).build();
    }

判断桶是否存在

BucketExistsArgs:用于创建检查特定存储桶(Bucket)是否存在的请求参数的类
boolean exists=minioClient.bucketExists(BucketExistsArgs.builder().bucket(name).build());

创建存储桶:

minioClient.makeBucket(MakeBucketArgs.builder().bucket(name).build());

删除桶:

 minioClient.removeBucket(RemoveBucketArgs.builder().bucket(name).build());

单个文件上传:

public String upload(MultipartFile file){

InputStream in=null;
try{

//获取输入流
in=file.getInputStream();
minioClient.putObject(PutObjectArgs.builder().bucket(bucketName).
object(filename).stream(in,in.available(),-1).contentType(file.getContentType()).build());
     
}catch (Exception e){
            e.printStackTrace();
        }
finally {
            if (in!=null){
                try {
                    in.close();
                }catch (IOException e){
                    e.printStackTrace();
                }
            }
        }
        return filename;   

}

下载文件:

 public void download(String fileName,HttpServletResponse response){

    InputStream in =null;
    OutputStream out=null;
    try{
         out=response.getOutputStream();
         in = minioClient.
            getObject(GetObjectArgs.builder().bucket(bucketName).object(fileName).build());
        byte buf[]=new byte[1024];
            int length=0;
            response.reset();
            response.setContentType("application/octet-stream");
            response.setCharacterEncoding("UTF-8");
            response.setHeader("Access-Control-Allow-Origin","*");
            response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));

            while ((length=in.read(buf))>0){
                //每次读取到数据后就写入到输入流中
                out.write(buf,0,length);
            }

}catch (Exception e) {
            e.printStackTrace();
        }
            finally {
                try {
                    if (in != null) {
                        try {
                            in.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                    if (out != null)
                        out.close();
                }
                        catch (IOException e){
                            e.printStackTrace();
                        }

                }
                  
             }




  • 9
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值