码迷,mamicode.com
首页 > 其他好文 > 详细

Minio sdk 测试类

时间:2020-04-29 12:46:55      阅读:102      评论:0      收藏:0      [点我收藏+]

标签:sid   ict   conflict   NPU   long   quartz   point   mes   secret   

maven

<properties>
    <minio.version>6.0.13</minio.version>
</properties>
<dependency>
            <groupId>io.minio</groupId>
            <artifactId>minio</artifactId>
            <version>${minio.version}</version>
        </dependency>

java 测试类

import io.minio.MinioClient;
import io.minio.Result;
import io.minio.ServerSideEncryption;
import io.minio.errors.*;
import io.minio.messages.Bucket;
import io.minio.messages.Item;
import io.minio.messages.Upload;
import org.junit.Before;
import org.junit.Test;
import org.xmlpull.v1.XmlPullParserException;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.HashMap;
import java.util.List;

public class MinioTest {
    private MinioClient minioClient;
    private String endpoint = "https://play.min.io";
    private String accessKey = "Q3AM3UQ867SPQQA43P2F";
    private String secretKey = "zuf+tfteSlswRu7BJ86wekitnifILbZam1KYY3TG";

    @Before
    public void before() throws InvalidPortException, InvalidEndpointException {
        minioClient = new MinioClient(endpoint, accessKey, secretKey);
    }

    @Test
    public void listBuckets() throws IOException, InvalidKeyException, NoSuchAlgorithmException, InsufficientDataException, InternalException, NoResponseException, InvalidBucketNameException, XmlPullParserException, ErrorResponseException, InvalidResponseException {
        List<Bucket> bucketList = minioClient.listBuckets();
        for (Bucket bucket : bucketList) {
            System.out.println(bucket.creationDate() + ", " + bucket.name());
        }
    }

    @Test
    public void bucketExists() throws IOException, InvalidKeyException, NoSuchAlgorithmException, InsufficientDataException, InternalException, NoResponseException, InvalidBucketNameException, XmlPullParserException, ErrorResponseException, InvalidResponseException {
        // Check whether ‘my-bucketname‘ exist or not.
        boolean found = minioClient.bucketExists("my-bucketname");
        if (found) {
            System.out.println("my-bucketname exists");
        } else {
            System.out.println("my-bucketname does not exist");
        }
    }

    @Test
    public void makeBucket () throws IOException, InvalidKeyException, NoSuchAlgorithmException, InsufficientDataException, InternalException, NoResponseException, InvalidBucketNameException, XmlPullParserException, ErrorResponseException, RegionConflictException, InvalidResponseException {
        boolean found = minioClient.bucketExists("my-bucketname");
        if (found) {
            System.out.println("my-bucketname already exists");
        } else {
            // Create bucket ‘my-bucketname‘.
            minioClient.makeBucket("my-bucketname");
            System.out.println("my-bucketname is created successfully");
        }
    }

    @Test
    public void removeBucket () throws IOException, InvalidKeyException, NoSuchAlgorithmException, InsufficientDataException, InternalException, NoResponseException, InvalidBucketNameException, XmlPullParserException, ErrorResponseException, InvalidResponseException {
        boolean found = minioClient.bucketExists("my-bucketname");
        if (found) {
            minioClient.removeBucket("my-bucketname");
            System.out.println("my-bucketname is removed successfully");
        } else {
            System.out.println("my-bucketname does not exist");
        }
    }

    /**
     * 获取指定bucket 下所有上传未完成的文件
     * @throws IOException
     * @throws InvalidKeyException
     * @throws NoSuchAlgorithmException
     * @throws InsufficientDataException
     * @throws InternalException
     * @throws NoResponseException
     * @throws InvalidBucketNameException
     * @throws XmlPullParserException
     * @throws ErrorResponseException
     */
    @Test
    public void listIncompleteUploads () throws IOException, InvalidKeyException, NoSuchAlgorithmException, InsufficientDataException, InternalException, NoResponseException, InvalidBucketNameException, XmlPullParserException, ErrorResponseException, InvalidResponseException {
        boolean found = minioClient.bucketExists("my-bucketname");
        if (found) {
            // List all incomplete multipart upload of objects in ‘my-bucketname‘
            Iterable<Result<Upload>> myObjects = minioClient.listIncompleteUploads("my-bucketname");
            for (Result<Upload> result : myObjects) {
                Upload upload = result.get();
                System.out.println(upload.uploadId() + ", " + upload.objectName());
            }
        } else {
            System.out.println("my-bucketname does not exist");
        }
    }

    @Test
    public void listObjects() throws IOException, InvalidKeyException, NoSuchAlgorithmException, InsufficientDataException, InternalException, NoResponseException, InvalidBucketNameException, XmlPullParserException, ErrorResponseException, InvalidResponseException {
        // 校验bucket 是否存在
        boolean found = minioClient.bucketExists("my-bucketname");
        if (found) {
            // List objects from ‘my-bucketname‘
            Iterable<Result<Item>> myObjects = minioClient.listObjects("my-bucketname");
            for (Result<Item> result : myObjects) {
                Item item = result.get();
                System.out.println(item.lastModified() + ", " + item.size() + ", " + item.objectName());
            }
        } else {
            System.out.println("my-bucketname does not exist");
        }
    }


    @Test
    public void upload() throws IOException, XmlPullParserException, NoSuchAlgorithmException, InvalidKeyException, InvalidArgumentException, InternalException, NoResponseException, InvalidBucketNameException, InsufficientDataException, ErrorResponseException, InvalidResponseException {
        // Create some content for the object.
        StringBuilder builder = new StringBuilder();
        for (int i = 0; i < 1000; i++) {
            builder.append(
                    "Sphinx of black quartz, judge my vow: Used by Adobe InDesign to display font samples. ");
        }

        // Create a InputStream for object upload.
        ByteArrayInputStream bais = new ByteArrayInputStream(builder.toString().getBytes("UTF-8"));

        // Create object ‘my-objectname‘ in ‘my-bucketname‘ with content from the input stream.
        minioClient.putObject("my-bucketname", "my-objectname", bais ,(long)builder.toString().getBytes().length,new HashMap<String,String>(), ServerSideEncryption.atRest(),"application/octet-stream");
        bais.close();
        System.out.println("my-objectname is uploaded successfully");
    }

    @Test
    public void download() throws IOException, InvalidKeyException, NoSuchAlgorithmException, InsufficientDataException, InternalException, NoResponseException, InvalidBucketNameException, XmlPullParserException, ErrorResponseException, InvalidArgumentException, InvalidResponseException {
        // Check whether the object exists using statObject().  If the object is not found,
        // statObject() throws an exception.  It means that the object exists when statObject()
        // execution is successful.
        minioClient.statObject("my-bucketname", "my-objectname");

        // Get input stream to have content of ‘my-objectname‘ from ‘my-bucketname‘
        InputStream stream = minioClient.getObject("my-bucketname", "my-objectname");

        // Close the input stream.
        stream.close();
    }
}

 

Minio sdk 测试类

标签:sid   ict   conflict   NPU   long   quartz   point   mes   secret   

原文地址:https://www.cnblogs.com/xiaojf/p/12801174.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!