一、下载MinIo的地址
https://min.io/download#/windows
二、如何进行启动
三、查看页面的相关的一些详情
四、SpringBoot集成minio
io.miniominio8.3.4
io.miniominio8.3.4com.squareup.okhttp3okhttpcom.squareup.okhttp3okhttp4.9.3
15:07:14.933 [mAIn] ERROR org.springframework.boot.diagnostics.LoggingFailureAnalysisReporter - *************************** AppLICATION FAILED TO START *************************** Description: An attempt was made to call a method that does not exist. The attempt was made from the following location: okio.Segment.writeTo(Segment.kt:169) The following method did not exist: kotlin.collections.ArraysKt.copyInto([B[BIII)[B
引入依赖
io.miniominio8.3.4com.squareup.okhttp3okhttpcom.squareup.okhttp3okhttp4.9.1org.jetbrains.kotlinkotlin-stdlib1.3.50
Resolved [io.minio.errors.InvalidResponseException: Non-XML response from server. Response code: 400, Content-Type: text/xml; charset=utf-8, body:InvalidArgument
S3 API Requests must be made to API port.0]
五、bucket的命名规则
六、代码实现
import io.minio.*;import io.minio.http.Method;import io.minio.messages.Bucket;import io.minio.messages.DeleteError;import io.minio.messages.DeleteObject;import io.minio.messages.Item;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.ResponseBody;import JAVA.io.File;import java.io.FileInputStream;import java.util.LinkedList;import java.util.List;import java.util.concurrent.TimeUnit;@Controllerpublic class TestMinioController {@ResponseBody@RequestMapping("testUpdate")public void testUpdate() throws Exception {MinioUtils.uploadObject("C:\Users\Administrator\Pictures\3.JPG");MinioUtils.bucketOperate();MinioUtils.getPresignedObjectUrl("3.JPG");MinioUtils.stateObject("3.JPG");MinioUtils.downloadObject("3.JPG","C:\Users\Administrator\Pictures\31.JPG");MinioUtils.removeObject("3.JPG");class MinioUtils {private static final String BUCKET_NAME = "test-minio-bucket";private static final String ENDPOINT = "http://127.0.0.1:9000";private static final String ACCESS_KEY = "private_admin";private static final String SECRET_KEY = "private_admin";static MinioClient minioClient = null;static {minioClient = MinioClient.builder().endpoint(ENDPOINT).credentials(ACCESS_KEY, SECRET_KEY).build();* 桶的操作public static void bucketOperate() throws Exception {// 判断桶是否存在,true 表示存在,false表示不存在boolean found = minioClient.bucketexists(BucketExistsArgs.builder().bucket(BUCKET_NAME).build());if (found) {System.out.println(BUCKET_NAME + " exists");} else {System.out.println(BUCKET_NAME + " does not exist");// 创建桶minioClient.makeBucket(MakeBucketArgs.builder().bucket(BUCKET_NAME).build());// 查看桶的列表List buckets = minioClient.listBuckets();buckets.stream().forEach(bucket -> System.out.println(bucket.name()));// 查询某个桶的所有信息Iterable> results = minioClient.listObjects(ListObjectsArgs.builder().bucket(BUCKET_NAME).build());results.forEach(itemResult -> {try {System.out.println(itemResult.get().objectName());} catch (Exception e) {e.printStackTrace();* 桶中的对象操作——上传文件* 桶内的对象名称不能重复* @throws Exceptionpublic static void uploadObject(String filePath) throws Exception {File file = new File(filePath);// 通过流上传FileInputStream fileInputStream = new FileInputStream(file);// objectSize 是对象的大小,// 如果对象大小未知,则将 -1 传递给 objectSize 并传递有效的 partSize,例如:10485760// 如果对象大小已知,则将 -1 传递给 partSize 以进行自动检测;// 上传未知文件大小PutObjectArgs putObjectArgs = PutObjectArgs.builder().bucket(BUCKET_NAME) // 桶名.object(file.getName()) // 上传到桶的对象名称.stream(fileInputStream, -1, xxxx).build();// minioClient.putObject(putObjectArgs);// 上传已知文件大小PutObjectArgs putObjectArgs2 = PutObjectArgs.builder().bucket(BUCKET_NAME) // 桶名.object(file.getName()) // 上传到桶的对象名称.stream(fileInputStream, file.length(), -1).build();// minioClient.putObject(putObjectArgs2);// 通过内容UploadObjectArgs uploadObjectArgs = UploadObjectArgs.builder().bucket(BUCKET_NAME).object(file.getName()) // 上传到桶的对象名称.filename(filePath) // 文件全路径.build();minioClient.uploadObject(uploadObjectArgs);* 桶中的对象操作——移除文件* @throws Exceptionpublic static void removeObject(String objName) throws Exception {// 删除单个对象RemoveObjectArgs removeObjectArgs = RemoveObjectArgs.builder().bucket(BUCKET_NAME).object(objName).build();minioClient.removeObject(removeObjectArgs);* 溢出多个对象的操作* @throws Exceptionpublic static void removeObjectMany() throws Exception {// 删除多个对象List objects = new LinkedList<>();objects.add(new DeleteObject("3.JPG"));RemoveObjectsArgs removeObjectsArgs = RemoveObjectsArgs.builder().bucket(BUCKET_NAME).objects(objects).build();Iterable> results = minioClient.removeObjects(removeObjectsArgs);for (Result result : results) {DeleteError error = result.get();System.out.println("Error in deleting object " + error.objectName() + "; " + error.message());* 获取下载链接* @throws Exceptionpublic static void getPresignedObjectUrl(String downObj) throws Exception {GetPresignedObjectUrlArgs getPresignedObjectUrlArgs = GetPresignedObjectUrlArgs.builder().method(Method.GET).bucket(BUCKET_NAME).object(downObj).expiry(2, TimeUnit.MINUTES) // 设置链接过期时间,不设置的话是永久链接.build();String url = minioClient.getPresignedObjectUrl(getPresignedObjectUrlArgs);System.out.println(url);* 下载* @throws Exceptionpublic static void downloadObject(String objName,String fileName) throws Exception {DownloadObjectArgs downloadObjectArgs = DownloadObjectArgs.builder().bucket(BUCKET_NAME).object(objName).filename(fileName) // 设置下载文件的路径.build();minioClient.downloadObject(downloadObjectArgs);* 获取对象的元数据信息* @throws Exceptionpublic static void stateObject(String objName) throws Exception {StatObjectArgs statObjectArgs = StatObjectArgs.builder().bucket(BUCKET_NAME).object(objName).build();StatObjectResponse statObjectResponse = minioClient.statObject(statObjectArgs);System.out.println(statObjectResponse);
输出结果
test-bucket existstest-bucket3.JPGhttp://127.0.0.1:9000/test-bucket/3.JPG?X-Amz-Algorithm=AWS4-Hmac-SHA256&X-Amz-Credential=private_admin%2F20220925%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20220925T082118Z&X-Amz-Expires=120&X-Amz-SignedHeaders=host&X-Amz-Signature=26e16e01229a71f96d5cd12944870696baf9dcd95642ec303b7bfcae4842f639ObjectStat{bucket=test-bucket, object=3.JPG, last-modified=2022-09-25T08:21:17Z, size=19856}