com.hn.upload.AliUpload Maven / Gradle / Ivy
package com.hn.upload;
import cn.hutool.core.date.DatePattern;
import cn.hutool.core.util.StrUtil;
import com.aliyun.oss.ClientException;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.OSSException;
import com.aliyun.oss.common.utils.BinaryUtil;
import com.aliyun.oss.model.MatchMode;
import com.aliyun.oss.model.PolicyConditions;
import com.hn.config.HnConfigUtils;
import com.hn.config.exception.ConfigException;
import com.hn.utils.AssertUtils;
import lombok.Data;
import java.io.File;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.Map;
/**
* 描述:
* 阿里云存储
*
* Api: https://help.aliyun.com/document_detail/31948.html?spm=a2c4g.11186623.2.18.4fbe69cb6DJfdE
* java sdk: https://help.aliyun.com/document_detail/32011.html?spm=a2c4g.11186623.2.23.669f1c62HV0ziO#concept-32011-zh
*
* @author fei
* 2019-12-20 09:06
*/
public class AliUpload extends AbstractFileUpload {
private Param param;
public static Param createParam() {
return new Param();
}
public AliUpload(Param param) {
checkParam(param);
this.param = param;
}
public AliUpload(String scene) {
Param param = getRequestParam(scene);
checkParam(param);
this.param = param;
}
@Override
public String getFileBaseUrl() {
return param.fileUrl;
}
@Override
public String getTempDownUrl(String key, Integer expireTime) {
OSS ossClient = getOssClient();
Date expiration = new Date(System.currentTimeMillis() + expireTime * 60L * 1000L);
URL url = ossClient.generatePresignedUrl(param.bucket, key, expiration);
ossClient.shutdown();
return String.valueOf(url);
}
/**
* 文件上传
*
* @param file 上传的文件
* @param objectName 上传文件到OSS时需要指定包含文件后缀在内的完整路径,例如abc/efg/123.jpg。
* @return objectName
*/
@Override
public String uploadFile(File file, String objectName) {
OSS ossClient = getOssClient();
ossClient.putObject(param.bucket, objectName, file);
ossClient.shutdown();
return objectName;
}
/**
* 文件上传
*
* @param is 上传的文件流
* @param objectName 上传文件到OSS时需要指定包含文件后缀在内的完整路径,例如abc/efg/123.jpg。
* @return objectName
*/
@Override
public String uploadFile(InputStream is, String objectName) {
// 创建OSSClient实例。
OSS ossClient = getOssClient();
// 上传内容到指定的存储空间(bucketName)并保存为指定的文件名称(objectName)。
ossClient.putObject(param.bucket, objectName, is);
// 关闭OSSClient。
ossClient.shutdown();
return objectName;
}
@Override
public void deleteFile(String key) {
OSS ossClient = getOssClient();
ossClient.deleteObject(param.bucket, key);
}
/**
* 直传
* 文档地址
* 前端代码参考
*
* @return map
*/
@Override
public Object getToken() {
String dir = DatePattern.PURE_DATE_FORMAT.format(new Date()) + "/";
return getToken(dir);
}
@Override
public Object getToken(String dir) {
OSS client = getOssClient();
long expireTime = 30;
long expireEndTime = System.currentTimeMillis() + expireTime * 1000;
Date expiration = new Date(expireEndTime);
PolicyConditions policyConditions = new PolicyConditions();
policyConditions.addConditionItem(PolicyConditions.COND_CONTENT_LENGTH_RANGE, 0, 1048576000);
policyConditions.addConditionItem(MatchMode.StartWith, PolicyConditions.COND_KEY, dir);
String postPolicy = client.generatePostPolicy(expiration, policyConditions);
byte[] binaryData = new byte[0];
try {
binaryData = postPolicy.getBytes("utf-8");
} catch (UnsupportedEncodingException e) {
throw new UploadException("阿里云上传失败:", e);
}
String encodedPolicy = BinaryUtil.toBase64String(binaryData);
String postSignature = client.calculatePostSignature(postPolicy);
Map respMap = new LinkedHashMap<>();
respMap.put("OSSAccessKeyId", param.accessKey);
respMap.put("policy", encodedPolicy);
respMap.put("signature", postSignature);
respMap.put("dir", dir);
respMap.put("host", param.fileUrl);
respMap.put("expire", String.valueOf(expireEndTime / 1000));
return respMap;
}
private OSS getOssClient() {
return new OSSClientBuilder().build(param.endpoint, param.accessKey, param.secretKey);
}
private void checkParam(Param param) {
AssertUtils.notNull(param, "param is null");
AssertUtils.notNull(param.endpoint, ConfigException.exception("阿里云存储endpoint未配置"));
AssertUtils.notNull(param.accessKey, ConfigException.exception("阿里云存储accessKey未配置"));
AssertUtils.notNull(param.secretKey, ConfigException.exception("阿里云存储secretKey未配置"));
AssertUtils.notNull(param.bucket, ConfigException.exception("阿里云存储bucketName未配置"));
AssertUtils.notNull(param.fileUrl, ConfigException.exception("阿里云存储fileUrl未配置"));
}
/**
* 配置前缀名
*/
private static final String CONFIG_KEY = "upload.ali";
/**
* 获取数据配置
*
* @return Param
*/
private Param getRequestParam(String scene) {
String configKey = CONFIG_KEY;
if (StrUtil.isNotBlank(scene)) {
configKey = configKey.concat("-").concat(scene);
}
String endpoint = HnConfigUtils.getConfig(configKey.concat(".endpoint"));
String accessKey = HnConfigUtils.getConfig(configKey.concat(".accessKey"));
String secretKey = HnConfigUtils.getConfig(configKey.concat(".secretKey"));
String bucket = HnConfigUtils.getConfig(configKey.concat(".bucket"));
String fileUrl = HnConfigUtils.getConfig(configKey.concat(".fileUrl"));
Param param = new Param();
// 从数据库读取配置
param.setEndpoint(endpoint);
param.setAccessKey(accessKey);
param.setSecretKey(secretKey);
param.setBucket(bucket);
param.setFileUrl(fileUrl);
return param;
}
@Data
public static class Param {
/**
* 【阿里云存储】endpoint
*/
private String endpoint;
/**
* 【阿里云存储】accessKey
*/
private String accessKey;
/**
* 【阿里云存储】secretKey
*/
private String secretKey;
/**
* 【阿里云存储】bucket
*/
private String bucket;
/**
* 【阿里云存储】fileUrl
*/
private String fileUrl;
}
}