码迷,mamicode.com
首页 > 编程语言 > 详细

Spring Boot获取oss前端下载签名

时间:2020-06-14 20:21:13      阅读:85      评论:0      收藏:0      [点我收藏+]

标签:row   ota   signed   存在   prope   service   map   star   ica   

获取下载签名代码

 /**
     * 获取下载的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前端下载签名

标签:row   ota   signed   存在   prope   service   map   star   ica   

原文地址:https://www.cnblogs.com/taoyuanju/p/13126507.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!