Obs文件存储服务

This commit is contained in:
jiang 2024-06-21 13:16:40 +08:00
parent f9b6ffa9cc
commit ea9b998c46
14 changed files with 599 additions and 1 deletions

View File

@ -0,0 +1,103 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.bonus</groupId>
<artifactId>bonus-modules</artifactId>
<version>3.6.4</version>
</parent>
<artifactId>bonus-modules-obs</artifactId>
<description>
bonus-modules-obs存储服务
</description>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<!-- SpringCloud Alibaba Nacos -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<!-- SpringCloud Alibaba Nacos Config -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
<!-- SpringCloud Alibaba Sentinel -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>
<!-- SpringBoot Actuator -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!-- FastDFS -->
<dependency>
<groupId>com.github.tobato</groupId>
<artifactId>fastdfs-client</artifactId>
</dependency>
<!-- Minio -->
<dependency>
<groupId>io.minio</groupId>
<artifactId>minio</artifactId>
<version>${minio.version}</version>
</dependency>
<!-- bonus Api System -->
<dependency>
<groupId>com.bonus</groupId>
<artifactId>bonus-api-system</artifactId>
</dependency>
<!-- bonus Common Swagger -->
<dependency>
<groupId>com.bonus</groupId>
<artifactId>bonus-common-swagger</artifactId>
</dependency>
<!-- huaweicloud obs -->
<dependency>
<groupId>com.huaweicloud</groupId>
<artifactId>esdk-obs-java-bundle</artifactId>
<version>3.23.9</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<finalName>${project.artifactId}</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>

View File

@ -0,0 +1,25 @@
package com.bonus.obs;
import com.bonus.common.swagger.annotation.EnableCustomSwagger2;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
@EnableCustomSwagger2
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class })
public class BonusObsApplication {
public static void main(String[] args)
{
SpringApplication.run(BonusObsApplication.class, args);
System.out.println("(♥◠‿◠)ノ゙ obs存储服务模块启动成功 ლ(´ڡ`ლ)゙ \n" +
" .-------. ____ __ \n" +
" | _ _ \\ \\ \\ / / \n" +
" | ( ' ) | \\ _. / ' \n" +
" |(_ o _) / _( )_ .' \n" +
" | (_,_).' __ ___(_ o _)' \n" +
" | |\\ \\ | || |(_,_)' \n" +
" | | \\ `' /| `-' / \n" +
" | | \\ / \\ / \n" +
" ''-' `'-' `-..-' ");
}
}

View File

@ -0,0 +1,50 @@
package com.bonus.obs.config;
import com.obs.services.ObsClient;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
@ConfigurationProperties(prefix = "obs")
public class ObsConfig {
private String endpoint;
private String ak;
private String sk;
private String bucket;
public String getEndpoint() {
return endpoint;
}
public void setEndpoint(String endpoint) {
this.endpoint = endpoint;
}
public String getAk() {
return ak;
}
public void setAk(String ak) {
this.ak = ak;
}
public String getSk() {
return sk;
}
public void setSk(String sk) {
this.sk = sk;
}
public String getBucket() {
return bucket;
}
public void setBucket(String bucket) {
this.bucket = bucket;
}
}

View File

@ -0,0 +1,90 @@
package com.bonus.obs.controller;
import com.bonus.common.core.domain.R;
import com.bonus.obs.service.ObsService;
import com.bonus.obs.utils.FileUtils;
import com.obs.services.model.DeleteObjectResult;
import com.obs.services.model.ObsObject;
import com.obs.services.model.PutObjectResult;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
@RestController
@RequestMapping("/obs")
public class ObsController {
@Resource
private ObsService service;
/**
* 文件上传
*
* @param param 文件流
* @return 文件信息
*/
@PostMapping("/upload")
public R<PutObjectResult> uploadFile(MultipartFile param) {
return service.uploadFile(param);
}
/**
* 删除文件从OBS
*
* @param objectKey 文件在OBS中的键
*/
@PostMapping("/delete")
public R<DeleteObjectResult> deleteFile(String objectKey) {
return service.deleteFile(objectKey);
}
/**
* 文件下载
*
* @param objectKey obs文件存储地址
*/
@GetMapping("/download")
public void download(HttpServletResponse response, @RequestParam String objectKey) {
R<ObsObject> obsObjectR = service.downloadFile(objectKey);
try {
if (R.isError(obsObjectR)) {
response.setStatus(HttpStatus.BAD_REQUEST.value());
return;
}
if (obsObjectR.getData() == null) {
response.setStatus(HttpStatus.NOT_FOUND.value());
return;
}
InputStream inputStream = obsObjectR.getData().getObjectContent();
String safeFileName = FileUtils.getFileNameFromPath(objectKey);
String encodedFileName = URLEncoder.encode(safeFileName, StandardCharsets.UTF_8.toString());
response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
response.setHeader(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + encodedFileName + "\"");
try (OutputStream outputStream = response.getOutputStream()) {
byte[] buffer = new byte[8192];
int bytesRead;
while ((bytesRead = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, bytesRead);
}
outputStream.flush();
} catch (IOException e) {
response.setStatus(HttpStatus.INTERNAL_SERVER_ERROR.value());
}
} catch (IllegalArgumentException e) {
response.setStatus(HttpStatus.BAD_REQUEST.value());
} catch (Exception e) {
response.setStatus(HttpStatus.INTERNAL_SERVER_ERROR.value());
}
}
}

