3.2.5 SDK
3.2.5.1上传文件
这个代码是官方的,如果看不懂可以看我的:MinIO使用idea上传文件方法_Relievedz的博客-CSDN博客 是简易版的
MinIO提供多个语言版本SDK的支持,下边找到java版本的文档:
地址:https://docs.min.io/docs/java-client-quickstart-guide.html
最低需求Java 1.8或更高版本:
maven依赖如下:
XML <dependency> <groupId>io.minio</groupId> <artifactId>minio</artifactId> <version>8.4.3</version> </dependency> <dependency> <groupId>com.squareup.okhttp3</groupId> <artifactId>okhttp</artifactId> <version>4.8.1</version> </dependency> |
在media-service工程添加此依赖。
参数说明:
需要三个参数才能连接到minio服务。
参数 | 说明 |
Endpoint | 对象存储服务的URL |
Access Key | Access key就像用户ID,可以唯一标识你的账户。 |
Secret Key | Secret key是你账户的密码。 |
示例代码如下:
Java import io.minio.BucketExistsArgs; import io.minio.MakeBucketArgs; import io.minio.MinioClient; import io.minio.UploadObjectArgs; import io.minio.errors.MinioException; import java.io.IOException; import java.security.InvalidKeyException; import java.security.NoSuchAlgorithmException; public class FileUploader { public static void main(String[] args)throws IOException, NoSuchAlgorithmException, InvalidKeyException { try { // Create a minioClient with the MinIO server playground, its access key and secret key. MinioClient minioClient = MinioClient.builder() .endpoint("https://play.min.io") .credentials("Q3AM3UQ867SPQQA43P2F", "zuf+tfteSlswRu7BJ86wekitnifILbZam1KYY3TG") .build(); // Make 'asiatrip' bucket if not exist. boolean found = minioClient.bucketExists(BucketExistsArgs.builder().bucket("asiatrip").build()); if (!found) { // Make a new bucket called 'asiatrip'. minioClient.makeBucket(MakeBucketArgs.builder().bucket("asiatrip").build()); } else { System.out.println("Bucket 'asiatrip' already exists."); } // Upload '/home/user/Photos/asiaphotos.zip' as object name 'asiaphotos-2015.zip' to bucket // 'asiatrip'. minioClient.uploadObject( UploadObjectArgs.builder() .bucket("asiatrip") .object("asiaphotos-2015.zip") .filename("/home/user/Photos/asiaphotos.zip") .build()); System.out.println( "'/home/user/Photos/asiaphotos.zip' is successfully uploaded as " + "object 'asiaphotos-2015.zip' to bucket 'asiatrip'."); } catch (MinioException e) { System.out.println("Error occurred: " + e); System.out.println("HTTP trace: " + e.httpTrace()); } } } |
参考示例在media-service工程中 测试上传文件功能,
首先创建一个用于测试的bucket
![](https://i-blog.csdnimg.cn/blog_migrate/eab28d8611b7c529f298fd8caeca5ae0.png)
点击“Manage”修改bucket的访问权限
![](https://i-blog.csdnimg.cn/blog_migrate/bf7189b76fcfda38750a3894837896e6.png)
选择public权限
![](https://i-blog.csdnimg.cn/blog_migrate/4cf192e8b7b7707ebb0a7362a88b8693.png)
测试代码如下:
Java package com.xuecheng.media;
import io.minio.BucketExistsArgs; import io.minio.MakeBucketArgs; import io.minio.MinioClient; import io.minio.UploadObjectArgs; import io.minio.errors.MinioException;
import java.io.IOException; import java.security.InvalidKeyException; import java.security.NoSuchAlgorithmException;
/** * @description 测试MinIO * @author Mr.M * @date 2022/9/11 21:24 * @version 1.0 */ public class MinIOTest {
static MinioClient minioClient = MinioClient.builder() .endpoint("http://192.168.101.65:9000") .credentials("minioadmin", "minioadmin") .build();
//上传文件 public static void upload()throws IOException, NoSuchAlgorithmException, InvalidKeyException { try { boolean found = minioClient.bucketExists(BucketExistsArgs.builder().bucket("testbucket").build()); //检查testbucket桶是否创建,没有创建自动创建 if (!found) { minioClient.makeBucket(MakeBucketArgs.builder().bucket("testbucket").build()); } else { System.out.println("Bucket 'testbucket' already exists."); } //上传1.mp4 minioClient.uploadObject( UploadObjectArgs.builder() .bucket("testbucket") .object("1.mp4") .filename("D:\\develop\\upload\\1.mp4") .build()); //上传1.avi,上传到avi子目录 minioClient.uploadObject( UploadObjectArgs.builder() .bucket("testbucket") .object("avi/1.avi") .filename("D:\\develop\\upload\\1.avi") .build()); System.out.println("上传成功"); } catch (MinioException e) { System.out.println("Error occurred: " + e); System.out.println("HTTP trace: " + e.httpTrace()); }
} public static void main(String[] args)throws IOException, NoSuchAlgorithmException, InvalidKeyException { upload(); }
} |