update
This commit is contained in:
@@ -0,0 +1,65 @@
|
||||
package com.cool.store.controller;
|
||||
|
||||
import com.aliyun.oss.OSSClient;
|
||||
import com.aliyun.oss.common.utils.BinaryUtil;
|
||||
import com.aliyun.oss.model.MatchMode;
|
||||
import com.aliyun.oss.model.PolicyConditions;
|
||||
import com.cool.store.response.ResponseResult;
|
||||
import com.cool.store.vo.oss.OssUploadConfigVO;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import java.sql.Date;
|
||||
|
||||
/**
|
||||
* @author zhangchenbiao
|
||||
* @FileName: OssClientController
|
||||
* @Description:
|
||||
* @date 2023-06-01 11:25
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping({"/oss"})
|
||||
@Slf4j
|
||||
public class OssClientController {
|
||||
|
||||
@Value("${oss.accessKeyId:null}")
|
||||
private String accessKeyId;
|
||||
@Value("${oss.accessKeySecret:null}")
|
||||
private String accessKeySecret;
|
||||
@Value("${oss.endpoint:null}")
|
||||
private String endpoint;
|
||||
@Value("${oss.bucket:null}")
|
||||
private String bucket;
|
||||
@Value("${corp.id:null}")
|
||||
private String corpId;
|
||||
|
||||
@GetMapping("/getUploadFileConfig")
|
||||
public ResponseResult getUploadFileConfig(){
|
||||
// host的格式为 bucketname.endpoint
|
||||
String host = "http://" + bucket + "." + endpoint;
|
||||
// 用户上传文件时指定的前缀。
|
||||
String dir = "partner/" + corpId + "/";
|
||||
|
||||
OSSClient client = new OSSClient(endpoint, accessKeyId, accessKeySecret);
|
||||
try {
|
||||
long expireTime = 300;
|
||||
long expireEndTime = System.currentTimeMillis() + expireTime * 1000;
|
||||
Date expiration = new Date(expireEndTime);
|
||||
PolicyConditions policyConds = new PolicyConditions();
|
||||
policyConds.addConditionItem(PolicyConditions.COND_CONTENT_LENGTH_RANGE, 0, 1048576000);
|
||||
policyConds.addConditionItem(MatchMode.StartWith, PolicyConditions.COND_KEY, dir);
|
||||
String postPolicy = client.generatePostPolicy(expiration, policyConds);
|
||||
byte[] binaryData = postPolicy.getBytes("utf-8");
|
||||
String encodedPolicy = BinaryUtil.toBase64String(binaryData);
|
||||
String signature = client.calculatePostSignature(postPolicy);
|
||||
OssUploadConfigVO result = new OssUploadConfigVO(accessKeyId, encodedPolicy, signature, dir, host, String.valueOf(expireEndTime / 1000));
|
||||
return ResponseResult.success(result);
|
||||
}catch (Exception e){
|
||||
log.info("exception", e);
|
||||
}
|
||||
return ResponseResult.success();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user