|
@@ -1,105 +0,0 @@
|
|
|
-package com.llisoft.service.file.service;
|
|
|
-
|
|
|
-import java.net.URL;
|
|
|
-import java.util.Date;
|
|
|
-import java.util.Objects;
|
|
|
-import java.util.UUID;
|
|
|
-
|
|
|
-import javax.annotation.PostConstruct;
|
|
|
-
|
|
|
-import org.slf4j.Logger;
|
|
|
-import org.slf4j.LoggerFactory;
|
|
|
-import org.springframework.beans.factory.annotation.Value;
|
|
|
-import org.springframework.stereotype.Service;
|
|
|
-
|
|
|
-import com.aliyun.oss.ClientConfiguration;
|
|
|
-import com.aliyun.oss.OSSClient;
|
|
|
-import com.aliyun.oss.common.auth.DefaultCredentialProvider;
|
|
|
-import com.aliyun.oss.common.utils.BinaryUtil;
|
|
|
-import com.aliyun.oss.model.MatchMode;
|
|
|
-import com.aliyun.oss.model.PolicyConditions;
|
|
|
-import com.llisoft.service.file.vo.UploadCdnResponseVo;
|
|
|
-import com.llisoft.service.file.vo.UploadResponseVo;
|
|
|
-
|
|
|
-/**
|
|
|
- * 阿里云OSS接口调用
|
|
|
- * @author YangJie [2019年2月18日]
|
|
|
- */
|
|
|
-@Service
|
|
|
-public class OssService {
|
|
|
-
|
|
|
- private Logger logger = LoggerFactory.getLogger(OssService.class);
|
|
|
-
|
|
|
- @Value("${mta.debug}")
|
|
|
- private boolean debug;
|
|
|
-
|
|
|
- @Value("${oss.url}")
|
|
|
- private String url;
|
|
|
- @Value("${oss.bucket}")
|
|
|
- private String bucket;
|
|
|
- @Value("${oss.endpoint}")
|
|
|
- private String endpoint;
|
|
|
- @Value("${oss.accessKeyId}")
|
|
|
- private String accessKeyId;
|
|
|
- @Value("${oss.accessKeySecret}")
|
|
|
- private String accessKeySecret;
|
|
|
-
|
|
|
-
|
|
|
- // OSS客户端
|
|
|
- private OSSClient ossClient;
|
|
|
-
|
|
|
- @PostConstruct
|
|
|
- public void init() {
|
|
|
- ossClient = new OSSClient(endpoint,new DefaultCredentialProvider(accessKeyId, accessKeySecret), new ClientConfiguration());
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 上传
|
|
|
- * @return
|
|
|
- * @throws Exception
|
|
|
- */
|
|
|
- public UploadResponseVo upload(String prefix, String suffix, boolean cdn) throws Exception {
|
|
|
- suffix = Objects.nonNull(suffix) ? "."+suffix : "";
|
|
|
- prefix = Objects.nonNull(prefix) ? prefix : "";
|
|
|
- prefix = cdn ? "public/" + prefix : "private/" + prefix; // 区分公开资源和私有资源
|
|
|
- prefix = debug ? "test/" + prefix : prefix; // 测试环境下添加固定前缀, 定时清理
|
|
|
- PolicyConditions policyConditions = new PolicyConditions();
|
|
|
- policyConditions.addConditionItem(PolicyConditions.COND_CONTENT_LENGTH_RANGE, 0, 1024*1024*1024*1); // 内容长度范围1G
|
|
|
- policyConditions.addConditionItem(MatchMode.StartWith, PolicyConditions.COND_KEY, prefix); // key前缀
|
|
|
- String postPolicy = ossClient.generatePostPolicy(this.getExpiration(10), policyConditions); // 有效期10分钟
|
|
|
- UploadResponseVo uploadBean = cdn ? new UploadCdnResponseVo() : new UploadResponseVo();
|
|
|
- uploadBean.setKey(prefix + UUID.randomUUID().toString().replace("-", "") + suffix);
|
|
|
- uploadBean.setPolicy(BinaryUtil.toBase64String(postPolicy.getBytes("utf-8")));
|
|
|
- uploadBean.setSignature(ossClient.calculatePostSignature(postPolicy));
|
|
|
- uploadBean.setAccessid(accessKeyId);
|
|
|
- uploadBean.setUrl(url);
|
|
|
- return uploadBean;
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 下载
|
|
|
- * @param key
|
|
|
- * @return
|
|
|
- */
|
|
|
- public String download(String key) {
|
|
|
- logger.info("oss下载请求: {}", key);
|
|
|
- if(Objects.isNull(key) || key.trim().isEmpty()) {
|
|
|
- return null;
|
|
|
- }
|
|
|
- URL url = ossClient.generatePresignedUrl(bucket, key, this.getExpiration(30)); // 授权有效期30分钟
|
|
|
- String result = url.toString();
|
|
|
- logger.info("oss下载返回: {}", result);
|
|
|
- return result;
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 获取有效期
|
|
|
- * @param minute 分钟
|
|
|
- * @return
|
|
|
- */
|
|
|
- private Date getExpiration(int minute) {
|
|
|
- return new Date(System.currentTimeMillis() + 1000*60*minute);
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
-}
|