檔案上傳微服務
阿新 • • 發佈:2021-07-28
首先建立一個配置檔案用於儲存mini服務端配置資訊
min.io.endpoint =
min.io.accessKey =
min.io.secretKey =
min.io.bucket =
之後建立一個資源類讀取配置檔案
@PropertySource
用於尋找要載入properties檔案目錄
@ConfigurationProperties
尋找propeties中自動新增字首 之後只要在類中新增properties檔案中去除字首的key即可 然後新增get set方法
package com.lyra.utils.extend; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.PropertySource; import org.springframework.stereotype.Component; @Component @PropertySource("classpath:minio.properties") @ConfigurationProperties(prefix = "min.io") public class MinioResources { private String endpoint; private String accessKey; private String secretKey; private String bucket; public String getBucket() { return bucket; } public void setBucket(String bucket) { this.bucket = bucket; } public String getEndpoint() { return endpoint; } public void setEndpoint(String endpoint) { this.endpoint = endpoint; } public String getAccessKey() { return accessKey; } public void setAccessKey(String accessKey) { this.accessKey = accessKey; } public String getSecretKey() { return secretKey; } public void setSecretKey(String secretKey) { this.secretKey = secretKey; } }
之後就可以寫工具類了
將配置資源類注入
sdk常用命令
建立一個客戶端用於操作minio
MinioClient minioClient =
MinioClient.builder()
.endpoint("https://play.min.io")
.credentials("Q3AM3UQ867SPQQA43P2F", "zuf+tfteSlswRu7BJ86wekitnifILbZam1KYY3TG")
.build();
檔案上傳 傳入bucket名稱和上傳的檔名稱和檔案流即可完成上傳
如果要傳入的是目錄 可以在目錄新增 \ 在本業務中我們根據的使用者id和傳入的檔名稱來設定上傳檔案的目錄 以使用者的id為目錄 目錄下儲存的使用者上傳的檔案
minioClient.putObject(PutObjectArgs.builder()
.bucket(minioResources.getBucket())
.object(folderAndFileName)
.stream(fileSteam, size, -1).build());
工具類的思想: 首先建立一個client用於操作minio
之後執行檔案上傳
上傳完畢後根據檔名稱 host bucket進行拼接 返回上傳檔案的url
package com.lyra.utils; import com.lyra.exception.GraceException; import com.lyra.result.ResponseStatusEnum; import com.lyra.utils.extend.MinioResources; import io.minio.*; import io.minio.http.Method; import org.apache.commons.lang3.StringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Bean; import org.springframework.stereotype.Component; import java.io.FileInputStream; import java.io.InputStream; @Component public class MinioUtils { @Autowired private MinioResources minioResources; public String uploadFile(InputStream fileSteam, String folderAndFileName, Long size) { try { MinioClient minioClient = MinioClient.builder() .endpoint(minioResources.getEndpoint()) .credentials(minioResources.getAccessKey(), minioResources.getSecretKey()).build(); // 上傳檔案 minioClient.putObject(PutObjectArgs.builder() .bucket(minioResources.getBucket()) .object(folderAndFileName) .stream(fileSteam, size, -1).build()); return minioResources.getEndpoint() + minioResources.getBucket() + "/" + folderAndFileName; } catch (Exception e) { GraceException.display(ResponseStatusEnum.UPLOAD_FAILED); e.printStackTrace(); } GraceException.display(ResponseStatusEnum.GET_FILE_IMAGE_URL_FAILED); return null; } }
建立檔案微服務
匯入依賴 因為要建立controller所以要匯入api 因為要使用minio utils和統一返回需要common
<dependencies>
<dependency>
<groupId>com.lyra</groupId>
<artifactId>news-common</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.lyra</groupId>
<artifactId>news-api</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>io.minio</groupId>
<artifactId>minio</artifactId>
</dependency>
</dependencies>
編寫啟動類 啟動類要將所有路徑都掃描到才可以使用swagger
@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)
@ComponentScan(value = {"com.lyra"})
public class FileServiceApplication {
public static void main(String[] args) {
SpringApplication.run(FileServiceApplication.class, args);
}
}
上傳的大小限制為500KB
maxFileSize
是單個檔案大小
maxRequestSize
是設定總上傳的資料大小
server:
port: 8004
spring:
redis:
host: 127.0.0.1
port: 6379
password: 990728
database: 0
application:
name: service-file
servlet:
multipart:
max-file-size: 512000
max-request-size: 512000