生活随笔
收集整理的這篇文章主要介紹了
MinIO安装和基本使用
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
文章目錄
- 1. 簡介
- 2. 安裝(Docker)
- 3. Java API
- 3.1 準(zhǔn)備工作
- 3.2 文件上傳
- 3.3 文件下載
- 4. 設(shè)置永久下載地址
1. 簡介
MinIO是一款高性能的對象存儲容器,適合存儲大量的非結(jié)構(gòu)化數(shù)據(jù),例如:圖片、視頻、Word等文件。此外,它還自帶非常方便的可視化界面,簡直是運(yùn)維的最愛啊。
2. 安裝(Docker)
docker pull minio
/minio
docker run
-it
--name minio
-p 9000:9000
-p 9001:9001
-d \
-v
/usr
/local
/docker
/minio
/data:
/data \
-v
/usr
/local
/docker
/minio
/config:
/root
/.minio \
-e
'MINIO_ROOT_USER=admin' \
-e
'MINIO_ROOT_PASSWORD=admin123' \
minio
/minio server
/data --console
-address
":9001"
3. Java API
3.1 準(zhǔn)備工作
這里以Java語言為例,框架以SpringBoot為例,調(diào)用Minio API進(jìn)行相關(guān)操作
- 首先,在上面的可視化界面點(diǎn)擊左側(cè) Buckets 菜單,創(chuàng)建一個Bucket,命名為 test01
- 然后,進(jìn)入實例代碼
- 使用maven引用minio依賴
<dependency><groupId>io.minio
</groupId><artifactId>minio
</artifactId><version>8.3.0
</version><exclusions><exclusion><groupId>com.squareup.okhttp3
</groupId><artifactId>okhttp
</artifactId></exclusion></exclusions></dependency><dependency><groupId>com.squareup.okhttp3
</groupId><artifactId>okhttp
</artifactId><version>4.8.1
</version></dependency>
- 建一個 MinioUtil 操作類,并實例化 MinioClient 對象
public class MinioUtil {private static MinioClient client
;static {try {client
= MinioClient.builder().endpoint(new URL("http://192.168.25.129:9000")).credentials("admin", "admin123").build();} catch (MalformedURLException e
) {e
.printStackTrace();}}
}
3.2 文件上傳
- 在 MinioUtil 補(bǔ)充2個上傳方法:上傳本地文本、上傳MultipartFile
public static ObjectWriteResponse upload(String bucket
, String localFileName
, String remoteFileName
) throws Exception{File file
= new File(localFileName
);FileInputStream fileInputStream
= new FileInputStream(file
);return client
.putObject(PutObjectArgs.builder().stream(fileInputStream
, file
.length(), PutObjectArgs.MIN_MULTIPART_SIZE
).object(remoteFileName
).bucket(bucket
).build());}public static ObjectWriteResponse upload(String bucket
, MultipartFile file
, String remoteFileName
) throws Exception {return client
.putObject(PutObjectArgs.builder().bucket(bucket
).stream(file
.getInputStream(), file
.getSize(), PutObjectArgs.MIN_MULTIPART_SIZE
).object(remoteFileName
).build());}
- 創(chuàng)建 MinioController ,加2個接口分別測試上傳:
@RestController
@RequestMapping("/minio")
public class MinioController {@GetMapping("/uploadLocalFile")public String uploadLocalFile() throws Exception{ObjectWriteResponse resp
= MinioUtil.upload("test01", "E:\\測試文件.txt", getDatePath()+"test.txt");return "上傳成功";}@PostMapping("/uploadMultipartFile")public String uploadMultipartFile(MultipartFile file
) throws Exception {ObjectWriteResponse resp
= MinioUtil.upload("test01", file
, getDatePath() + file
.getOriginalFilename());return "上傳成功";}private String getDatePath(){LocalDateTime now
= LocalDateTime.now();return String.format("/%s/%s/%s/", now
.getYear(), now
.getMonthValue(), now
.getDayOfMonth());}
}
- 執(zhí)行完畢后,在 test01 這個 bucket 底下,會創(chuàng)建 /2021/09/27/ 的目錄,并且將文件上傳到該目錄下
3.3 文件下載
- 修改 MinioUtil,增加 2 個下載方法,分別是下載到本地和瀏覽器打開下載
public static void downLocal(String bucket
, String remoteFileName
, String localFileName
) throws Exception {client
.downloadObject(DownloadObjectArgs.builder().bucket(bucket
).object(remoteFileName
).filename(localFileName
).build());}public static void downResponse(String bucket
, String remoteFileName
, HttpServletResponse response
) throws Exception {GetObjectResponse object
= client
.getObject(GetObjectArgs.builder().bucket(bucket
).object(remoteFileName
).build());response
.setHeader("Content-Disposition", "attachment;filename=" + remoteFileName
.substring(remoteFileName
.lastIndexOf("/")+1));response
.setContentType("application/force-download");response
.setCharacterEncoding("UTF-8");IOUtils.copy(object
, response
.getOutputStream());}
- 修改 MinioController,增加2個對應(yīng)的接口
@PostMapping("/downLocal")public String downLocal() throws Exception {MinioUtil.downLocal("test01", "/2021/9/27/test.txt", "test.txt");return "下載成功";}@GetMapping("/downResponse")public void downResponse(HttpServletResponse response
) throws Exception {MinioUtil.downResponse("test01", "/2021/9/27/test.txt", response
);}
分別訪問這2個接口就可以測試,這邊就不演示了
4. 設(shè)置永久下載地址
前面講的例子是調(diào)用 Minio API 獲取文件,從而實現(xiàn)下載功能。此外,minio 也允許通過文件地址直接下載。
- 打開 Bucket,點(diǎn)擊 Manage 進(jìn)行設(shè)置管理
- 點(diǎn)擊這里的 Private,把它改成 Public
- 以這個文件為例,下載地址就是 http://192.168.25.129:9000/test01/2021/9/27/test.txt,直接從瀏覽器打開就可以測試。
總結(jié)
以上是生活随笔為你收集整理的MinIO安装和基本使用的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。