上篇文章中使用了MinIO中文官网的Java API示例进行了验证,版本与当前稳定版本相比过旧,在英文官网发现了最新的API使用方式,在此文中进行验证。
目录
旧版验证详细
- 旧版验证详细
- 升至7.1.4版本的问题
- 使用示例
-
- Pom文件
- 代码示例
- 执行与结果确认
- 总结
MinIO Java Client SDK(3.0.10)的使用示例可参看如下内容:
- https://liumiaocn.blog.csdn.net/article/details/109460948
Maven使用如下版本之后,上述代码编译无法通过
<dependency> <groupId>io.minio <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0 public static void main(String[] args) throws NoSuchAlgorithmException, IOException, InvalidKeyException { try { // 使用MinIO服务的URL,端口,Access key和Secret key创建一个MinioClient对象 MinioClient minioClient =MinioClient.builder() .endpoint("http://localhost:9000") .credentials("liumiao", "password") .build(); // 检查存储桶是否已经存在 boolean isExist = minioClient.bucketExists(BucketExistsArgs.builder().bucket("anotherbucket").build()); if(isExist) { System.out.println("Bucket already exists."); } else { // 创建一个名为newbucket的存储桶。 minioClient.makeBucket(MakeBucketArgs.builder().bucket("anotherbucket").build()); } // 使用putObject上传一个文件到存储桶中。 minioClient.uploadObject(UploadObjectArgs.builder() .bucket("anotherbucket").object("local-file-list.info").filename("/Users/liumiao/local-file-list.info").build()); System.out.println("/Users/liumiao/local-file-list.info is successfully uploaded as local-file-list.info to `anotherbucket` bucket."); } catch(MinioException e) { System.out.println("Error occurred: " + e); } } }执行与结果确认
这里将存储桶的名称改为anotherbucket,执行并确认结果如下
-
执行上述程序
然后从MinIO中就可以看到创建的新存储桶和文件的信息了
这篇文章继续更新了一下最新版本的MinIO中如何使用Java API进行存储桶的创建和文件上传的操作方法。