欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

springboot整合minio實(shí)現(xiàn)文件上傳與下載且支持鏈接永久訪問

 更新時間:2022年01月27日 10:45:16   作者:句號管  
本文主要介紹了springboot整合minio實(shí)現(xiàn)文件上傳與下載且支持鏈接永久訪問,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下

1、minio部署

1.1 拉取鏡像

docker pull minio/minio

1.2 創(chuàng)建數(shù)據(jù)目錄

mkdir -p /home/guanz/minio
mkdir -p /home/guanz/minio/midata

1.3 啟動minio

docker run -d -p 9000:9000 -p 9001:9001 --restart=always -e MINIO_ACCESS_KEY=guanz -e MINIO_SECRET_KEY=guanz@123 -v $PWD/midata:/data  minio/minio server /data --console-address "192.168.1.139:9001"

2、項(xiàng)目搭建

2.1 引入jar

        <dependency>
            <groupId>io.minio</groupId>
            <artifactId>minio</artifactId>
            <version>8.0.3</version>
        </dependency>

2.2 application-dev.yml

spring
    minio:
    # Minio服務(wù)器地址
    endpoint: http://192.168.1.139:9000
    port: 9001
    create-bucket: true
    bucketName: push-test
    # Minio服務(wù)器賬號
    accessKey: guanz
    # Minio服務(wù)器密碼
    secretKey: guanz@123
    secure: false
    configDir: /home/push
    # 文件大小 單位M
    maxFileSize: 10
    expires: 604800

2.4 MinioConfig.java

package com.pavis.app.saasbacken.config;

import io.minio.MinioClient;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

/**
?* @program: push-saas
?* @description:
?* @author: Guanzi
?* @created: 2021/11/02 13:47
?*/
@Data
@Component
@ConfigurationProperties(prefix = "minio")
@Slf4j
@Configuration
public class MinioConfig {
? ? @ApiModelProperty("endPoint是一個URL,域名,IPv4或者IPv6地址")
? ? @Value("${spring.minio.endpoint}")
? ? private String endpoint;

? ? @ApiModelProperty("TCP/IP端口號")
? ? @Value("${spring.minio.port}")
? ? private int port;

? ? @ApiModelProperty("accessKey類似于用戶ID,用于唯一標(biāo)識你的賬戶")
? ? @Value("${spring.minio.accessKey}")
? ? private String accessKey;

? ? @ApiModelProperty("secretKey是你賬戶的密碼")
? ? @Value("${spring.minio.secretKey}")
? ? private String secretKey;

? ? @ApiModelProperty("如果是true,則用的是https而不是http,默認(rèn)值是true")
? ? @Value("${spring.minio.secure}")
? ? private Boolean secure;

? ? @ApiModelProperty("默認(rèn)存儲桶")
? ? @Value("${spring.minio.bucketName}")
? ? private String bucketName;

? ? @ApiModelProperty("配置目錄")
? ? @Value("${spring.minio.configDir}")
? ? private String configDir;

? ? @ApiModelProperty("文件大小")
? ? @Value("${spring.minio.maxFileSize}")
? ? private Integer maxFileSize;

? ? @ApiModelProperty("簽名有效時間")
? ? @Value("${spring.minio.expires}")
? ? private Integer expires;

? ? /**
? ? ?* 注入minio 客戶端
? ? ?* @return
? ? ?*/
? ? @Bean
? ? public MinioClient minioClient(){
? ? ? ? log.info("endpoint:{},port:{},accessKey:{},secretKey:{},secure:{}",endpoint, port, accessKey, secretKey,secure);
? ? ? ? return MinioClient.builder()
? ? ? ? ? ? ? ? .endpoint(endpoint)
? ? ? ? ? ? ? ? .credentials(accessKey, secretKey)
? ? ? ? ? ? ? ? .build();
? ? }
}

3、文件上傳

3.1 關(guān)鍵代碼

MinioController.java

/**
? ? ?* 文件上傳
? ? ?* @param file
? ? ?* @return
? ? ?*/
? ? @PostMapping("/upload")
? ? public Map<String, Object> upload(MultipartFile file){
? ? ? ? return minioService.upload(file);
? ? }

MinioServiceImpl.java

