首页 > 其他分享 >minio基本使用

minio基本使用

时间:2023-03-20 20:00:09浏览次数:36  
标签:基本 minio builder bucket build 使用 import minioClient bucketName

  1. 使用例子

    package com.lomi.minio;
    
    import cn.hutool.core.io.FileUtil;
    import cn.hutool.core.io.IoUtil;
    import cn.hutool.http.ContentType;
    import cn.hutool.json.JSONUtil;
    import com.alibaba.fastjson.JSONObject;
    import io.minio.*;
    import io.minio.errors.*;
    import io.minio.http.Method;
    import io.minio.messages.Bucket;
    import io.minio.messages.DeleteError;
    import io.minio.messages.DeleteObject;
    import io.minio.messages.Item;
    import okhttp3.*;
    import org.apache.commons.io.FilenameUtils;
    import org.junit.Before;
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.test.context.SpringBootTest;
    import org.springframework.core.io.ClassPathResource;
    import org.springframework.test.context.junit4.SpringRunner;
    
    import javax.annotation.Resource;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.nio.charset.Charset;
    import java.security.InvalidKeyException;
    import java.security.NoSuchAlgorithmException;
    import java.time.LocalDate;
    import java.time.ZonedDateTime;
    import java.time.format.DateTimeFormatter;
    import java.util.*;
    import java.util.concurrent.TimeUnit;
    
    /**
     * minio 测试类
     */
    //@RunWith(SpringRunner.class)
    //@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
    public class MinioTest {
    
        String bucketName = "miniobucket";
    
        //@Resource
        MinioClient minioClient;
    
    
        @Before
        public void before(){
            MinioClient minioClient = MinioClient.builder()
                    .endpoint("http://" + "192.168.100.66" + ":" + "9000")
                    .credentials("minioadmin", "minioadmin")
                    .build();
    
            this.minioClient = minioClient;
        }
    
    
    
    
        /**
         * put上传文件
         * @throws Exception
         */
        @Test
        public void putFile() throws Exception {
            ClassPathResource classPathResource = new ClassPathResource("temp\\minio\\测试文件1.txt");
            File file = classPathResource.getFile();
            String oldFileName = file.getName();
    
            //创建bucket
            if (!minioClient.bucketExists(BucketExistsArgs.builder().bucket(bucketName).build())) {
                minioClient.makeBucket(MakeBucketArgs.builder().bucket(bucketName).build());
            }
    
            //String newFileName = generateNewFileName(oldFileName);
            String newFileName = classPathResource.getFilename();
    
    
            //上传
            InputStream inputStream = new FileInputStream( file );
            PutObjectArgs putObjectArgs = PutObjectArgs.builder()
                    .bucket(bucketName)
                    .contentType(ContentType.TEXT_PLAIN.getValue())
                    .object(newFileName)
                    .stream(inputStream, inputStream.available(), -1)
                    .build();
            ObjectWriteResponse objectWriteResponse = minioClient.putObject(putObjectArgs);
            System.out.println( "上传的文件地址是:" + newFileName );
    
    
            //获取文件流
            GetObjectArgs objectArgs = GetObjectArgs.builder().bucket(bucketName).object(newFileName).build();
            GetObjectResponse response = minioClient.getObject(objectArgs);
            String content = IoUtil.read(response, Charset.forName("utf8"));
    
    
    
            //获取文件下载地址,并且指定过去时间
            GetPresignedObjectUrlArgs getPresignedObjectUrlArgs = GetPresignedObjectUrlArgs.builder()
                    .method(Method.GET)
                    .bucket(bucketName)
                    .object(newFileName)
                    .expiry(20, TimeUnit.SECONDS)
                    .build();
            String url = minioClient.getPresignedObjectUrl(getPresignedObjectUrlArgs);
            System.out.println("文件请求地址:" + url );
    
    
            //下载downloadObject
            //下载到 TestExample\target\classes\temp\minio\测试文件2.txt
            String downloadPath = new ClassPathResource("temp\\minio").getFile().getAbsolutePath();
            System.out.println("下载路径" +  downloadPath );
            minioClient.downloadObject(DownloadObjectArgs.builder().bucket(bucketName).object(newFileName )
                    .filename(downloadPath+"/"+"测试文件2.txt")
                    .build());
    
    
            System.out.println( "读取到的文件是:" + content );
        }
    
    
    
    
        /**
         * upload上传文件(只能通过文件路径上传文件,put方式可以通过流的方式)
         * @throws Exception
         */
        @Test
        public void uploadFile() throws Exception {
            ClassPathResource classPathResource = new ClassPathResource("temp\\minio\\测试文件1.txt");
            File file = classPathResource.getFile();
    
            //创建bucket
            if (!minioClient.bucketExists(BucketExistsArgs.builder().bucket(bucketName).build())) {
                minioClient.makeBucket(MakeBucketArgs.builder().bucket(bucketName).build());
            }
    
            //String newFileName = generateNewFileName(oldFileName);
            String newFileName = classPathResource.getFilename();
    
    
            //上传
            InputStream inputStream = new FileInputStream( file );
            PutObjectArgs putObjectArgs = PutObjectArgs.builder()
                    .bucket(bucketName)
                    .contentType(ContentType.TEXT_PLAIN.getValue())
                    .object(newFileName)
                    .stream(inputStream, inputStream.available(), -1)
                    .build();
            ObjectWriteResponse objectWriteResponse = minioClient.uploadObject( UploadObjectArgs.builder()
                    .bucket(bucketName)
                    .contentType(ContentType.TEXT_PLAIN.getValue())
                    .object(newFileName)
                    .filename( file.getAbsolutePath() )
                    .build() );
            System.out.println( "上传的文件地址是:" + newFileName );
    
        }
    
    
    
    
        /**
         *  表单预上传文件
         * @throws Exception
         */
        @Test
        public void fromUploadFile() throws Exception {
            String objectName = "2023/03/20/无标题图片2.jpg";
    
            // 为存储桶创建一个上传策略,过期时间为7天
            PostPolicy policy = new PostPolicy(bucketName, ZonedDateTime.now().plusDays(7));
            // 设置一个参数key-value,值为上传对象的名称(保存在桶中的名字)
            policy.addEqualsCondition("key", objectName);
            // 添加 Content-Type以"image/"开头,表示只能上传照片
            policy.addStartsWithCondition("Content-Type", "image/png");
            policy.addContentLengthRangeCondition(10 * 1024, 10 * 1024 * 1024);
            Map<String, String> formData = minioClient.getPresignedPostFormData(policy);
    
    
            fromUploadFile2( formData , objectName );
    
            System.out.println(JSONUtil.toJsonStr( formData ));
        }
    
        /**
         * 代码模拟from上传文件
         * @param formData
         * @param objectName
         * @throws Exception
         */
        public void fromUploadFile2(Map<String, String> formData,String objectName) throws Exception {
            File localFile = new File("C:\\work\\testCode\\TestExample\\src\\main\\resources\\temp\\minio\\无标题.png");
    
            //拼装from请求参数
            MultipartBody.Builder multipartBuilder = new MultipartBody.Builder();
            multipartBuilder.setType(MultipartBody.FORM);
            multipartBuilder.addFormDataPart("key", objectName);// 必须要和策略参数一样
            multipartBuilder.addFormDataPart("Content-Type", "image/png");
            //这里是 name = file,value=二进制文件
            multipartBuilder.addFormDataPart("file", objectName, RequestBody.create(localFile, null));
            //把预上传的值都带上
            for (Map.Entry<String, String> entry : formData.entrySet()) {
                multipartBuilder.addFormDataPart(entry.getKey(), entry.getValue());
            }
    
    
            //from上传地址是到bucket的
            Request request =
                    new Request.Builder()
                            .url("http://192.168.100.66:9000/" + bucketName)
                            .post(multipartBuilder.build())
                            .build();
            OkHttpClient httpClient = new OkHttpClient().newBuilder().build();
            Response response = httpClient.newCall(request).execute();
            if (response.isSuccessful()) {
                System.out.println("uploaded successfully using POST object");
            } else {
                System.out.println("Failed to upload Pictures");
            }
    
        }
    
    
    
    
    
        /**
         * 一些其他方法
         * @throws Exception
         */
        @Test
        public void t2() throws Exception {
            //bucket是否存在
            boolean bucketExist = minioClient.bucketExists(BucketExistsArgs.builder().bucket(bucketName+1).build());
            System.out.println( "bucketExist:"  + bucketExist );
    
            //删除bucket
            System.out.println("删除指定bucket------------------");
            if( bucketExist ){
                minioClient.removeBucket(RemoveBucketArgs.builder().bucket(bucketName+"1").build());
            }
    
            //创建bucket
            minioClient.makeBucket(MakeBucketArgs.builder().bucket(bucketName+"1").build());
    
    
            //查询所有bucket
            List<Bucket> buckets = minioClient.listBuckets(  );
            System.out.println("查询所有bucket-------------------------------");
            buckets.forEach( item-> System.out.println( item.name() ));
    
    
    
            //获取所有的文件(需要/结尾表示目录,不然只能取到指定目录)//.recursive
            System.out.println("查询指定目录下面的bucket-------------------------------");
            Iterable<Result<Item>> myObjects = minioClient.listObjects(ListObjectsArgs.builder().bucket(bucketName).prefix("2023/03/20/").maxKeys(100).build());
            for (Result<Item> result : myObjects) {
                Item item = result.get();
                System.out.println("文件:" + item.objectName() );
            }
    
    
    
            //删除文件(删除不存在的文件不会报错)
            //System.out.println("删除单个文件-------------------------------");
            minioClient.removeObject(RemoveObjectArgs.builder().bucket( bucketName ).object("2023/03/20/1bb94970ee61426682a45f8f9105990b.txt").build());
    
    
            //批量删除
            List<DeleteObject> objects = new ArrayList<>();
            objects.add(new DeleteObject("2023/03/20/020b26d3b00940828b47ba57c77b454c.txt"));
            objects.add(new DeleteObject("2023/03/20/1bb94970ee61426682a45f8f9105990b.txt"));
            objects.add(new DeleteObject("2023/03/20/7a874e7e2c1e47a8ae2e22c782926e99.txt"));
    
    
            System.out.println("批量删除文件(removeObjects 要求强制遍历结果,赖删除可能被丢弃 )-------------------------------");
            Iterable<Result<DeleteError>> results = minioClient.removeObjects(RemoveObjectsArgs.builder().bucket(bucketName).objects(objects).build());
            results.forEach(item -> {
                try {
                    System.out.println("异常:" + item.get());
                } catch (Exception e) {
                    e.printStackTrace();
                }
            });
    
    
            //复制,从一个bucket 复制到另外一个bucket
            minioClient.copyObject(CopyObjectArgs.builder()
                    .source(CopySource.builder()
                            .bucket(bucketName)
                            .object("2023/03/20/7caa4813faf042df9c831ee7a2a7ceb6.txt")
                            .build())
                    .bucket(bucketName+1)
                    .object("2023/03/20/7caa4813faf042df9c831ee7a2a7ceb6.txt")
                    .build());
        }
    
    
        /**
         * 生成按照日期存放的随机文件名
         * @param oldFileName
         * @return
         */
        private String generateNewFileName( String oldFileName ){
            String datePath = LocalDate.now().format(DateTimeFormatter.ofPattern("yyyy/MM/dd"));
            String uuid = UUID.randomUUID().toString().replace("-", "");
            String extension = FilenameUtils.getExtension(oldFileName);
            String newFileName = datePath + "/" + uuid + "." + extension;
            return newFileName;
        }
    
    
    }
    
    
  2. getPresignedPostFormData 预上传文件方式后续文件上传
    image-20230320181648404

标签:基本,minio,builder,bucket,build,使用,import,minioClient,bucketName
From: https://www.cnblogs.com/cxygg/p/17237524.html

相关文章