增加签名的url[presignedUrl], minio和aliyun oss开发、测试完成

pull/84/head
duandazhi 4 years ago
parent a0d68be942
commit 30558aa7e1

@ -21,7 +21,7 @@ import org.springframework.stereotype.Component;
public class MinioConfig {
public static final String PREFIX = "minio";
/**
*
* url endpoint
* eg: http://192.168.254.100:9900
*/
private String url;
@ -46,7 +46,7 @@ public class MinioConfig {
/**
* 访; url访ip
* eg: https://image.bj.gov.cn/appt-file
* eg: https://yq666.bj.gov.cn/appt-file
*/
private String domain;

@ -1,12 +1,15 @@
package com.ruoyi.file.controller;
import com.ruoyi.file.service.IDfsService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import com.ruoyi.common.core.domain.R;
@ -46,6 +49,7 @@ public class SysFileController {
/**
*
*/
@ApiOperation(value = "删除文件", notes = "根据文件的完整url进行删除资源")
@PostMapping("delete")
public R<Boolean> delete(String fileUrl) {
if (StringUtils.isBlank(fileUrl)) {
@ -65,8 +69,22 @@ public class SysFileController {
*
* 233.57 GB 72.12 GB
*/
@ApiOperation(value = "获取文件服务器的容量", notes = "获取文件服务器占用资源的容量,剩余容量(可选)")
@GetMapping("objectsCapacityStr")
public R<String> objectsCapacityStr() {
public R<String> capacityStr() {
return R.ok(dfsService.objectsCapacityStr(), "获取成功");
}
/**
* 访url
* 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
* 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
*/
@ApiOperation(value = "临时安全凭证、获取加签的url", notes = "根据输入的url,获取带有临时安全凭证的url")
@GetMapping("getPresignedUrl")
public R<String> getPresignedUrl(
@ApiParam("需要访问的url,字段名:fileUrl,必填;不要带有?后面的参数") @RequestParam(value = "fileUrl") String fileUrl) {
return R.ok(dfsService.presignedUrl(fileUrl), "获取成功");
}
}

@ -42,6 +42,7 @@ import java.util.concurrent.TimeUnit;
* @date 2019/8/6 19:02
* //@see AliyunMsgUtil
*/
//@Primary
@Service
public class AliyunOssDsfServiceImpl implements IDfsService {
private static final Logger log = LoggerFactory.getLogger(AliyunOssDsfServiceImpl.class);
@ -516,6 +517,12 @@ public class AliyunOssDsfServiceImpl implements IDfsService {
return result;
}
@Override
public String presignedUrl(String fileUrl) {
String objectKey = this.getStorePath(fileUrl);
return this.getStsURL(objectKey);
}
/**
* oss 使URL
* https://help.aliyun.com/document_detail/32016.html?spm=a2c4g.11186623.6.992.7a943b4aPjkyTA#title-pu8-5o8-x7j
@ -523,7 +530,7 @@ public class AliyunOssDsfServiceImpl implements IDfsService {
* @param objectName url, filePath
* @return urlurl
*/
public String getStsURL(String objectName) {
private String getStsURL(String objectName) {
if (StringUtils.isBlank(objectName)) {
return objectName;
}

@ -129,6 +129,11 @@ public class CephDfsServiceImpl implements IDfsService {
throw new CustomException("ceph-获取文件占用空间功能,敬请期待");
}
@Override
public String presignedUrl(String fileUrl) {
return fileUrl;
}
/**
* url
*

@ -72,4 +72,9 @@ public class FastDfsServiceImpl implements IDfsService
public String objectsCapacityStr() {
throw new CustomException("fastdfs-获取文件占用空间功能,敬请期待");
}
@Override
public String presignedUrl(String fileUrl) {
return fileUrl;
}
}

@ -77,6 +77,11 @@ public class FtpFileServiceImpl implements IDfsService {
throw new CustomException("fpt-获取文件占用空间功能,敬请期待");
}
@Override
public String presignedUrl(String fileUrl) {
return fileUrl;
}
/**
* urlkey
*

@ -65,6 +65,22 @@ public interface IDfsService
*/
String objectsCapacityStr();
/**
* 访url
* AWS Security Token Service (STS) (federation token)
*
* 1aliyun oss STS Security Token ServiceSTS https://help.aliyun.com/document_detail/28761.html?spm=a2c4g.11186623.6.880.22bd2fe5pL1d39
* 2minio resignedGetObject Security Token ServiceSTS; Presigned presignedGetObject
* http://docs.minio.org.cn/docs/master/minio-sts-quickstart-guide
* minio SDKS Java Client API http://docs.minio.org.cn/docs/master/java-client-api-reference
* 3qiniu 访 https://developer.qiniu.com/kodo/1239/java#fusion-antileech
* https://developer.qiniu.com/kodo/5914/s3-compatible-sts
* 4 访 GetFederationToken 使 https://cloud.tencent.com/document/product/436/14048?from=10680
* @param fileUrl 访,
* @return url
*/
String presignedUrl(String fileUrl);
/**
* & & &
*

@ -84,6 +84,11 @@ public class LocalFileServiceImpl implements IDfsService
return "总 " + totalSpace + " 可用 " + freeSpace;
}
@Override
public String presignedUrl(String fileUrl) {
return fileUrl;
}
/**
* urlkey
*

@ -1,21 +1,24 @@
package com.ruoyi.file.service;
import cn.hutool.extra.spring.SpringUtil;
import com.ruoyi.common.core.exception.CustomException;
import io.minio.RemoveObjectArgs;
import io.minio.*;
import io.minio.errors.*;
import io.minio.http.Method;
import io.minio.messages.Item;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import com.ruoyi.file.config.MinioConfig;
import io.minio.MinioClient;
import io.minio.PutObjectArgs;
import java.io.IOException;
import java.math.BigDecimal;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicLong;
import java.util.function.Consumer;
/**
* Minio
@ -38,7 +41,7 @@ public class MinioDfsServiceImpl implements IDfsService
@Autowired
private MinioConfig minioConfig;
@Autowired
private MinioClient client;
private MinioClient minioClient;
/**
*
@ -68,9 +71,9 @@ public class MinioDfsServiceImpl implements IDfsService
.stream(file.getInputStream(), file.getSize(), -1)
.contentType(file.getContentType())
.build();
client.putObject(args);
minioClient.putObject(args);
//return minioConfig.getUrl() + "/" + minioConfig.getBucketName() + "/" + fileName;
//形如https://image.bj.gov.cn/appt-file/dev/default/2021/07/18/f4243eb2-06a1-4304-bdfc-e2964b8721bb.jpeg
//形如https://yq666.bj.gov.cn/appt-file/dev/default/2021/07/18/f4243eb2-06a1-4304-bdfc-e2964b8721bb.jpeg
return minioConfig.getDomain() + "/" + minioConfig.getBucketName() + "/" + fileName;
}
@ -81,7 +84,7 @@ public class MinioDfsServiceImpl implements IDfsService
object(getStorePath(fileUrl)).
build();
try {
client.removeObject(args);
minioClient.removeObject(args);
return true;
} catch (ErrorResponseException |
InsufficientDataException |
@ -99,13 +102,69 @@ public class MinioDfsServiceImpl implements IDfsService
@Override
public String objectsCapacityStr() {
throw new CustomException("minio存储-获取文件占用空间功能,敬请期待");
/*MinioClient minioClient = MinioClient.builder().endpoint(minioConfig.getUrl())
.credentials(minioConfig.getAccessKey(), minioConfig.getSecretKey())
.build();*/
AtomicLong atomicLong = new AtomicLong();
atomicLong.set(0);
String result = "";
ListObjectsArgs args = ListObjectsArgs.builder().bucket(minioConfig.getBucketName()).build();
minioClient.listObjects(args).forEach(new Consumer<Result<Item>>() {
@Override
public void accept(Result<Item> itemResult) {
try {
atomicLong.addAndGet(itemResult.get().size() / 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;
}
/**
* URL add
* [STS ]
* http://docs.minio.org.cn/docs/master/minio-sts-quickstart-guide
* minio SDKS Java Client API http://docs.minio.org.cn/docs/master/java-client-api-reference
* Presigned presignedGetObject
*/
@Override
public String presignedUrl(String fileUrl) {
String objectName = this.getStorePath(fileUrl);
GetPresignedObjectUrlArgs args = GetPresignedObjectUrlArgs.builder().
bucket(minioConfig.getBucketName()).
method(Method.GET).
object(objectName).expiry(5, TimeUnit.DAYS).build();
String presignedObjectUrl = null;
try {
presignedObjectUrl = minioClient.getPresignedObjectUrl(args);
String basePrivateUrl = minioConfig.getUrl() + "/" + minioConfig.getBucketName() + "/";
presignedObjectUrl = presignedObjectUrl.replace(basePrivateUrl, "");
} catch (ErrorResponseException | InsufficientDataException | InternalException | InvalidKeyException | InvalidResponseException | IOException | NoSuchAlgorithmException | XmlParserException | ServerException e) {
e.printStackTrace();
presignedObjectUrl = fileUrl;
}
return minioConfig.getDomain() + "/" + minioConfig.getBucketName() + "/" + presignedObjectUrl;
}
/**
* urlkey
*
* @param filePath https://image.bj.gov.cn/appt-file/dev/default/2021/07/18/f4243eb2-06a1-4304-bdfc-e2964b8721bb.jpeg
* @param filePath https://yq666.bj.gov.cn/appt-file/dev/default/2021/07/18/f4243eb2-06a1-4304-bdfc-e2964b8721bb.jpeg
* @return dev/default/2021/07/18/f4243eb2-06a1-4304-bdfc-e2964b8721bb.jpeg
*/
private String getStorePath(String filePath) {

@ -29,6 +29,8 @@ import java.io.InputStream;
* @see QiniuKodoConfig
* https://developer.qiniu.com/kodo/1239/java#upload-stream
* ==>JAVASDK==>==>
*
* 访 https://developer.qiniu.com/kodo/1239/java#fusion-antileech
*/
//@Primary
@Service
@ -116,6 +118,11 @@ public class QiniuDfsServiceImpl implements IDfsService {
throw new CustomException("七牛云-获取文件占用空间功能,敬请期待");
}
@Override
public String presignedUrl(String fileUrl) {
return fileUrl;
}
/**
* urlkey
*

@ -17,9 +17,11 @@ spring:
discovery:
# 服务注册地址
server-addr: 127.0.0.1:8848
namespace: 72b686a1-d9f6-499f-8275-e481b664779e
config:
# 配置中心地址
server-addr: 127.0.0.1:8848
namespace: 72b686a1-d9f6-499f-8275-e481b664779e
# 配置文件格式
file-extension: yml
# 共享配置
@ -54,7 +56,7 @@ minio:
accessKey: D99KGE6ZTQXSATTJWU24
secretKey: QyVqGnhIQQE734UYSUFlGOZViE6+ZlDEfUG3NjhJ
bucketName: appt-file
domain: https://image.bj.gov.cn
domain: https://yq666.bj.gov.cn
# 文件服务器之5 aliyun oss
aliyun-oss:

Loading…
Cancel
Save