@Override
? ? public Map<String, Object> upload(MultipartFile file) {
? ? ? ? Map<String, Object> res = new HashMap<>();
? ? ? ? try {
? ? ? ? ? ? BucketExistsArgs bucketArgs = BucketExistsArgs.builder().bucket(bucketName).build();
? ? ? ? ? ? // todo 檢查bucket是否存在。
? ? ? ? ? ? boolean found = minioClient.bucketExists(bucketArgs);

? ? ? ? ? ? PutObjectArgs objectArgs = PutObjectArgs.builder().object(file.getOriginalFilename())
? ? ? ? ? ? ? ? ? ? .bucket(bucketName)
? ? ? ? ? ? ? ? ? ? .contentType(file.getContentType())
? ? ? ? ? ? ? ? ? ? .stream(file.getInputStream(), file.getSize(), -1).build();

? ? ? ? ? ? ObjectWriteResponse objectWriteResponse = minioClient.putObject(objectArgs);
? ? ? ? ? ? System.out.println(objectWriteResponse.etag());
? ? ? ? ? ? res.put("code", "1");
? ? ? ? ? ? res.put("mess", "ok");
? ? ? ? ? ? return res;
? ? ? ? } catch (Exception e) {
? ? ? ? ? ? e.printStackTrace();
? ? ? ? ? ? log.info(e.getMessage());
? ? ? ? }
? ? ? ? res.put("code", "0");
? ? ? ? return res;
? ? }

4、文件下載

