Commit 83c656f5 by 邓实川

oss删除文件接口

parent 3a4c114f
...@@ -7,7 +7,9 @@ import java.util.List; ...@@ -7,7 +7,9 @@ import java.util.List;
import org.apache.ibatis.annotations.Param; import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
...@@ -15,6 +17,7 @@ import org.springframework.web.multipart.MultipartFile; ...@@ -15,6 +17,7 @@ import org.springframework.web.multipart.MultipartFile;
import cn.timer.api.config.annotation.CurrentUser; import cn.timer.api.config.annotation.CurrentUser;
import cn.timer.api.config.annotation.UserBean; import cn.timer.api.config.annotation.UserBean;
import cn.timer.api.dto.oss.OssDto;
import cn.timer.api.utils.Result; import cn.timer.api.utils.Result;
import cn.timer.api.utils.ResultUtil; import cn.timer.api.utils.ResultUtil;
import cn.timer.api.utils.aliyun.OSSUtil; import cn.timer.api.utils.aliyun.OSSUtil;
...@@ -113,4 +116,39 @@ public class OSSController { ...@@ -113,4 +116,39 @@ public class OSSController {
return ResultUtil.data(url, "获取成功"); return ResultUtil.data(url, "获取成功");
} }
/***********
* DELETE
*
* @param moudle
************/
@DeleteMapping(value = "/delSingle")
@ApiOperation(value = "删除单个(谨慎使用)", httpMethod = "DELETE", notes = "接口发布说明")
public Result<String> delSingle(@CurrentUser UserBean userBean, @RequestParam(required = false) String moudle,
@RequestParam(required = false) String fileName) {
String path = "8timer2.0/" + userBean.getOrgCode() + "/" + moudle + "/" + fileName;
oss.delSingleFile(path);
return ResultUtil.success("删除成功");
}
@DeleteMapping(value = "/delFiles")
@ApiOperation(value = "删除多个(谨慎使用,谨慎使用)", httpMethod = "DELETE", notes = "接口发布说明")
public Result<List<String>> delFiles(@CurrentUser UserBean userBean, @RequestBody OssDto ossDto) {
List<String> keys = null;
for (String fileName : ossDto.getFileNames()) {
keys = new ArrayList<String>();
String key = "8timer2.0/" + userBean.getOrgCode() + "/" + ossDto.getMoudle() + "/" + fileName;
keys.add(key);
}
List<String> list = oss.delFiles(keys, ossDto.isQuiet());
return ResultUtil.data(list, "删除成功");
}
// @DeleteMapping(value = "/delWithPrefix")
// @ApiOperation(value = "删除指定前缀文件(谨慎使用,谨慎使用,谨慎使用)", httpMethod = "DELETE", notes = "接口发布说明")
// public Result<String> delWithPrefix(@RequestParam String prefix) {
// oss.delPrefixWith(prefix);
// return ResultUtil.success("删除成功");
// }
} }
...@@ -18,6 +18,7 @@ import org.springframework.web.bind.annotation.RequestMapping; ...@@ -18,6 +18,7 @@ import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper; import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.core.metadata.IPage;
...@@ -171,18 +172,21 @@ public class CmsController { ...@@ -171,18 +172,21 @@ public class CmsController {
@ApiOperation(value = "添加/修改企业OA讯息", httpMethod = "POST", notes = "接口发布说明") @ApiOperation(value = "添加/修改企业OA讯息", httpMethod = "POST", notes = "接口发布说明")
public Result<CmsContent> updateqyxx(@CurrentUser UserBean userBean, @RequestBody CmsContentDto cmsContentDto) { public Result<CmsContent> updateqyxx(@CurrentUser UserBean userBean, @RequestBody CmsContentDto cmsContentDto) {
Integer orgCode = userBean.getOrgCode();
Integer empNum = userBean.getEmpNum();
String str = StringUtils.join(cmsContentDto.getFilepath(), ","); // 数组转字符串(逗号分隔)(推荐) String str = StringUtils.join(cmsContentDto.getFilepath(), ","); // 数组转字符串(逗号分隔)(推荐)
CmsContent qynr = new CmsContent(); CmsContent qynr = new CmsContent();
BeanUtil.copyProperties(cmsContentDto, qynr); BeanUtil.copyProperties(cmsContentDto, qynr);
qynr.setFilepath(str); qynr.setFilepath(str);
Integer i = 10000; // Integer i = 10000;
qynr.setXxbh("XX" + i++); // qynr.setXxbh("XX" + i++);
Boolean a = qynr.getId() == null; Boolean a = qynr.getId() == null;
if (a && qynr.getPublisher() == null) if (a && qynr.getPublisher() == null)
qynr.setPublisher(userBean.getEmpNum()); qynr.setPublisher(empNum);
qynr.setOrganizationId(userBean.getOrgCode()); qynr.setOrganizationId(orgCode);
Long now = new Date().getTime(); Long now = new Date().getTime();
// 录入时间 // 录入时间
qynr.setAddeddate(now); qynr.setAddeddate(now);
...@@ -194,6 +198,8 @@ public class CmsController { ...@@ -194,6 +198,8 @@ public class CmsController {
qynr.setReleasetime(qynr.getReleasetime()); qynr.setReleasetime(qynr.getReleasetime());
} }
qynr.setPublisherName(YgglMainEmp.builder().build().selectOne(new LambdaQueryWrapper<YgglMainEmp>()
.eq(YgglMainEmp::getOrgCode, orgCode).eq(YgglMainEmp::getEmpNum, empNum)).getName());
qynr.setReleasetype(fbfs); qynr.setReleasetype(fbfs);
qynr.setStatus(0); qynr.setStatus(0);
qynr.insertOrUpdate(); qynr.insertOrUpdate();
...@@ -225,9 +231,11 @@ public class CmsController { ...@@ -225,9 +231,11 @@ public class CmsController {
@ApiOperation(value = "审核企业OA讯息", httpMethod = "POST", notes = "接口发布说明") @ApiOperation(value = "审核企业OA讯息", httpMethod = "POST", notes = "接口发布说明")
public Result<CmsContent> shqyxx(@CurrentUser UserBean userBean, @RequestBody CmsContent qynr) { public Result<CmsContent> shqyxx(@CurrentUser UserBean userBean, @RequestBody CmsContent qynr) {
Long now = new Date().getTime(); Long now = new Date().getTime();
Integer orgCode = userBean.getOrgCode();
Integer empNum = userBean.getEmpNum();
YgglMainEmp ygglMainEmp = YgglMainEmp.builder().build() YgglMainEmp ygglMainEmp = YgglMainEmp.builder().build().selectOne(new QueryWrapper<YgglMainEmp>().lambda()
.selectOne(new QueryWrapper<YgglMainEmp>().eq("emp_num", userBean.getEmpNum())); .eq(YgglMainEmp::getOrgCode, orgCode).eq(YgglMainEmp::getEmpNum, empNum));
if (ygglMainEmp != null) { if (ygglMainEmp != null) {
qynr.setAuditor(ygglMainEmp.getName());// 审核人 qynr.setAuditor(ygglMainEmp.getName());// 审核人
} }
......
/**
* <p>Title: OssDto.java</p>
* <p>Description: </p>
* @author dsc
* @date 2020年5月11日
* @version 1.0
*/
package cn.timer.api.dto.oss;
import java.io.Serializable;
import java.util.List;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
/**
* <p>
* Title: OssDto.java
* </p>
* <p>
* Description:
* </p>
*
* @author dsc
* @date 2020年5月11日
* @version 1.0
*/
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class OssDto implements Serializable {
private static final long serialVersionUID = 6897868588272725L;
private String moudle;
private List<String> fileNames;
private boolean quiet;
}
...@@ -6,6 +6,7 @@ import java.io.IOException; ...@@ -6,6 +6,7 @@ import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.net.URL; import java.net.URL;
import java.util.ArrayList;
import java.util.Date; import java.util.Date;
import java.util.Iterator; import java.util.Iterator;
import java.util.List; import java.util.List;
...@@ -18,8 +19,13 @@ import org.springframework.web.multipart.commons.CommonsMultipartResolver; ...@@ -18,8 +19,13 @@ import org.springframework.web.multipart.commons.CommonsMultipartResolver;
import com.aliyun.oss.OSS; import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder; import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.DeleteObjectsRequest;
import com.aliyun.oss.model.DeleteObjectsResult;
import com.aliyun.oss.model.GetObjectRequest; import com.aliyun.oss.model.GetObjectRequest;
import com.aliyun.oss.model.ListObjectsRequest;
import com.aliyun.oss.model.OSSObject; import com.aliyun.oss.model.OSSObject;
import com.aliyun.oss.model.OSSObjectSummary;
import com.aliyun.oss.model.ObjectListing;
import com.aliyun.oss.model.PutObjectRequest; import com.aliyun.oss.model.PutObjectRequest;
import cn.hutool.core.util.StrUtil; import cn.hutool.core.util.StrUtil;
...@@ -40,22 +46,22 @@ public class OSSUtil { ...@@ -40,22 +46,22 @@ public class OSSUtil {
@Value("${config-8timer.Aliyun.ACCESSKEY_ID}") @Value("${config-8timer.Aliyun.ACCESSKEY_ID}")
private String accessKeyId; private String accessKeyId;
@Value("${config-8timer.Aliyun.SECRET}") @Value("${config-8timer.Aliyun.SECRET}")
private String accessKeySecret; private String accessKeySecret;
@Value("${config-8timer.Aliyun.bucketName}") @Value("${config-8timer.Aliyun.bucketName}")
private String bucketName; private String bucketName;
@Value("${config-8timer.Aliyun.bucketName_pri}") @Value("${config-8timer.Aliyun.bucketName_pri}")
private String bucketName_pri; private String bucketName_pri;
@Value("${config-8timer.Aliyun.project_package}") @Value("${config-8timer.Aliyun.project_package}")
private String project_package; private String project_package;
@Value("${config-8timer.Aliyun.expirationTime}") @Value("${config-8timer.Aliyun.expirationTime}")
private String expirationTime; private String expirationTime;
@Value("${config-8timer.Aliyun.expirationTime_pri}") @Value("${config-8timer.Aliyun.expirationTime_pri}")
private String expirationTime_pri; private String expirationTime_pri;
...@@ -70,7 +76,7 @@ public class OSSUtil { ...@@ -70,7 +76,7 @@ public class OSSUtil {
// 阿里云主账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维,请登录 // 阿里云主账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维,请登录
// https://ram.console.aliyun.com 创建RAM账号。 // https://ram.console.aliyun.com 创建RAM账号。
// 创建OSSClient实例。 // 创建OSSClient实例。
OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret); OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
// 创建存储空间。 // 创建存储空间。
ossClient.createBucket(bucketName); ossClient.createBucket(bucketName);
...@@ -328,6 +334,107 @@ public class OSSUtil { ...@@ -328,6 +334,107 @@ public class OSSUtil {
return url.toString(); return url.toString();
} }
/**
* 删除单个
*
* @param objectName 文件名
*/
public void delSingleFile(String objectName) {
// // Endpoint以杭州为例,其它Region请按实际情况填写。
// String endpoint = "http://oss-cn-hangzhou.aliyuncs.com";
// // 阿里云主账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维,请登录 https://ram.console.aliyun.com 创建RAM账号。
// String accessKeyId = "<yourAccessKeyId>";
// String accessKeySecret = "<yourAccessKeySecret>";
// String bucketName = "<yourBucketName>";
// String objectName = "<yourObjectName>";
// 创建OSSClient实例。
OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
// 删除文件。如需删除文件夹,请将ObjectName设置为对应的文件夹名称。如果文件夹非空,则需要将文件夹下的所有object删除后才能删除该文件夹。
ossClient.deleteObject(bucketName, objectName);
// 关闭OSSClient。
ossClient.shutdown();
}
/**
* 删除多个文件
* @param keys 需要删除的文件。
* @param quiet 返回模式。true表示简单模式,false表示详细模式。默认为详细模式。
* @return 删除结果。详细模式下为删除成功的文件列表,简单模式下为删除失败的文件列表。
*/
public List<String> delFiles(List<String> keys, boolean quiet) {
// // Endpoint以杭州为例,其它Region请按实际情况填写。
// String endpoint = "http://oss-cn-hangzhou.aliyuncs.com";
// // 阿里云主账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维,请登录 https://ram.console.aliyun.com 创建RAM账号。
// String accessKeyId = "<yourAccessKeyId>";
// String accessKeySecret = "<yourAccessKeySecret>";
// String bucketName = "<yourBucketName>";
// 创建OSSClient实例。
OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
// 删除文件。key等同于ObjectName,表示删除OSS文件时需要指定包含文件后缀在内的完整路径,例如abc/efg/123.jpg。
// List<String> keys = new ArrayList<String>();
// keys.add("key0");
// keys.add("key1");
// keys.add("key2");
DeleteObjectsResult deleteObjectsResult = ossClient
.deleteObjects(new DeleteObjectsRequest(bucketName).withKeys(keys).withQuiet(quiet));
List<String> deletedObjects = deleteObjectsResult.getDeletedObjects();
// 关闭OSSClient。
ossClient.shutdown();
return deletedObjects;
}
/**
* 删除指定前缀(prefix)的文件
* @param prefix
*/
public void delPrefixWith(String prefix) {
// // Endpoint以杭州为例,其它Region请按实际情况填写。
// String endpoint = "http://oss-cn-hangzhou.aliyuncs.com";
// // 阿里云主账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维,请登录
// // https://ram.console.aliyun.com 创建RAM账号。
// String accessKeyId = "<yourAccessKeyId>";
// String accessKeySecret = "<yourAccessKeySecret>";
// String bucketName = "<yourBucketName>";
//
// // 指定前缀。
// final String prefix = "<yourkeyPrefix>";
// 创建OSSClient实例。
OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
// 列举所有包含指定前缀的文件并删除。
String nextMarker = null;
ObjectListing objectListing = null;
do {
ListObjectsRequest listObjectsRequest = new ListObjectsRequest(bucketName).withPrefix(prefix)
.withMarker(nextMarker);
objectListing = ossClient.listObjects(listObjectsRequest);
if (objectListing.getObjectSummaries().size() > 0) {
List<String> keys = new ArrayList<String>();
for (OSSObjectSummary s : objectListing.getObjectSummaries()) {
System.out.println("key name: " + s.getKey());
keys.add(s.getKey());
}
DeleteObjectsRequest deleteObjectsRequest = new DeleteObjectsRequest(bucketName).withKeys(keys);
ossClient.deleteObjects(deleteObjectsRequest);
}
nextMarker = objectListing.getNextMarker();
} while (objectListing.isTruncated());
// 关闭OSSClient。
ossClient.shutdown();
}
/* /*
* (跨域资源) // Endpoint以杭州为例,其它Region请按实际情况填写。 String endpoint = * (跨域资源) // Endpoint以杭州为例,其它Region请按实际情况填写。 String endpoint =
* "http://oss-cn-hangzhou.aliyuncs.com"; // * "http://oss-cn-hangzhou.aliyuncs.com"; //
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment