一、下載MinIo的地址
https://min.io/download#/windows
二、如何進行啟動
- 絕對路徑minio.exe server F:Data --console-address ":9001"
- 訪問頁面http://192.168.1.100:9001/
- 輸入用戶名:minioadmin 輸入密碼:minioadmin
三、查看頁面的相關的一些詳情
四、SpringBoot集成minio
- 引入依賴包,如果單純引入minio的,會出現以下的報錯現象
io.miniominio8.3.4
- 解決方案,導入大于4.8.1的okhttp
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的命名規則
- bucket名稱的長度必須介于 3(最小)和 63(最大)字符之間。
- bucket名稱只能由小寫字母、數字、點 (.) 和連字符 (-) 組成。
- bucket名稱不得包含兩個相鄰的句點,或與連字符相鄰的句點。
- bucket名稱不得格式化為 IP 地址(例如,192.168.5.4)。
- bucket名稱不能以前綴 xn-- 開頭。
- bucket名稱不得以后綴 -s3alias 結尾。該后綴是為接入點別名保留的。
- 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}