Spring Boot获取oss前端下载签名
Posted 一只奋斗的蜗牛
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spring Boot获取oss前端下载签名相关的知识,希望对你有一定的参考价值。
获取下载签名代码
/**
* 获取下载的url
* @param objectName
* @return
*/
public String downloadUrl(OSSClient ossClient,OssProperties ossProperties,String objectName) {
Download<Object> download = new Download<>();
// 创建OSSClient实例。
try {
long expireTime = 10;
if(StringUtils.isNotBlank(ossProperties.getExpire())){
expireTime = Long.parseLong(ossProperties.getExpire());
}
long expireEndTime = System.currentTimeMillis() + expireTime * 1000;
Date expiration = new Date(expireEndTime);
URL downloadUrl = ossClient.generatePresignedUrl(ossProperties.getBucketName(),objectName,expiration);
/// download.setDownloadUrl(downloadUrl.toString());
return downloadUrl.toString();
}catch (Exception e){
log.error("获取下载url失败:",e);
}
return "";
}
使用说明
@SpringBootApplication
@EnableAliyunOss
@EnableSwagger2Doc
@RestController
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
@ApiOperation("获取下载的url")
@PostMapping("/getUrl")
public String downloadUrl(@RequestParam("objectName") String objectName,@RequestParam("fileName")String fileName) {
UploadFileDO uploadFile = new UploadFileDO();
uploadFile.setNewObjectName(objectName);
uploadFile.setOldObjectName(fileName);
uploadFile.setGmtModified(new Date());
uploadFile.setGmtCreate(new Date());
UploadFileDO uploadFileDO = remoteUploadFileService.getObjectName(uploadFile);
if (uploadFileDO == null){
throw new CusException("该文件不存在");
}
String url = new OssClientManager().downloadUrl(ossClient, ossProperties, objectName);
if (StringUtils.isBlank(url)){
throw new CusException("获取下载url失败");
}
return url;
}
}
以上是关于Spring Boot获取oss前端下载签名的主要内容,如果未能解决你的问题,请参考以下文章
Spring(Boot):达到 EOL 和不再支持 OSS 有啥区别?
Microservices Reference Architecture - with Spring Boot, Spring Cloud and Netflix OSS--转
具有有效证书的spring boot https获取ERR_SSL_VERSION_OR_CIPHER_MISMATCH,自签名工作正常