package wangbiao.config.minio;

import com.fasterxml.jackson.core.JsonProcessingException;
import io.minio.*;
import io.minio.errors.*;
import io.minio.messages.DeleteError;
import io.minio.messages.DeleteObject;
import lombok.SneakyThrows;
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.test.context.junit4.SpringRunner;

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.*;

@RunWith(SpringRunner.class)
@SpringBootTest
public class MinIoTest {

    @Autowired
    private MinioClient minioClient;
    private final String bucketName = "AAAAA";


    @Test
        //上传已知大小的输入流
    void getObject0() throws IOException, InvalidKeyException, InvalidResponseException, InsufficientDataException,
            NoSuchAlgorithmException, ServerException, InternalException, XmlParserException, ErrorResponseException {
        File file = new File("C:\\Users\\lzadmin\\Desktop\\MinIoUtil.java");
        String name = file.getName();
        String substring = name.substring(name.lastIndexOf("."));
        FileInputStream inputStream = new FileInputStream(file);
        minioClient.putObject(
                PutObjectArgs.builder().bucket(bucketName).object("aaaa").stream(
                        inputStream, 2795, -1)
                        .contentType(substring)
                        .build());
    }

    @SneakyThrows
    @Test
        //上传W未知知大小的输入流
    void getObject1() throws FileNotFoundException {
        File file = new File("C:\\Users\\lzadmin\\Desktop\\MinIoUtil.java");
        String name = file.getName();
        String substring = name.substring(name.lastIndexOf("."));
        FileInputStream inputStream = new FileInputStream(file);
        minioClient.putObject(
                PutObjectArgs.builder().bucket(bucketName).object("bbbb").stream(
                        inputStream, -1, 10485760)
                        .contentType(substring)
                        .build());
    }

    @Test
        //创建带有"/"结尾的文件夹
    void getObject2() throws IOException, InvalidKeyException, InvalidResponseException, InsufficientDataException,
            NoSuchAlgorithmException, ServerException, InternalException, XmlParserException, ErrorResponseException {
        minioClient.putObject(
                PutObjectArgs.builder().bucket(bucketName).object("cccc/").stream(
                        new ByteArrayInputStream(new byte[]{}), 0, -1)
                        .build());
    }

    @Test
        //上床具有header信息和用户元数据的文件流
    void getObject3() throws IOException, InvalidKeyException, InvalidResponseException, InsufficientDataException,
            NoSuchAlgorithmException, ServerException, InternalException, XmlParserException, ErrorResponseException {
        File file = new File("C:\\Users\\lzadmin\\Desktop\\MinIoUtil.java");
        FileInputStream inputStream = new FileInputStream(file);
        Map<String, String> headers = new HashMap<>();
        headers.put("X-Amz-Storage-Class", "REDUCED_REDUNDANCY");
        Map<String, String> userMetadata = new HashMap<>();
        userMetadata.put("My-Project", "Project One");
        ObjectWriteResponse objectWriteResponse = minioClient.putObject(
                PutObjectArgs.builder().bucket(bucketName).object("dddd").stream(
                        inputStream, 2795, -1)
                        .headers(headers)
                        .userMetadata(userMetadata)
                        .build());
        String etag = objectWriteResponse.etag();
        String versionId = objectWriteResponse.versionId();
        String bucket = objectWriteResponse.bucket();
        String object = objectWriteResponse.object();
        String region = objectWriteResponse.region();
        System.out.println("etag:" + etag);
        System.out.println("versionId:" + versionId);
        System.out.println("object:" + object);
        System.out.println("bucket:" + bucket);
        System.out.println("region:" + region);

    }

    @Test
        // 上传带有服务器信息的流
    void getObject4() throws IOException, InvalidKeyException, InvalidResponseException, InsufficientDataException,
            NoSuchAlgorithmException, ServerException, InternalException, XmlParserException, ErrorResponseException {
        HashMap<String, String> stringStringHashMap = new HashMap<>();
        ServerSideEncryptionKms serverSideEncryptionKms = new ServerSideEncryptionKms("132", stringStringHashMap);
        File file = new File("C:\\Users\\lzadmin\\Desktop\\MinIoUtil.java");
        FileInputStream inputStream = new FileInputStream(file);
        minioClient.putObject(
                PutObjectArgs.builder().bucket(bucketName).object("eeee").stream(
                        inputStream, 2795, -1)
                        .sse(serverSideEncryptionKms)
                        .build());
    }

