阿里oss

This commit is contained in:
ran.shang
2025-06-16 14:53:21 +08:00
parent 06f091967d
commit 0d04167bca
3 changed files with 137 additions and 0 deletions

View File

@@ -220,6 +220,12 @@
<artifactId>fontbox</artifactId>
<version>2.0.20</version>
</dependency>
<!-- oss -->
<dependency>
<groupId>com.aliyun.oss</groupId>
<artifactId>aliyun-sdk-oss</artifactId>
<version>3.14.0</version>
</dependency>
<!-- Spire.Pdf -->
<dependency>

View File

@@ -0,0 +1,123 @@
package com.newyetai.component.file.service.storage;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import org.apache.catalina.connector.ClientAbortException;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import com.aliyun.oss.ClientException;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.OSSException;
import com.aliyun.oss.model.OSSObject;
import com.newyetai.common.constant.CommonConstants;
import com.newyetai.common.exception.BusinessException;
import com.newyetai.common.exception.DevopsException;
import com.newyetai.common.exception.util.Assert;
import com.newyetai.common.util.EnvironmentUtils;
import com.newyetai.component.file.config.FileProperties;
import com.newyetai.component.file.config.FileProperties.AliStorage;
import com.newyetai.component.file.constant.FileStorageEnum;
import com.newyetai.component.file.service.IFileStorage;
import lombok.extern.slf4j.Slf4j;
@Slf4j
public class AliFileStorage implements IFileStorage {
private FileProperties fileProperties;
private OSS ossClient;
private String bucketName;
public AliFileStorage(FileProperties fileProperties) {
this.fileProperties = fileProperties;
AliStorage aliStorage = fileProperties.getStorage().getAli();
ossClient = new OSSClientBuilder().build(aliStorage.getEndpoint(), aliStorage.getAccessKeyId(),
aliStorage.getAccessKeySecret());
this.bucketName = String.join(CommonConstants.HYPHEN, aliStorage.getBucketName(), EnvironmentUtils.getProfile());
}
@Override
public void upload(InputStream is, String filePath) {
try {
ossClient.putObject(bucketName, dealPath(filePath), is);
} catch (OSSException oe) {
log.info("Caught an OSSException, which means your request made it to OSS, "
+ "but was rejected with an error response for some reason.");
log.info("Error Message:" + oe.getErrorMessage());
log.info("Error Code:" + oe.getErrorCode());
log.info("Request ID:" + oe.getRequestId());
log.info("Host ID:" + oe.getHostId());
throw new DevopsException("oss请求异常" + oe.getMessage());
} catch (ClientException ce) {
log.info("Caught an ClientException, which means the client encountered "
+ "a serious internal problem while trying to communicate with OSS, "
+ "such as not being able to access the network.");
log.info("Error Message:" + ce.getMessage());
throw new DevopsException("oss客户端异常" + ce.getMessage());
}
}
@Override
public void download(String filePath, OutputStream os) {
try(InputStream is = download(filePath)) {
try {
IOUtils.copy(is, os);
} catch (ClientAbortException e) {
throw new BusinessException("下载客户端异常:" + e.getMessage());
}
} catch (IOException ie) {
log.info("Caught an IOException", ie);
throw new DevopsException("oss流处理异常" + ie.getMessage());
}
}
private String dealPath(String path) {
Assert.isTrue(StringUtils.isNotBlank(path), "文件地址不能为空");
String seperate = String.valueOf(File.separatorChar);
if (path.startsWith("/")) {
return path.substring(1);
}
if (!path.startsWith(seperate)) {
return path;
}
return path.substring(seperate.length());
}
@Override
public String getType() {
return FileStorageEnum.ALI_NAME;
}
@Override
public InputStream download(String filePath) {
try {
// 调用ossClient.getObject返回一个OSSObject实例,该实例包含文件内容及文件元信息。
OSSObject ossObject = ossClient.getObject(bucketName, dealPath(filePath));
// 调用ossObject.getObjectContent获取文件输入流,可读取此输入流获取其内容。
InputStream content = ossObject.getObjectContent();
return content;
} catch (OSSException oe) {
log.info("Caught an OSSException, which means your request made it to OSS, "
+ "but was rejected with an error response for some reason.");
log.info("Error Message:" + oe.getErrorMessage());
log.info("Error Code:" + oe.getErrorCode());
log.info("Request ID:" + oe.getRequestId());
log.info("Host ID:" + oe.getHostId());
throw new DevopsException("oss请求异常" + oe.getMessage());
} catch (ClientException ce) {
log.info("Caught an ClientException, which means the client encountered "
+ "a serious internal problem while trying to communicate with OSS, "
+ "such as not being able to access the network.");
log.info("Error Message:" + ce.getMessage());
throw new DevopsException("oss客户端异常" + ce.getMessage());
}
}
}

View File

@@ -1,4 +1,5 @@
spring.application.name=alita-bjy-uygur
spring.profiles.active=uat-uygur
##数据源
spring.datasource.url=jdbc:dm://139.159.237.199:5236?schema=BJY_UYGUR_JOB&useUnicode=true&serverTimezone=Asia/Shanghai&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull&rewriteBatchedStatements=true&allowMultiQueries=true&useSSL=false
@@ -42,6 +43,13 @@ logging.pattern.file:%d{yyyy/MM/dd-HH:mm:ss} %X{traceId:-} [%thread] %-5level %l
file.upload-dir=/var/${spring.application.name}/upload
java.io.tmpdir=/var/${spring.application.name}/upload/tmpdir
#文件存储配置
file.storage.type=ali
file.storage.ali.endpoint=https://oss-cn-shanghai.aliyuncs.com
file.storage.ali.accessKeyId=LTAI5tMYmstNNcvsf67CWR5P
file.storage.ali.accessKeySecret=JyRcBvT8jVC1dhP5ji85WDTmgEOvZW
file.storage.ali.bucketName=newyetai
#发号器
#机器id:根据ip自己定义
code.snowflake.workid.192.168.31.234=23