View File

@ -0,0 +1,42 @@
package com.bonus.obs.domain;
import lombok.Builder;
import lombok.Data;
/**
* OBSObject Storage Service信息类
* 用于封装与OBS对象相关的元数据信息
*/
@Data
@Builder
public class ObsInfo {
/**
* 对象的名称
* 代表OBS对象的唯一标识
*/
private String name;
/**
* 对象在OBS中的存储路径
* 包含桶bucket名称和对象在桶内的相对路径
*/
private String path;
/**
* 对象的大小
* 以字符串形式表示可能包含单位如字节KBMB等
*/
private String length;
/**
* 对象的文件类型
* 用于标识对象的MIME类型例如text/plainimage/jpeg等
*/
private String fileType;
/**
* 对象所属的桶bucket的名称
* 每个桶在OBS中都是唯一的用于存储对象
*/
private String bucketName;
}

View File

@ -0,0 +1,31 @@
package com.bonus.obs.service;
import com.bonus.common.core.domain.R;
import com.obs.services.model.DeleteObjectResult;
import com.obs.services.model.ObsObject;
import com.obs.services.model.PutObjectResult;
import org.springframework.web.multipart.MultipartFile;
public interface ObsService {
/**
* 文件上传
*
* @param param 文件流
* @return 文件信息
*/
R<PutObjectResult> uploadFile(MultipartFile param);
/**
* 删除文件从OBS
*
* @param objectKey 文件在OBS中的键
*/
public R<DeleteObjectResult> deleteFile(String objectKey);
/**
* 下载文件从OBS
*
* @param objectKey 文件在OBS中的键
*/
public R<ObsObject> downloadFile(String objectKey);
}

View File

@ -0,0 +1,58 @@
package com.bonus.obs.service.impl;
import com.bonus.common.core.domain.R;
import com.bonus.obs.service.ObsService;
import com.bonus.obs.utils.FileUtils;
import com.bonus.obs.utils.ObsUtils;
import com.obs.services.model.DeleteObjectResult;
import com.obs.services.model.ObsObject;
import com.obs.services.model.PutObjectResult;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
import java.io.File;
@Service
public class ObsServiceImpl implements ObsService {
@Resource
private ObsUtils obsUtils;
/**
* 文件上传
*
* @param param 文件流
* @return 文件信息
*/
@Override
public R<PutObjectResult> uploadFile(MultipartFile param) {
try {
String objectKey = param.getOriginalFilename();
File file = FileUtils.multipartFileToFile(param);
objectKey = FileUtils.generateObjectName(objectKey);
return obsUtils.uploadFile(objectKey, file);
} catch (Exception e) {
return R.fail(e.getMessage());
}
}
/**
* 删除文件从OBS
*
* @param objectKey 文件在OBS中的键
*/
@Override
public R<DeleteObjectResult> deleteFile(String objectKey) {
return obsUtils.deleteFile(objectKey);
}
/**
* 下载文件从OBS
*
* @param objectKey 文件在OBS中的键
*/
@Override
public R<ObsObject> downloadFile(String objectKey) {
return obsUtils.downloadFile(objectKey);
}
}

View File

@ -1,4 +1,4 @@
package com.bonus.oss.utils;
package com.bonus.obs.utils;
import com.bonus.common.core.utils.StringUtils;
import org.springframework.web.multipart.MultipartFile;

View File

