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();
        }
    }
  • 相关阅读:
    Treap 树堆 容易实现的平衡树
    (转)Maven实战(二)构建简单Maven项目
    (转)Maven实战(一)安装与配置
    根据请求头跳转判断Android&iOS
    (转)苹果消息推送服务器 php 证书生成
    (转)How to renew your Apple Push Notification Push SSL Certificate
    (转)How to build an Apple Push Notification provider server (tutorial)
    (转)pem, cer, p12 and the pains of iOS Push Notifications encryption
    (转)Apple Push Notification Services in iOS 6 Tutorial: Part 2/2
    (转)Apple Push Notification Services in iOS 6 Tutorial: Part 1/2
  • 原文地址:https://www.cnblogs.com/xiaojf/p/12801174.html
Copyright © 2011-2022 走看看