    @Test
        //把文件作为对象存在桶里
    void getObject5() throws IOException, ServerException, InsufficientDataException, InternalException,
            InvalidResponseException, InvalidKeyException, NoSuchAlgorithmException, XmlParserException,
            ErrorResponseException {
        minioClient.uploadObject(
                UploadObjectArgs.builder()
                        .bucket(bucketName)
                        .object("ffffff")
                        .filename("C:\\Users\\lzadmin\\Desktop\\MinIoUtil.java")
                        .build());

//// 设置上传内容类型
//        minioClient.uploadObject(
//                UploadObjectArgs.builder()
//                        .bucket("my-bucketname")
//                        .object("my-objectname")
//                        .filename("my-video.avi")
//                        .contentType("video/mp4")
//                        .build());
    }

    //多文件上传    ?T
    @Test
    void getObject6() throws IOException, InvalidKeyException, InvalidResponseException, InsufficientDataException,
            NoSuchAlgorithmException, ServerException, InternalException, XmlParserException, ErrorResponseException {

        File file0 = new File("C:\\Users\\lzadmin\\Desktop\\MinIoUtil.java");
        FileInputStream inputStream0 = new FileInputStream(file0);
        byte[] bytes0 = new byte[1024];
        ByteArrayOutputStream output0 = null;
        int read0;
        while ((read0 = inputStream0.read(bytes0)) != -1) {
            output0.write(bytes0, 0, read0);
        }
        byte[] buffer0 = output0.toByteArray();


        File file1 = new File("C:\\Users\\lzadmin\\Desktop\\MinIoUtil.java");
        FileInputStream inputStream1 = new FileInputStream(file1);
        byte[] bytes1 = new byte[1024];
        ByteArrayOutputStream output1 = null;
        int read1;
        while ((read1 = inputStream1.read(bytes1)) != -1) {
            output0.write(bytes1, 0, read1);
        }
        byte[] buffer1 = output1.toByteArray();

        List<SnowballObject> objects = new ArrayList<SnowballObject>();
        objects.add(
                new SnowballObject(
                        "gggg",
                        new ByteArrayInputStream(buffer0),
                        5,
                        null));
        objects.add(
                new SnowballObject(
                        "hhhhhh",
                        new ByteArrayInputStream(buffer1),
                        4,
                        null));
        minioClient.uploadSnowballObjects(
                UploadSnowballObjectsArgs.builder().bucket(bucketName).objects(objects).build());

    }

    @Test
    void getObject7() throws IOException, InvalidKeyException, InvalidResponseException, InsufficientDataException,
            NoSuchAlgorithmException, ServerException, InternalException, XmlParserException, ErrorResponseException {

        //删除文件
        minioClient.removeObject(
                RemoveObjectArgs.builder().bucket(bucketName).object("aaaa").build());

// 删除指定版本的案件
        minioClient.removeObject(
                RemoveObjectArgs.builder()
                        .bucket(bucketName)
                        .object("my-versioned-objectname")
                        .versionId("my-versionid")
                        .build());

// Remove versioned object bypassing Governance mode.
        minioClient.removeObject(
                RemoveObjectArgs.builder()
                        .bucket("my-bucketname")
                        .object("my-versioned-objectname")
                        .versionId("my-versionid")
                        .bypassGovernanceMode(true)//旁路管理模式
                        .build());
    }

    @Test
    //删除多个文件
    void getObject8() throws IOException, InvalidKeyException, InvalidResponseException, InsufficientDataException,
            NoSuchAlgorithmException, ServerException, InternalException, XmlParserException, ErrorResponseException {
        List<DeleteObject> objects = new LinkedList<>();
        objects.add(new DeleteObject("aaaa"));
        objects.add(new DeleteObject("bbbb"));
        objects.add(new DeleteObject("cccc"));
        Iterable<Result<DeleteError>> results =
                minioClient.removeObjects(
                        RemoveObjectsArgs.builder().bucket(bucketName).objects(objects).build());
        for (Result<DeleteError> result : results) {
            DeleteError error = result.get();
            System.out.println(
                    "Error in deleting object " + error.objectName() + "; " + error.message());
        }
    }

    @Test
    void getObject9() {

    }

    @Test
    void getObject10() {

    }
}

 

原文地址:http://www.cnblogs.com/wangbiaohistory/p/16867390.html

1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长! 2. 分享目的仅供大家学习和交流,请务用于商业用途! 3. 如果你也有好源码或者教程,可以到用户中心发布,分享有积分奖励和额外收入! 4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解! 5. 如有链接无法下载、失效或广告,请联系管理员处理! 6. 本站资源售价只是赞助,收取费用仅维持本站的日常运营所需! 7. 如遇到加密压缩包,默认解压密码为"gltf",如遇到无法解压的请联系管理员! 8. 因为资源和程序源码均为可复制品,所以不支持任何理由的退款兑现,请斟酌后支付下载 声明:如果标题没有注明"已测试"或者"测试可用"等字样的资源源码均未经过站长测试.特别注意没有标注的源码不保证任何可用性