feat[文件上传]:oss阿里云对象存储集成

This commit is contained in:
2025-01-20 09:29:33 +08:00
parent 9960a696b4
commit 4ce91aa3ef
15 changed files with 357 additions and 18 deletions
@@ -41,5 +41,11 @@
<version>5.6.227</version>
</dependency>
<dependency>
<groupId>com.aliyun.oss</groupId>
<artifactId>aliyun-sdk-oss</artifactId>
<version>3.17.4</version>
</dependency>
</dependencies>
</project>
@@ -3,6 +3,7 @@ package com.central.oss.config;
import com.central.oss.properties.FileServerProperties;
import com.central.oss.template.CosTemplate;
import com.central.oss.template.FdfsTemplate;
import com.central.oss.template.OssTemplate;
import com.central.oss.template.S3Template;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Import;
@@ -13,7 +14,7 @@ import org.springframework.context.annotation.Import;
* <p>
*/
@EnableConfigurationProperties(FileServerProperties.class)
@Import({FdfsTemplate.class, S3Template.class, CosTemplate.class})
@Import({FdfsTemplate.class, S3Template.class, CosTemplate.class, OssTemplate.class})
public class OssAutoConfigure {
}
@@ -19,6 +19,7 @@ public class FileServerProperties {
public static final String TYPE_FDFS = "fastdfs";
public static final String TYPE_S3 = "s3";
public static final String TYPE_COS = "cos";
public static final String TYPE_OSS = "oss";
/**
* 为以下2个值,指定不同的自动化配置
@@ -41,4 +42,9 @@ public class FileServerProperties {
* cos 配置
*/
CosProperties cos = new CosProperties();
/**
* cos 配置
*/
OssProperties oss = new OssProperties();
}
@@ -0,0 +1,41 @@
package com.central.oss.properties;
import lombok.Getter;
import lombok.Setter;
/**
* oss 协议配置
*
* @author lsl
* @date 2025/1/13
* <p>
*/
@Setter
@Getter
public class OssProperties {
/**
* 用户名
*/
private String secretId;
/**
* 密码
*/
private String secretKey;
/**
* bucket名称
*/
private String bucketName;
/**
*
*/
private String endpoint;
/**
* 区域
*/
private String region;
/**
* path-style
*/
private Boolean pathStyleAccessEnabled = true;
}
@@ -0,0 +1,168 @@
package com.central.oss.template;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.OSSObject;
import com.aliyun.oss.model.ObjectMetadata;
import com.aliyun.oss.model.PutObjectRequest;
import com.central.oss.model.ObjectInfo;
import com.central.oss.properties.FileServerProperties;
import lombok.SneakyThrows;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.web.multipart.MultipartFile;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.util.Calendar;
/**
* cos 配置
*
* @author lsl
* @date 2025/1/13
* <p>
*/
@ConditionalOnClass(OSS.class)
@ConditionalOnProperty(prefix = FileServerProperties.PREFIX, name = "type", havingValue = FileServerProperties.TYPE_OSS)
public class OssTemplate implements InitializingBean {
private static final String DEF_CONTEXT_TYPE = "application/octet-stream";
private static final String PATH_SPLIT = "/";
@Autowired
private FileServerProperties fileProperties;
private OSS ossClient;
@Override
public void afterPropertiesSet() {
// 生成 oss 客户端
this.ossClient = new OSSClientBuilder()
.build(fileProperties.getOss().getEndpoint(), fileProperties.getOss().getSecretId(), fileProperties.getOss().getSecretKey());
}
@SneakyThrows
public ObjectInfo upload(String fileName, InputStream is) {
return upload(fileProperties.getOss().getBucketName(), fileName, is, is.available(), DEF_CONTEXT_TYPE);
}
@SneakyThrows
public ObjectInfo upload(MultipartFile file) {
return upload(fileProperties.getOss().getBucketName(), file.getOriginalFilename(), file.getInputStream()
, ((Long)file.getSize()).intValue(), file.getContentType());
}
@SneakyThrows
public ObjectInfo upload(String bucketName, String fileName, InputStream is) {
return upload(bucketName, fileName, is, is.available(), DEF_CONTEXT_TYPE);
}
/**
* 上传对象
* @param bucketName bucket名称
* @param objectName 对象名
*/
public ObjectInfo upload(String bucketName, String objectName, InputStream is, int size, String contentType) {
ObjectMetadata objectMetadata = new ObjectMetadata();
objectMetadata.setContentLength(size);
objectMetadata.setContentType(contentType);
PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, objectName, is, objectMetadata);
ossClient.putObject(putObjectRequest);
ObjectInfo obj = new ObjectInfo();
obj.setObjectPath(bucketName + PATH_SPLIT + objectName);
obj.setObjectUrl(fileProperties.getCos().getEndpoint() + PATH_SPLIT + obj.getObjectPath());
return obj;
}
/**
* 上传对象
* @param objectName 对象名
*/
public ObjectInfo upload(String objectName, InputStream is, int size, String contentType) {
ObjectMetadata objectMetadata = new ObjectMetadata();
objectMetadata.setContentLength(size);
objectMetadata.setContentType(contentType);
PutObjectRequest putObjectRequest = new PutObjectRequest(fileProperties.getCos().getBucketName(), objectName, is, objectMetadata);
ossClient.putObject(putObjectRequest);
ObjectInfo obj = new ObjectInfo();
obj.setObjectPath(fileProperties.getCos().getBucketName() + PATH_SPLIT + objectName);
obj.setObjectUrl(fileProperties.getCos().getEndpoint() + PATH_SPLIT + obj.getObjectPath());
return obj;
}
public void delete(String objectName) {
delete(fileProperties.getCos().getBucketName(), objectName);
}
public void delete(String bucketName, String objectName) {
ossClient.deleteObject(bucketName, objectName);
}
/**
* 获取预览地址
* @param bucketName bucket名称
* @param objectName 对象名
* @param expires 有效时间(分钟),最大7天有效
* @return
*/
public String getViewUrl(String bucketName, String objectName, int expires) {
Calendar cal = Calendar.getInstance();
cal.add(Calendar.MINUTE, expires);
URL url = ossClient.generatePresignedUrl(bucketName, objectName, cal.getTime());
return url.toString();
}
/**
* 获取预览地址
* @param objectName 对象名
* @param expires 有效时间(分钟),最大7天有效
* @return
*/
public String getViewUrl(String objectName, int expires) {
Calendar cal = Calendar.getInstance();
cal.add(Calendar.MINUTE, expires);
URL url = ossClient.generatePresignedUrl(fileProperties.getCos().getBucketName(), objectName, cal.getTime());
return url.toString();
}
/**
* 获取预览地址
* @param objectName 对象名
* @return
*/
public String getViewUrl(String objectName) {
Calendar cal = Calendar.getInstance();
cal.add(Calendar.MINUTE, 7);
URL url = ossClient.generatePresignedUrl(fileProperties.getCos().getBucketName(), objectName, cal.getTime());
return url.toString();
}
public void out(String objectName, OutputStream os) {
out(fileProperties.getS3().getBucketName(), objectName, os);
}
/**
* 输出对象
* @param bucketName bucket名称
* @param objectName 对象名
* @param os 输出流
*/
@SneakyThrows
public void out(String bucketName, String objectName, OutputStream os) {
OSSObject ossObject = ossClient.getObject(bucketName, objectName);
try (
InputStream ossis = ossObject.getObjectContent();
) {
// IOUtils.copy(ossis, os);
}
}
}