@ -0,0 +1,87 @@
package com.bonus.obs.utils;
import com.bonus.common.core.domain.R;
import com.bonus.obs.config.ObsConfig;
import com.obs.services.ObsClient;
import com.obs.services.model.DeleteObjectResult;
import com.obs.services.model.GetObjectRequest;
import com.obs.services.model.ObsObject;
import com.obs.services.model.PutObjectResult;
import org.springframework.stereotype.Service;
import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
@Service
public class ObsUtils {
@Resource
private ObsConfig obsConfig;
/**
* obs 客户端
*/
private ObsClient obsClient;
/**
* 初始化信息
* 构建OSS客户端实例
*/
@PostConstruct
public void init() {
obsClient = new ObsClient(obsConfig.getAk(), obsConfig.getSk(), obsConfig.getEndpoint());
}
/**
* 上传文件到OBS
*
* @param file 要上传的文件
* @return 上传结果
*/
public R<PutObjectResult> uploadFile(String objectKey, File file) {
return R.ok(obsClient.putObject(obsConfig.getBucket(), objectKey, file));
}
/**
* 下载文件从OBS
*
* @param objectKey 文件在OBS中的键
*/
public R<ObsObject> downloadFile(String objectKey) {
if (!doesObjectExist(objectKey)) {
return R.fail("文件不存在");
}
return R.ok(obsClient.getObject(new GetObjectRequest(obsConfig.getBucket(), objectKey)));
}
/**
* 删除文件从OBS
*
* @param objectKey 文件在OBS中的键
*/
public R<DeleteObjectResult> deleteFile(String objectKey) {
if (!doesObjectExist(objectKey)) {
return R.fail("文件不存在");
}
return R.ok(obsClient.deleteObject(obsConfig.getBucket(), objectKey));
}
/**
* 判断文件是否存在
*
* @param objectKey 文件在OBS中的键
* @return 如果文件存在则返回true否则返回false
*/
public boolean doesObjectExist(String objectKey) {
try {
return obsClient.doesObjectExist(obsConfig.getBucket(), objectKey);
} catch (Exception e) {
return false;
}
}
}

View File

@ -0,0 +1,9 @@
Spring Boot Version: ${spring-boot.version}
Spring Application Name: ${spring.application.name}
_ _
| | | |
| |__ ___ _ __ _ _ ___ ______ ___ | |__ ___
| '_ \ / _ \ | '_ \ | | | | / __| |______| / _ \ | '_ \ / __|
| |_) | | (_) | | | | | | |_| | \__ \ | (_) | | |_) | \__ \
|_.__/ \___/ |_| |_| \__,_| |___/ \___/ |_.__/ |___/

View File

@ -0,0 +1,29 @@
# Tomcat
server:
port: 9205
# Spring
spring:
application:
# 应用名称
name: bonus-obs
profiles:
# 环境配置
active: dev
cloud:
nacos:
username: nacos
password: nacos
discovery:
# 服务注册地址
server-addr: 192.168.0.14:8848
namespace: f1fcd3ea-9460-4597-8acd-0f334527017c
config:
# 配置中心地址
server-addr: 192.168.0.14:8848
namespace: f1fcd3ea-9460-4597-8acd-0f334527017c
# 配置文件格式
file-extension: yml
# 共享配置
shared-configs:
- application-${spring.profiles.active}.${spring.cloud.nacos.config.file-extension}

View File

@ -0,0 +1,74 @@
<?xml version="1.0" encoding="UTF-8"?>
<configuration scan="true" scanPeriod="60 seconds" debug="false">
<!-- 日志存放路径 -->
<property name="log.path" value="logs/bonus-file" />
<!-- 日志输出格式 -->
<property name="log.pattern" value="%d{HH:mm:ss.SSS} [%thread] %-5level %logger{20} - [%method,%line] - %msg%n" />
<!-- 控制台输出 -->
<appender name="console" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>${log.pattern}</pattern>
</encoder>
</appender>
<!-- 系统日志输出 -->
<appender name="file_info" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${log.path}/info.log</file>
<!-- 循环政策:基于时间创建日志文件 -->
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- 日志文件名格式 -->
<fileNamePattern>${log.path}/info.%d{yyyy-MM-dd}.log</fileNamePattern>
<!-- 日志最大的历史 60天 -->
<maxHistory>60</maxHistory>
</rollingPolicy>
<encoder>
<pattern>${log.pattern}</pattern>
</encoder>
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<!-- 过滤的级别 -->
<level>INFO</level>
<!-- 匹配时的操作:接收(记录) -->
<onMatch>ACCEPT</onMatch>
<!-- 不匹配时的操作:拒绝(不记录) -->
<onMismatch>DENY</onMismatch>
</filter>
</appender>
<appender name="file_error" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${log.path}/error.log</file>
<!-- 循环政策:基于时间创建日志文件 -->
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- 日志文件名格式 -->
<fileNamePattern>${log.path}/error.%d{yyyy-MM-dd}.log</fileNamePattern>
<!-- 日志最大的历史 60天 -->
<maxHistory>60</maxHistory>
</rollingPolicy>
<encoder>
<pattern>${log.pattern}</pattern>
</encoder>
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<!-- 过滤的级别 -->
<level>ERROR</level>
<!-- 匹配时的操作:接收(记录) -->
<onMatch>ACCEPT</onMatch>
<!-- 不匹配时的操作:拒绝(不记录) -->
<onMismatch>DENY</onMismatch>
</filter>
</appender>
<!-- 系统模块日志级别控制 -->
<logger name="com.bonus" level="info" />
<!-- Spring日志级别控制 -->
<logger name="org.springframework" level="warn" />
<root level="info">
<appender-ref ref="console" />
</root>
<!--系统操作日志-->
<root level="info">
<appender-ref ref="file_info" />
<appender-ref ref="file_error" />
</root>
</configuration>