aliyun oss和minio 增加过期时间配置

pull/84/head
duandazhi 4 years ago
parent b863b079d9
commit 69dc496580

@ -23,7 +23,7 @@ public class AliyunOssConfig {
* AccessKeyId secretKeyeg:LTAI4GFov2QymkmPf9cXdH5z * AccessKeyId secretKeyeg:LTAI4GFov2QymkmPf9cXdH5z
* AccessKeySecret secretKey eg:ap8nmIvD1TctcCLsADS4JbkOoXOluW * AccessKeySecret secretKey eg:ap8nmIvD1TctcCLsADS4JbkOoXOluW
* BucketName eg:yuebaoxiao * BucketName eg:yuebaoxiao
* Endpoint eg:oss-cn-shenzhen.aliyuncs.com * Endpoint URL eg:oss-cn-shenzhen.aliyuncs.com
* *
* ak https://ak-console.aliyun.com/#/accesskey * ak https://ak-console.aliyun.com/#/accesskey
* *
@ -44,6 +44,16 @@ public class AliyunOssConfig {
* 4: eg: https://image.jl-media.cn * 4: eg: https://image.jl-media.cn
*/ */
private String domain = null; private String domain = null;
/**
*
* 13600L
* 932400L
* 1243200L, 324009 11
* -1 url
* URL360032400
* 访 https://help.aliyun.com/document_detail/32016.html?spm=a2c4g.11186623.6.996.335b6d13O5xgUH
*/
private Long expiryDuration = 32400L;
public String getAccessKey() { public String getAccessKey() {
return accessKey; return accessKey;
@ -84,4 +94,21 @@ public class AliyunOssConfig {
public void setDomain(String domain) { public void setDomain(String domain) {
this.domain = domain; this.domain = domain;
} }
public Long getExpiryDuration() {
if (expiryDuration == null) {
// 默认一个小时, 3600秒
expiryDuration = 3600L;
}
if (expiryDuration < 1L && expiryDuration != -1) {
// 最小1秒
// 如果要永不过期,就不要调用 -1 直接原样返回
expiryDuration = 1L;
}
return expiryDuration;
}
public void setExpiryDuration(Long expiryDuration) {
this.expiryDuration = expiryDuration;
}
} }

@ -8,6 +8,8 @@ import org.springframework.context.annotation.Configuration;
import io.minio.MinioClient; import io.minio.MinioClient;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import java.util.concurrent.TimeUnit;
/** /**
* Minio * Minio
* *
@ -20,8 +22,14 @@ import org.springframework.stereotype.Component;
@ConfigurationProperties(prefix = MinioConfig.PREFIX) @ConfigurationProperties(prefix = MinioConfig.PREFIX)
public class MinioConfig { public class MinioConfig {
public static final String PREFIX = "minio"; public static final String PREFIX = "minio";
@Bean
public MinioClient getMinioClient() {
return MinioClient.builder().endpoint(url).credentials(accessKey, secretKey).build();
}
/** /**
* url endpoint * url endpoint URL
* eg: http://192.168.254.100:9900 * eg: http://192.168.254.100:9900
*/ */
private String url; private String url;
@ -50,6 +58,18 @@ public class MinioConfig {
*/ */
private String domain; private String domain;
/**
*
* MinIO STS http://docs.minio.org.cn/docs/master/minio-sts-quickstart-guide
* Amazon S3MinIO Java SDK: API: Presigned: presignedGetObject: http://docs.minio.org.cn/docs/master/java-client-quickstart-guide
* 7
* 13600 = 60 * 60 * 1
* 24186400 = 60 * 60 * 24
* 7604800 = 86400 * 7
* -1 url
*/
private Integer expiryDuration = 86400;
public String getUrl() { public String getUrl() {
return url; return url;
} }
@ -90,8 +110,20 @@ public class MinioConfig {
this.domain = domain; this.domain = domain;
} }
@Bean public Integer getExpiryDuration() {
public MinioClient getMinioClient() { if (expiryDuration == null) {
return MinioClient.builder().endpoint(url).credentials(accessKey, secretKey).build(); // 默认一个小时, 3600秒
expiryDuration = 86400;
}
if (expiryDuration < 1L && expiryDuration != -1) {
// 最小1秒
// 如果要永不过期,就不要调用 -1 直接原样返回
expiryDuration = 1;
}
return expiryDuration;
}
public void setExpiryDuration(Integer expiryDuration) {
this.expiryDuration = expiryDuration;
} }
} }

@ -80,6 +80,8 @@ public class SysFileController {
* AWS Security Token Service (STS) (federation token) * AWS Security Token Service (STS) (federation token)
* aliyun oss : http://react-yuebaoxiao-pro.oss-cn-shanghai.aliyuncs.com/dev/upload/default/20210719-23d31398-4849-408d-8775-a5b668ccafc3.jpeg?Expires=1626736182&OSSAccessKeyId=LTAI4GDQSbwgmbsRxxbDXnKT&Signature=P3w3%2FIpEnZEUhYku6scOos4p54A%3D * aliyun oss : http://react-yuebaoxiao-pro.oss-cn-shanghai.aliyuncs.com/dev/upload/default/20210719-23d31398-4849-408d-8775-a5b668ccafc3.jpeg?Expires=1626736182&OSSAccessKeyId=LTAI4GDQSbwgmbsRxxbDXnKT&Signature=P3w3%2FIpEnZEUhYku6scOos4p54A%3D
* minio : https://yq666.bj.gov.cn/appt-file/dev/default/2021/07/19/5fe1478b-969c-4b6e-9cc0-742412dc3128.jpeg?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=D99KGE6ZTQXSATTJWU24%2F20210719%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20210719T112025Z&X-Amz-Expires=432000&X-Amz-SignedHeaders=host&X-Amz-Signature=e45171d0885f006ee1de43cec9d88963e2b55c6e671740ae5695410ba16770c5 * minio : https://yq666.bj.gov.cn/appt-file/dev/default/2021/07/19/5fe1478b-969c-4b6e-9cc0-742412dc3128.jpeg?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=D99KGE6ZTQXSATTJWU24%2F20210719%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20210719T112025Z&X-Amz-Expires=432000&X-Amz-SignedHeaders=host&X-Amz-Signature=e45171d0885f006ee1de43cec9d88963e2b55c6e671740ae5695410ba16770c5
* ---------------------------------------------------
* 访 bucket 访
*/ */
@ApiOperation(value = "临时安全凭证、获取加签的url", notes = "根据输入的url,获取带有临时安全凭证的url") @ApiOperation(value = "临时安全凭证、获取加签的url", notes = "根据输入的url,获取带有临时安全凭证的url")
@GetMapping("getPresignedUrl") @GetMapping("getPresignedUrl")

@ -518,6 +518,9 @@ public class AliyunOssDsfServiceImpl implements IDfsService {
@Override @Override
public String presignedUrl(String fileUrl) { public String presignedUrl(String fileUrl) {
if (aliyunOssConfig.getExpiryDuration() == -1) {
return fileUrl;
}
String objectKey = this.getStorePath(fileUrl); String objectKey = this.getStorePath(fileUrl);
return this.getStsURL(objectKey); return this.getStsURL(objectKey);
} }
@ -539,14 +542,16 @@ public class AliyunOssDsfServiceImpl implements IDfsService {
return objectName; return objectName;
} }
if (objectName.startsWith("/")) { if (objectName.startsWith("/")) {
objectName = objectName.replaceFirst("/", ""); // 不能以/ 开头。例如 /dev/upload/123.jpg需要转为 dev/upload/123.jpg // 不能以/ 开头。例如 /dev/upload/123.jpg需要转为 dev/upload/123.jpg
objectName = objectName.replaceFirst("/", "");
} }
} catch (MalformedURLException e) { } catch (MalformedURLException e) {
// 忽略 // 忽略
} }
OSS ossClient = new OSSClientBuilder().build(aliyunOssConfig.getEndpoint(), aliyunOssConfig.getAccessKey(), aliyunOssConfig.getSecretKey()); OSS ossClient = new OSSClientBuilder().build(aliyunOssConfig.getEndpoint(), aliyunOssConfig.getAccessKey(), aliyunOssConfig.getSecretKey());
// 设置URL过期时间为12小时最大值就是43200 // 设置URL过期时间为9小时最大值就是 32400L
Date expiration = new Date(System.currentTimeMillis() + (43200 * 1000)); // 设置签名URL过期时间为3600秒1小时
Date expiration = new Date(System.currentTimeMillis() + (aliyunOssConfig.getExpiryDuration() * 1000));
// 生成以GET方法访问的签名URL访客可以直接通过浏览器访问相关内容。 // 生成以GET方法访问的签名URL访客可以直接通过浏览器访问相关内容。
URL url = ossClient.generatePresignedUrl(aliyunOssConfig.getBucketName(), objectName, expiration); URL url = ossClient.generatePresignedUrl(aliyunOssConfig.getBucketName(), objectName, expiration);
// 关闭OSSClient。 // 关闭OSSClient。

@ -143,11 +143,15 @@ public class MinioDfsServiceImpl implements IDfsService
*/ */
@Override @Override
public String presignedUrl(String fileUrl) { public String presignedUrl(String fileUrl) {
if (minioConfig.getExpiryDuration() == -1) {
return fileUrl;
}
String objectName = this.getStorePath(fileUrl); String objectName = this.getStorePath(fileUrl);
GetPresignedObjectUrlArgs args = GetPresignedObjectUrlArgs.builder(). GetPresignedObjectUrlArgs args = GetPresignedObjectUrlArgs.builder().
bucket(minioConfig.getBucketName()). bucket(minioConfig.getBucketName()).
method(Method.GET). method(Method.GET).
object(objectName).expiry(5, TimeUnit.DAYS).build(); object(objectName).
expiry(minioConfig.getExpiryDuration(), TimeUnit.SECONDS).build();
String presignedObjectUrl = null; String presignedObjectUrl = null;
try { try {

Loading…
Cancel
Save