检查存储桶是否存在。方法声明如下:
public boolean bucketExists(String bucketName)
其中:
bucketName:存储桶名称
创建一个新的存储桶,方法声明如下:
public void makeBucket(String bucketName) public void makeBucket(String bucketName, String region) public void makeBucket(String bucketName, String region, boolean objectLock)
其中:
bucketName:存储桶名称
region:存储桶所属区域
objectLock:是否启动对象锁,true-启用;false-不启用
先使用 bucketExists() 方法判断存储桶是否存在,如果存储桶不存在,则使用 makeBucket() 方法创建存储桶。代码如下:
package com.hxstrive.minio.demos.bucket; import io.minio.MinioClient; import io.minio.errors.MinioException; /** * 创建存储桶 * @author Administrator 2021/7/20 13:16 * @version 1.0 */ public class MakeBucket { /** MinioClient.makeBucket() example. */ public static void main(String[] args) throws Exception { try { MinioClient minioClient = new MinioClient( "http://127.0.0.1:9000", "root", "mypassword"); // 如果不存在,则创建存储桶 “my-bucketname” if (!minioClient.bucketExists("my-bucketname")) { minioClient.makeBucket("my-bucketname"); System.out.println("my-bucketname is created successfully"); } // 如果不存在,则在 “eu-west-1” 区域中创建存储桶 “my-bucketname-in-eu” if (!minioClient.bucketExists("my-bucketname-in-eu")) { minioClient.makeBucket("my-bucketname-in-eu", "eu-west-1"); System.out.println("my-bucketname-in-eu is created successfully"); } // 在启用对象锁定功能的 “eu-west-1” 中创建存储桶 “my-bucketname-in-eu-with-object-lock”。 if (!minioClient.bucketExists("my-bucketname-in-eu-with-object-lock")) { minioClient.makeBucket("my-bucketname-in-eu-with-object-lock", "eu-west-1", true); System.out.println("my-bucketname-in-eu-with-object-lock is created successfully"); } } catch (MinioException e) { e.printStackTrace(); } } }
列出 MinIO 中所有存储桶。方法声明如下:
public List<Bucket> listBuckets()
实例:在控制台输出当前 MinIO 服务中所有的存储桶信息,代码如下:
package com.hxstrive.minio.demos.bucket; import io.minio.MinioClient; import io.minio.errors.MinioException; import io.minio.messages.Bucket; import java.util.List; /** * 获取 MinIO 中所有的存储桶 * @author Administrator 2021/7/16 13:50 * @version 1.0 */ public class ListBuckets { /** MinioClient.listBuckets() example. */ public static void main(String[] args) throws Exception { try { MinioClient minioClient = new MinioClient( "http://127.0.0.1:9000", "root", "mypassword"); // 列出我们至少具有读取权限的存储桶 List<Bucket> bucketList = minioClient.listBuckets(); for (Bucket bucket : bucketList) { System.out.println(bucket.creationDate() + ", " + bucket.name()); } } catch (MinioException e) { e.printStackTrace(); } } }
删除一个存储桶。注意,如果存储桶中存在对象(即存储桶不是一个空的存储桶)removeBucket 删除会失败,你需要通过 removeObject 来删除存储桶下面所有的对象后,才能正常删除存储桶。方法声明如下:
public void removeBucket(String bucketName)
实例:判断 my-bucketname 存储桶是否存在,如果存储桶存在,则删除该存储同。代码如下:
package com.hxstrive.minio.demos.bucket; import io.minio.MinioClient; import io.minio.errors.MinioException; /** * 删除存储桶 * @author Administrator 2021/7/20 13:21 * @version 1.0 */ public class RemoveBucket { /** MinioClient.removeBucket() example. */ public static void main(String[] args) throws Exception { try { MinioClient minioClient = new MinioClient( "http://127.0.0.1:9000", "root", "mypassword"); // 删除存储桶 “my-bucketname”(如果存在) // 此操作仅在您的存储桶为空时才有效 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"); } } catch (MinioException e) { e.printStackTrace(); } } }
列出某个存储桶中的所有对象。方法声明如下:
public Iterable<Result<Item>> listObjects(String bucketName) public Iterable<Result<Item>> listObjects(String bucketName, String prefix) public Iterable<Result<Item>> listObjects(String bucketName, String prefix, boolean recursive) public Iterable<Result<Item>> listObjects(String bucketName, String prefix, boolean recursive, boolean useVersion1) public Iterable<Result<Item>> listObjects(String bucketName, String prefix, boolean recursive, boolean includeUserMetadata, boolean useVersion1)
其中:
bucketName:存储桶名称
prefix:对象名称的前缀
recursive:是否递归查找;如果是false,就模拟文件夹结构查找
useVersion1:如果是 true,使用版本1 REST API
实例:列出 images 存储桶中的对象,代码如下:
package com.hxstrive.minio.demos.bucket; import io.minio.MinioClient; import io.minio.Result; import io.minio.errors.MinioException; import io.minio.messages.Item; /** * 获取指定存储桶中所有的对象 * @author Administrator 2021/7/16 13:54 * @version 1.0 */ public class ListObjects { public static void main(String[] args) throws Exception { try { MinioClient minioClient = new MinioClient( "http://127.0.0.1:9000", "root", "mypassword"); { // 列出 images 存储桶中的对象信息 Iterable<Result<Item>> results = minioClient.listObjects("images"); for (Result<Item> result : results) { Item item = result.get(); System.out.println(item.lastModified() + "\t" + item.size() + "\t" + item.objectName()); } } System.out.println("============ 分割线 ============"); { // 递归的列出 images 存储桶中对象信息 Iterable<Result<Item>> results = minioClient.listObjects("images", "300x300", true); for (Result<Item> result : results) { Item item = result.get(); System.out.println(item.lastModified() + "\t" + item.size() + "\t" + item.objectName()); } } } catch (MinioException e) { e.printStackTrace(); } } }
列出存储桶中被部分上传的对象(部分上传即不完整对象)。方法声明如下:
public Iterable<Result<Upload>> listIncompleteUploads(String bucketName) public Iterable<Result<Upload>> listIncompleteUploads(String bucketName, String prefix) public Iterable<Result<Upload>> listIncompleteUploads(String bucketName, String prefix, boolean recursive)
其中:
bucketName:存储桶名称
prefix:对象名称的前缀,列出有该前缀的对象
recursive:是否递归查找;如果是 false,就模拟文件夹结构查找
实例:列出存储桶 my-bucketname 中不完整的对象,代码如下:
package com.hxstrive.minio.demos.bucket; import io.minio.MinioClient; import io.minio.Result; import io.minio.errors.MinioException; import io.minio.messages.Upload; import java.util.Iterator; /** * 列出存储桶中不完整对象 * @author Administrator 2021/7/20 13:21 * @version 1.0 */ public class ListIncompleteUploadsBucket { /** MinioClient.bucketExists() example. */ public static void main(String[] args) throws Exception { try { MinioClient minioClient = new MinioClient( "http://127.0.0.1:9000", "root", "mypassword"); boolean found = minioClient.bucketExists("my-bucketname"); if (found) { Iterable<Result<Upload>> iterable = minioClient.listIncompleteUploads("my-bucketname"); Iterator<Result<Upload>> iterator = iterable.iterator(); while(iterator.hasNext()) { Result<Upload> result = iterator.next(); System.out.println(result.get().objectName()); } } else { System.out.println("my-bucketname does not exist"); } } catch (MinioException e) { e.printStackTrace(); } } }