@Override
? ? public void download(String filename, HttpServletResponse res) throws IOException, InvalidKeyException, InvalidResponseException, InsufficientDataException, NoSuchAlgorithmException, ServerException, InternalException, XmlParserException, ErrorResponseException {
? ? ? ? BucketExistsArgs bucketArgs = BucketExistsArgs.builder().bucket(bucketName).build();
? ? ? ? boolean bucketExists = minioClient.bucketExists(bucketArgs);
? ? ? ? log.info("bucketExists:{}", bucketExists);

? ? ? ? GetObjectArgs objectArgs = GetObjectArgs.builder().bucket(bucketName)
? ? ? ? ? ? ? ? .object(filename).build();
? ? ? ? System.err.println("objectArgs:" + JSON.toJSONString(objectArgs));
? ? ? ? try (GetObjectResponse response = minioClient.getObject(objectArgs)) {
? ? ? ? ? ? System.err.println("response:" + JSON.toJSONString(response));
? ? ? ? ? ? byte[] buf = new byte[1024];
? ? ? ? ? ? int len;
? ? ? ? ? ? try (FastByteArrayOutputStream os = new FastByteArrayOutputStream()) {
? ? ? ? ? ? ? ? while ((len = response.read(buf)) != -1) {
? ? ? ? ? ? ? ? ? ? os.write(buf, 0, len);
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? os.flush();
? ? ? ? ? ? ? ? byte[] bytes = os.toByteArray();

? ? ? ? ? ? ? ? res.setCharacterEncoding("utf-8");
? ? ? ? ? ? ? ? res.setContentType("application/force-download");// 設(shè)置強(qiáng)制下載不打開
? ? ? ? ? ? ? ? res.addHeader("Content-Disposition", "attachment;fileName=" + filename);
? ? ? ? ? ? ? ? try (ServletOutputStream stream = res.getOutputStream()) {
? ? ? ? ? ? ? ? ? ? stream.write(bytes);
? ? ? ? ? ? ? ? ? ? stream.flush();
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }

? ? ? ? } catch (Exception e) {
? ? ? ? ? ? e.printStackTrace();
? ? ? ? }
}

下載地址:

5、文件永久鏈接下載

5.1 配置

5.2 關(guān)鍵代碼

/**
? ? * 生成一個GET請求的分享鏈接。
? ? * 失效時間默認(rèn)是7天。
? ? *
? ? * @param bucketName 存儲桶名稱
? ? * @param objectName 存儲桶里的對象名稱
? ? * @param expires ? ?失效時間(以秒為單位),默認(rèn)是7天,不得大于七天
? ? * @return
? ? */
? ?public String presignedGetObject(String bucketName, String objectName, Integer expires) throws IOException, InvalidKeyException, InvalidResponseException, InsufficientDataException, NoSuchAlgorithmException, ServerException, InternalException, XmlParserException, ErrorResponseException {
? ? ? ?BucketExistsArgs bucketArgs = BucketExistsArgs.builder().bucket(bucketName).build();
? ? ? ?boolean bucketExists = minioClient.bucketExists(bucketArgs);
? ? ? ?// boolean flag = bucketExists(bucketName);
? ? ? ?String url = "";
? ? ? ?if (bucketExists) {
? ? ? ? ? ?try {
? ? ? ? ? ? ? ?if (expires == null){
? ? ? ? ? ? ? ? ? ?expires = 604800;
? ? ? ? ? ? ? ?}

? ? ? ? ? ? ? ?GetPresignedObjectUrlArgs getPresignedObjectUrlArgs = GetPresignedObjectUrlArgs.builder()
? ? ? ? ? ? ? ? ? ? ? ?.method(Method.GET)
? ? ? ? ? ? ? ? ? ? ? ?.bucket(bucketName)
? ? ? ? ? ? ? ? ? ? ? ?.object(objectName)
? ? ? ? ? ? ? ? ? ? ? ?// .expiry(expires)
? ? ? ? ? ? ? ? ? ? ? ?.build();
? ? ? ? ? ? ? ?url = minioClient.getPresignedObjectUrl(getPresignedObjectUrlArgs);
? ? ? ? ? ? ? ?log.info("*******url2:{}",url);
? ? ? ? ? ?} catch (Exception e) {
? ? ? ? ? ? ? ?log.info("presigned get object fail:{}",e);
? ? ? ? ? ?}
? ? ? ?}
? ? ? ?return url;
? ?}

下載地址:http://192.168.1.139:9000/push-test/qiyeku.jpg

至此,springboot+minio 結(jié)束。

到此這篇關(guān)于springboot整合minio實(shí)現(xiàn)文件上傳與下載且支持鏈接永久訪問的文章就介紹到這了,更多相關(guān)springboot minio 文件上傳下載 內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 學(xué)習(xí)Java中Class類及其用法

    學(xué)習(xí)Java中Class類及其用法

    這篇文章分為三大部分,主要內(nèi)容有Class類原理詳解、用法及泛化,需要的朋友可以參考下
    2015-07-07
  • Java獲取文件的hash值(SHA256)兩種方式

    Java獲取文件的hash值(SHA256)兩種方式

    這篇文章主要給大家介紹了關(guān)于Java獲取文件hash值(SHA256)的兩種方式,SHA256是一種哈希算法,它是不可逆的,也就是說無法解密,需要的朋友可以參考下
    2023-09-09
  • springboot使用事物注解方式代碼實(shí)例

    springboot使用事物注解方式代碼實(shí)例

    這篇文章主要介紹了springboot使用事物注解方式代碼實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2019-11-11
  • Java多線程解決龜兔賽跑問題詳解

    Java多線程解決龜兔賽跑問題詳解

    這篇文章主要為大家詳細(xì)介紹了Java如何使用線程休眠模擬龜兔賽跑比賽問題,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下
    2022-08-08
  • java開發(fā)分布式服務(wù)框架Dubbo服務(wù)引用過程詳解

    java開發(fā)分布式服務(wù)框架Dubbo服務(wù)引用過程詳解

    這篇文章主要為大家介紹了java開發(fā)分布式服務(wù)框架Dubbo服務(wù)引用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步
    2021-11-11
  • SpringBoot集成mybatis實(shí)例

    SpringBoot集成mybatis實(shí)例

    本篇文章主要介紹了SpringBoot集成mybatis實(shí)例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-04-04
  • SpringBoot和Tomcat的關(guān)系解讀

    SpringBoot和Tomcat的關(guān)系解讀

    這篇文章主要介紹了SpringBoot和Tomcat的關(guān)系,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2025-03-03
  • Java中ArrayIndexOutOfBoundsException 異常報錯的解決方案

    Java中ArrayIndexOutOfBoundsException 異常報錯的解決方案

    本文主要介紹了Java中ArrayIndexOutOfBoundsException 異常報錯的解決方案,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-06-06
  • 2023年IDEA最新永久激活教程(親測可用)

    2023年IDEA最新永久激活教程(親測可用)

    打開電腦,發(fā)現(xiàn)?IDEA?又更新了一個小版本,2022.3.2?版本來了,真的是非常高興,那么這么新的版本怎么激活使用呢?下面小編給大家?guī)砹薸dea2023年最新永久激活方法,感興趣的朋友一起看看吧
    2023-04-04
  • java數(shù)據(jù)結(jié)構(gòu)與算法之noDups去除重復(fù)項(xiàng)算法示例

    java數(shù)據(jù)結(jié)構(gòu)與算法之noDups去除重復(fù)項(xiàng)算法示例

    這篇文章主要介紹了java數(shù)據(jù)結(jié)構(gòu)與算法之noDups去除重復(fù)項(xiàng)算法實(shí)現(xiàn)技巧,程序代碼非常簡單,關(guān)鍵在于循環(huán)與判定,需要的朋友可以參考下
    2016-08-08

最新評論