zoukankan      html  css  js  c++  java
  • Minio sdk 测试类

    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();
        }
    }
  • 相关阅读:
    start pyhton project(2)
    java.lang.ClassFormatError: Truncated class file
    linux 查看计算机信息命令
    VS2010UltimTrialCHS 版注册码
    VS2008打包安装程序,实现覆盖安装设置
    WPF移动不规则渐变色窗体
    C#下移动无边框窗体(直接粘贴可用)
    TCP通信过程中时时监测连接是否已断开
    WIN7下使用DotNetBar,关闭Aero效果,使用Office2007Form皮肤
    【原创】企业级工作流管理系统评价依据或标准
  • 原文地址:https://www.cnblogs.com/xiaojf/p/12801174.html
Copyright © 2011-2022 走看看