Skip to content

Commit

Permalink
fix(oss): 修改私有访问的 bucket previewUrl 链接的拼接
Browse files Browse the repository at this point in the history
  • Loading branch information
andanyoung committed Nov 10, 2023
1 parent ba6d8f4 commit 2632aa7
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 11 deletions.
2 changes: 1 addition & 1 deletion oss-spring-boot-starter/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

<groupId>com.admin4j.framework</groupId>
<artifactId>oss-spring-boot-starter</artifactId>
<version>0.9.0</version>
<version>0.9.1</version>
<packaging>jar</packaging>
<description>封装基于Amazon S3的OSS对象存储服务。在SpringBoot 中通过封装,简单地方式将文件存储到
MinIO、阿里云OSS、华为云OBS、七牛云Kodo、腾讯云COS等支持
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.admin4j.oss;

import lombok.Data;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.boot.context.properties.ConfigurationProperties;

/**
Expand All @@ -9,7 +11,7 @@
*/
@Data
@ConfigurationProperties(prefix = "admin4j.oss")
public class OssProperties {
public class OssProperties implements InitializingBean {
/**
* 是否启用
*/
Expand Down Expand Up @@ -64,4 +66,28 @@ public class OssProperties {
* 最大线程数,默认: 100
*/
private Integer maxConnections = 100;

@Override
public void afterPropertiesSet() throws Exception {
init();
}

/**
* 初始化方法
* - Endpoint 不能带 /
* - PreviewUrl, IntranetUrl 需要带 /
*/
public void init() {

if (StringUtils.endsWith(getEndpoint(), "/")) {
setEndpoint(StringUtils.substring(getEndpoint(), 0, -1));
}

if (!StringUtils.endsWith(getPreviewUrl(), "/")) {
setPreviewUrl(getPreviewUrl() + "/");
}
if (!StringUtils.endsWith(getIntranetUrl(), "/")) {
setIntranetUrl(getIntranetUrl() + "/");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import com.admin4j.oss.UploadFileService;
import com.admin4j.oss.entity.vo.UploadFileVO;
import com.amazonaws.services.s3.model.PutObjectResult;
import lombok.RequiredArgsConstructor;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.multipart.MultipartFile;
Expand All @@ -21,7 +20,7 @@
* @author andanyang
* @since 2023/4/14 9:27
*/
@RequiredArgsConstructor
// @RequiredArgsConstructor
public class SimpleOSSUploadFileService implements UploadFileService {

protected final static DateTimeFormatter FILEPATH_DATETIME_FORMATTER = DateTimeFormatter.ofPattern("yyyyMMdd");
Expand All @@ -30,6 +29,12 @@ public class SimpleOSSUploadFileService implements UploadFileService {

protected final OssProperties ossProperties;

public SimpleOSSUploadFileService(OssTemplate ossTemplate, OssProperties ossProperties) {
this.ossTemplate = ossTemplate;
this.ossProperties = ossProperties;
}


/**
* 上传文件
*
Expand All @@ -45,11 +50,11 @@ public UploadFileVO upload(String path, MultipartFile file) throws IOException {
uploadFileVO.setContentType(file.getContentType());
uploadFileVO.setCreateTime(LocalDateTime.now());
uploadFileVO.setBucket(defaultBucketName());
//计算文件md5
// 计算文件md5
String md5 = DigestUtils.md5Hex(file.getBytes());
uploadFileVO.setMd5(md5);

//生成文件存储路径
// 生成文件存储路径
if (StringUtils.isNotBlank(path)) {
uploadFileVO.setPrefix(path);
}
Expand Down Expand Up @@ -111,7 +116,7 @@ public UploadFileVO upload(String key, String originalFilename, String contentTy
uploadFileVO.setContentType(contentType);
uploadFileVO.setCreateTime(LocalDateTime.now());
uploadFileVO.setBucket(defaultBucketName());
//计算文件md5
// 计算文件md5
String md5 = DigestUtils.md5Hex(file.getBytes());
uploadFileVO.setMd5(md5);

Expand All @@ -133,7 +138,7 @@ public String getPreviewUrl(String key) {
if (ossProperties.getExpires() == -1) {
return ossProperties.getPreviewUrl() + key;
} else {
return getPrivateUrl(key, ossProperties.getExpires());
return getPrivateUrl(ossProperties.getPreviewUrl(), key, ossProperties.getExpires());
}
}
return getPrivateUrl(key, ossProperties.getExpires() == -1 ? 300 : ossProperties.getExpires());
Expand All @@ -153,6 +158,25 @@ public String getPrivateUrl(String key, Integer expires) {
return ossTemplate.getObjectURL(defaultBucketName(), key, expires, TimeUnit.SECONDS);
}

/**
* 使用预览域名,通过OSS直接查看文件预览路径
* 获取私有链接
*
* @param url 域名前缀
* @param key oss key
* @param expires 私有链接有效秒数
* @return 文件阅览路径
*/
// @Override
public String getPrivateUrl(String url, String key, Integer expires) {
String objectURL = ossTemplate.getObjectURL(defaultBucketName(), key, expires, TimeUnit.SECONDS);
if (StringUtils.isNotBlank(url)) {

return objectURL.replace(ossProperties.getEndpoint() + "/" + defaultBucketName() + "/", url);
}
return objectURL;
}

/**
* 文件内网阅览路径
*
Expand All @@ -165,7 +189,7 @@ public String getPreviewIntranetUrl(String key) {
if (ossProperties.getExpires() == -1) {
return ossProperties.getIntranetUrl() + key;
} else {
return getPrivateUrl(key, ossProperties.getExpires());
return getPrivateUrl(ossProperties.getIntranetUrl(), key, ossProperties.getExpires());
}
}
return getPreviewUrl(key);
Expand All @@ -192,7 +216,7 @@ protected String generateFilePath(UploadFileVO uploadFileVO) {
if (StringUtils.isNotBlank(uploadFileVO.getKey())) {
return uploadFileVO.getKey();
}
//后缀
// 后缀
String postfix = null;
if (StringUtils.isNotBlank(uploadFileVO.getOriginalFilename()) && StringUtils.contains(uploadFileVO.getOriginalFilename(), ".")) {

Expand All @@ -201,7 +225,7 @@ protected String generateFilePath(UploadFileVO uploadFileVO) {

StringBuilder filePathBuilder = new StringBuilder();

//文件前缀
// 文件前缀
if (StringUtils.isNotBlank(uploadFileVO.getPrefix())) {
filePathBuilder.append(uploadFileVO.getPrefix());
if (!StringUtils.endsWith(uploadFileVO.getPrefix(), "/")) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package com.admin4j.oss.impl;

import com.admin4j.oss.AmazonS3Factory;
import com.admin4j.oss.OssProperties;
import com.amazonaws.services.s3.AmazonS3;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

/**
* @author andanyang
* @since 2023/11/10 9:48
*/
class SimpleOSSUploadFileServiceTest {

static SimpleOSSUploadFileService simpleOSSUploadFileService;

@BeforeAll
public static void init() {

OssProperties ossProperties = new OssProperties();
ossProperties.setEndpoint("http://192.168.1.13:9901/");
ossProperties.setAccessKey("4NdsQG6g6hzJfwko");
ossProperties.setSecretKey("zeSSPz3WC3Wn4zBZsYtxK9YXhZ7Hjnnv");
ossProperties.setBucket("crm-email");
ossProperties.setPreviewUrl("http://192.168.1.13:9902/crm-email");
ossProperties.setIntranetUrl("http://192.168.1.13:9902/crm/");
ossProperties.setExpires(300);

ossProperties.init();

AmazonS3 amazonS3 = AmazonS3Factory.create(ossProperties);
OssTemplateImpl ossTemplate = new OssTemplateImpl(amazonS3, ossProperties);

simpleOSSUploadFileService = new SimpleOSSUploadFileService(ossTemplate, ossProperties);
}

@Test
void getPreviewUrl() {

String previewUrl = simpleOSSUploadFileService.getPreviewUrl("attachment/20231005/cc56268d64944a7348d43741129292fa.jpg");
System.out.println("previewUrl = " + previewUrl);
}

@Test
void getPrivateUrl() {
}

@Test
void testGetPrivateUrl() {
}

@Test
void getPreviewIntranetUrl() {
}
}

0 comments on commit 2632aa7

Please sign in to comment.