添加阿里云文件上传的删除机制

This commit is contained in:
udf_admin@163.com
2018-01-19 23:53:30 +08:00
parent d3695a27f5
commit 6ab7efefd6
2 changed files with 47 additions and 4 deletions
@@ -6,13 +6,16 @@ import com.aliyun.oss.model.ListObjectsRequest;
import com.aliyun.oss.model.OSSObjectSummary;
import com.aliyun.oss.model.ObjectListing;
import com.aliyun.oss.model.PolicyConditions;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.itkk.udf.cache.redis.CacheRedisProperties;
import org.itkk.udf.core.ApplicationConfig;
import org.itkk.udf.core.exception.ParameterValidException;
import org.itkk.udf.file.aliyun.oss.api.domain.PolicyResult;
import org.itkk.udf.file.aliyun.oss.api.meta.AliyunOssAccessMeta;
import org.itkk.udf.file.aliyun.oss.api.meta.AliyunOssPathMeta;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import java.io.File;
@@ -22,6 +25,7 @@ import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Set;
/**
* OssWarpper
@@ -29,6 +33,12 @@ import java.util.List;
@Component
public class OssWarpper {
/**
* UPLOAD_FILE_CACHE_KEY
*/
private static final String UPLOAD_FILE_CACHE_KEY = ":upload_file";
/**
* MSG_1
*/
@@ -66,6 +76,18 @@ public class OssWarpper {
@Autowired
private AliyunOssProperties aliyunOssProperties;
/**
* redisTemplate
*/
@Autowired
private RedisTemplate<Object, Object> redisTemplate;
/**
* cacheRedisProperties
*/
@Autowired
private CacheRedisProperties cacheRedisProperties;
/**
* list
*
@@ -255,12 +277,33 @@ public class OssWarpper {
//实例化oss对象
OSSClient client = new OSSClient(auth.getEndPoint(), auth.getAccessId(), auth.getAccessKey());
try {
//上传
String objectKey = "upload/" + new SimpleDateFormat("yyyy-MM-dd").format(new Date()) + "/" + file.getName();
client.putObject(path.getBucketName(), objectKey, file);
//记录
String key = code + UPLOAD_FILE_CACHE_KEY;
redisTemplate.opsForHash().put(key, objectKey, null);
//返回
return objectKey;
} finally {
client.shutdown();
}
}
/**
* 清理文件
*
* @param code code
*/
public void cleanUploadFile(String code) {
String key = code + UPLOAD_FILE_CACHE_KEY;
Set<Object> fileMap = redisTemplate.opsForHash().keys(key);
if (CollectionUtils.isNotEmpty(fileMap)) {
fileMap.forEach(objectKey -> {
this.delete(code, objectKey.toString());
redisTemplate.opsForHash().delete(key, objectKey);
});
}
}
}
@@ -42,14 +42,14 @@ public class DownLoadProcessAspect {
public static final String DOWNLOAD_CACHE_PREFIX = "DOWNLOAD_CACHE:";
/**
* DOWNLOAD_WAITING_CACHE_EXPIRATION(待执行,执行中,缓存过期时间为1天)
* DOWNLOAD_WAITING_CACHE_EXPIRATION(待执行,执行中,缓存过期时间为1小时)
*/
public static final long DOWNLOAD_WAITING_CACHE_EXPIRATION = 60L * 24L;
public static final long DOWNLOAD_WAITING_CACHE_EXPIRATION = 60L * 1L;
/**
* DOWNLOAD_CACHE_EXPIRATION(执行完成,执行错误,缓存过期时间为10分钟)
* DOWNLOAD_CACHE_EXPIRATION(执行完成,执行错误,缓存过期时间为1分钟)
*/
public static final long DOWNLOAD_CACHE_EXPIRATION = 10;
public static final long DOWNLOAD_CACHE_EXPIRATION = 1;
/**
* redisTemplate