ceph 完善 presignedUrl objectsCapacityStr 方法
This commit is contained in:
parent
80db2e2b9e
commit
d890758c43
|
|
@ -38,6 +38,18 @@ public class CephConfig {
|
||||||
* 最后带不带斜杠都可以
|
* 最后带不带斜杠都可以
|
||||||
*/
|
*/
|
||||||
private String domain = null;
|
private String domain = null;
|
||||||
|
/**
|
||||||
|
* 过期时间,单位秒;
|
||||||
|
* 如:1小时就写:3600L
|
||||||
|
* 如:9小时就写:32400L
|
||||||
|
* 如:12小时就写:43200L, 【不支持】,最大是 未知, 最小 1(1秒钟)
|
||||||
|
* 如:-1: 就永不过期,原样返回url
|
||||||
|
* 签名URL的默认过期时间为3600秒,最大值为32400秒
|
||||||
|
*
|
||||||
|
* 注意!!:qoniu oss 设置Bucket私有,必须要有凭证才能访问 https://developer.qiniu.com/kodo/1202/download-token
|
||||||
|
* 下载凭证(如果Bucket设置成私有,必须要有 下载凭证),路径:【对象存储==》使用指南===》安全机制===》 下载凭证】
|
||||||
|
*/
|
||||||
|
private Long expiryDuration = 32400L;
|
||||||
|
|
||||||
public String getAccessKey() {
|
public String getAccessKey() {
|
||||||
return accessKey;
|
return accessKey;
|
||||||
|
|
@ -78,4 +90,16 @@ public class CephConfig {
|
||||||
public void setDomain(String domain) {
|
public void setDomain(String domain) {
|
||||||
this.domain = domain;
|
this.domain = domain;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void setExpiryDuration(Long expiryDuration) {
|
||||||
|
this.expiryDuration = expiryDuration;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Long getExpiryDuration() {
|
||||||
|
if (expiryDuration != -1 && expiryDuration < 0) {
|
||||||
|
// 最小是1秒
|
||||||
|
expiryDuration = 1L;
|
||||||
|
}
|
||||||
|
return expiryDuration;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,6 @@
|
||||||
package com.ruoyi.file.service;
|
package com.ruoyi.file.service;
|
||||||
|
|
||||||
|
import cn.hutool.core.date.DateTime;
|
||||||
import cn.hutool.extra.spring.SpringUtil;
|
import cn.hutool.extra.spring.SpringUtil;
|
||||||
import com.amazonaws.ClientConfiguration;
|
import com.amazonaws.ClientConfiguration;
|
||||||
import com.amazonaws.Protocol;
|
import com.amazonaws.Protocol;
|
||||||
|
|
@ -8,7 +9,6 @@ import com.amazonaws.auth.BasicAWSCredentials;
|
||||||
import com.amazonaws.services.s3.AmazonS3;
|
import com.amazonaws.services.s3.AmazonS3;
|
||||||
import com.amazonaws.services.s3.AmazonS3Client;
|
import com.amazonaws.services.s3.AmazonS3Client;
|
||||||
import com.amazonaws.services.s3.model.*;
|
import com.amazonaws.services.s3.model.*;
|
||||||
import com.ruoyi.common.core.exception.CustomException;
|
|
||||||
import com.ruoyi.file.config.CephConfig;
|
import com.ruoyi.file.config.CephConfig;
|
||||||
import org.apache.commons.lang3.StringUtils;
|
import org.apache.commons.lang3.StringUtils;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
|
|
@ -18,6 +18,11 @@ import org.springframework.stereotype.Service;
|
||||||
import org.springframework.web.multipart.MultipartFile;
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
|
|
||||||
import javax.annotation.PostConstruct;
|
import javax.annotation.PostConstruct;
|
||||||
|
import java.math.BigDecimal;
|
||||||
|
import java.net.URL;
|
||||||
|
import java.util.Date;
|
||||||
|
import java.util.concurrent.atomic.AtomicLong;
|
||||||
|
import java.util.function.Consumer;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author dazer
|
* @author dazer
|
||||||
|
|
@ -126,12 +131,39 @@ public class CephDfsServiceImpl implements IDfsService {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String objectsCapacityStr() {
|
public String objectsCapacityStr() {
|
||||||
throw new CustomException("ceph-获取文件占用空间功能,敬请期待");
|
AtomicLong atomicLong = new AtomicLong();
|
||||||
|
atomicLong.set(0);
|
||||||
|
String result;
|
||||||
|
|
||||||
|
amazonS3.listObjects(cephConfig.getBucketName()).getObjectSummaries().forEach(s3ObjectSummary -> {
|
||||||
|
try {
|
||||||
|
atomicLong.addAndGet(s3ObjectSummary.getSize() / 1024);
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
long size = atomicLong.get();
|
||||||
|
if (size > (1024 * 1024)) {
|
||||||
|
result = (new BigDecimal((double) size / 1024 / 1024)).setScale(2, BigDecimal.ROUND_HALF_UP) + "GB";
|
||||||
|
} else if (size > 1024) {
|
||||||
|
result = (new BigDecimal((double) size / 1024).setScale(2, BigDecimal.ROUND_HALF_UP)) + "MB";
|
||||||
|
} else {
|
||||||
|
result = size + "KB";
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String presignedUrl(String fileUrl) {
|
public String presignedUrl(String fileUrl) {
|
||||||
return fileUrl;
|
String storePath = getStorePath(fileUrl);
|
||||||
|
if (cephConfig.getExpiryDuration() == -1) {
|
||||||
|
return fileUrl;
|
||||||
|
}
|
||||||
|
Date expiration = new DateTime(System.currentTimeMillis() + cephConfig.getExpiryDuration());
|
||||||
|
URL url = amazonS3.generatePresignedUrl(cephConfig.getBucketName(), storePath, expiration);
|
||||||
|
return url.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue