一只奋斗的蜗牛

vuePress-theme-reco 一只奋斗的蜗牛    2017 - 2020
一只奋斗的蜗牛 一只奋斗的蜗牛

Choose mode

  • dark
  • auto
  • light
Home
Category
  • JavaScript
  • Java
  • Vue
  • Spring Boot
  • CSS
Tag
TimeLine
Contact
  • GitHub
author-avatar

一只奋斗的蜗牛

8

Article

5

Tag

Home
Category
  • JavaScript
  • Java
  • Vue
  • Spring Boot
  • CSS
Tag
TimeLine
Contact
  • GitHub
  • Spring Boot

    • Spring Boot获取oss前端上传签名
    • Spring Boot获取oss前端下载签名
      • 获取下载签名代码
      • 使用说明
    • 自定义阿里云oss Starter

Spring Boot获取oss前端下载签名

vuePress-theme-reco 一只奋斗的蜗牛    2017 - 2020

Spring Boot获取oss前端下载签名


一只奋斗的蜗牛 2020-06-14 Spring Boot

# 获取下载签名代码

 /**
     * 获取下载的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 "";
    }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26

# 使用说明

@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;
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28

码云