Minio sdk 测试类
Posted xiaojf
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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(); } }
以上是关于Minio sdk 测试类的主要内容,如果未能解决你的问题,请参考以下文章
使用 S3 Java SDK 与 S3 兼容存储通信 (minio)
java 使用AWS Java SDK构建本地Minio S3客户端