1、文件服务阿里云oss文件删除接口实现
This commit is contained in:
parent
53d337c1aa
commit
7a374dee0d
|
|
@ -1,9 +1,13 @@
|
|||
package com.ruoyi.file.controller;
|
||||
|
||||
import io.swagger.annotations.Api;
|
||||
import io.swagger.annotations.ApiOperation;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.DeleteMapping;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
import com.ruoyi.common.core.domain.R;
|
||||
|
|
@ -17,6 +21,7 @@ import com.ruoyi.system.api.domain.SysFile;
|
|||
* @author ruoyi
|
||||
*/
|
||||
@RestController
|
||||
@Api(tags = "文件模块")
|
||||
public class SysFileController
|
||||
{
|
||||
private static final Logger log = LoggerFactory.getLogger(SysFileController.class);
|
||||
|
|
@ -45,4 +50,20 @@ public class SysFileController
|
|||
return R.fail(e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* 删除文件
|
||||
* </p>
|
||||
* @author xjs
|
||||
* @since 2022-03-02
|
||||
* @param url 文件路径
|
||||
* @return R
|
||||
*/
|
||||
@ApiOperation("删除文件")
|
||||
@DeleteMapping("/remove")
|
||||
public R removeFile(@RequestParam("url") String url) {
|
||||
sysFileService.removeFile(url);
|
||||
return R.ok();
|
||||
}
|
||||
}
|
||||
|
|
@ -1,9 +1,9 @@
|
|||
package com.ruoyi.file.service;
|
||||
|
||||
import cn.hutool.core.date.DateUtil;
|
||||
import cn.hutool.core.util.RandomUtil;
|
||||
import com.aliyun.oss.OSS;
|
||||
import com.aliyun.oss.OSSClientBuilder;
|
||||
import com.ruoyi.common.core.text.UUID;
|
||||
import com.ruoyi.file.config.AliyunOssProperties;
|
||||
import com.ruoyi.file.utils.FileUploadUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
|
|
@ -26,6 +26,11 @@ import java.util.Date;
|
|||
@Primary
|
||||
public class AliyunOssFileServiceImpl implements ISysFileService {
|
||||
|
||||
public static final String HTTPS = "https://";
|
||||
|
||||
public static final String DOT = ".";
|
||||
|
||||
public static final String SLASH = "/";
|
||||
|
||||
@Autowired
|
||||
private AliyunOssProperties aliyunOssProperties;
|
||||
|
|
@ -35,29 +40,43 @@ public class AliyunOssFileServiceImpl implements ISysFileService {
|
|||
Assert.notNull(file, "file is null");
|
||||
try {
|
||||
String endpoint = aliyunOssProperties.getEndpoint();
|
||||
String keyId = aliyunOssProperties.getKeyId();
|
||||
String keySecret = aliyunOssProperties.getKeySecret();
|
||||
String bucketName = aliyunOssProperties.getBucketName();
|
||||
OSS ossClient = new OSSClientBuilder().build(endpoint,
|
||||
keyId, keySecret);
|
||||
OSS ossClient = this.getOssClient();
|
||||
//获取流
|
||||
InputStream is = file.getInputStream();
|
||||
//获取文件后缀
|
||||
String extension = FileUploadUtils.getExtension(file);
|
||||
//获取文件名称
|
||||
String fileName = getDataTime() + "." + extension;
|
||||
String fileName = this.getDataTime() + DOT + extension;
|
||||
//执行文件上传 bucket名称 文件名称 文件流
|
||||
ossClient.putObject(bucketName, fileName, is);
|
||||
//关闭ossClient
|
||||
ossClient.shutdown();
|
||||
//拼接文件地址
|
||||
return "https://" + bucketName + "." + endpoint + "/" + fileName;
|
||||
return HTTPS + bucketName + DOT + endpoint + SLASH + fileName;
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void removeFile(String url) {
|
||||
String endpoint = aliyunOssProperties.getEndpoint();
|
||||
String bucketName = aliyunOssProperties.getBucketName();
|
||||
String host = HTTPS + bucketName + DOT + endpoint + SLASH;
|
||||
|
||||
String objectName = url.substring(host.length());
|
||||
OSS ossClient = this.getOssClient();
|
||||
|
||||
//执行删除
|
||||
ossClient.deleteObject(bucketName, objectName);
|
||||
|
||||
//关闭ossClient
|
||||
ossClient.shutdown();
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成一个当前日期文件名
|
||||
*
|
||||
|
|
@ -65,9 +84,19 @@ public class AliyunOssFileServiceImpl implements ISysFileService {
|
|||
*/
|
||||
private String getDataTime() {
|
||||
String today = DateUtil.format(new Date(), "yyyy-MM");
|
||||
String time = DateUtil.formatDateTime(new Date());
|
||||
int random = RandomUtil.randomInt(100, 10000);
|
||||
//防止同一时间生成文件名重复
|
||||
return today + "/" + time + "-" + random;
|
||||
return today + SLASH + UUID.randomUUID();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取oss实例
|
||||
*
|
||||
* @return OSS
|
||||
*/
|
||||
private OSS getOssClient() {
|
||||
String endpoint = aliyunOssProperties.getEndpoint();
|
||||
String keyId = aliyunOssProperties.getKeyId();
|
||||
String keySecret = aliyunOssProperties.getKeySecret();
|
||||
return new OSSClientBuilder().build(endpoint,
|
||||
keyId, keySecret);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -39,4 +39,9 @@ public class FastDfsSysFileServiceImpl implements ISysFileService
|
|||
FilenameUtils.getExtension(file.getOriginalFilename()), null);
|
||||
return domain + "/" + storePath.getFullPath();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void removeFile(String url) {
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -36,4 +36,9 @@ public class FtpSysFileServiceImpl implements ISysFileService{
|
|||
}
|
||||
return url;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void removeFile(String url) {
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -17,4 +17,10 @@ public interface ISysFileService
|
|||
* @throws Exception
|
||||
*/
|
||||
public String uploadFile(MultipartFile file) throws Exception;
|
||||
|
||||
/**
|
||||
* 文件删除接口
|
||||
* @param url 文件路径
|
||||
*/
|
||||
void removeFile(String url);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -45,4 +45,9 @@ public class LocalSysFileServiceImpl implements ISysFileService
|
|||
String url = domain + localFilePrefix + name;
|
||||
return url;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void removeFile(String url) {
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -42,4 +42,9 @@ public class MinioSysFileServiceImpl implements ISysFileService
|
|||
client.putObject(args);
|
||||
return minioConfig.getUrl() + "/" + minioConfig.getBucketName() + "/" + fileName;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void removeFile(String url) {
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue