增加oss文件删除接口
This commit is contained in:
parent
6e82c17f72
commit
079a119ccf
@ -50,11 +50,24 @@ public class OssFileController {
|
||||
public HttpResult upload(@RequestParam("file") MultipartFile multipartFile, String deviceId) throws Exception {
|
||||
System.out.println("upload device image:" + multipartFile.getOriginalFilename());
|
||||
if (StringUtils.isEmpty(deviceId)) {
|
||||
HttpResult.fail("设备编号不能为空");
|
||||
return HttpResult.fail("设备编号不能为空");
|
||||
}
|
||||
|
||||
String filename = deviceId + "_" + UUID.randomUUID().toString().replace("-", "") + ".jpg";
|
||||
String url = aliyunOssUtils.uploadFile(multipartFile, filename, "device-img");
|
||||
return HttpResult.success(url);
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除oss上面的文件
|
||||
*
|
||||
* @param url 图片访问链接
|
||||
* @return HttpResult
|
||||
*/
|
||||
@RequestMapping(value = "/delete")
|
||||
public HttpResult delete(String url) {
|
||||
System.out.println("delete file:" + url);
|
||||
aliyunOssUtils.deleteFile(url);
|
||||
return HttpResult.success(null);
|
||||
}
|
||||
}
|
||||
|
||||
@ -79,4 +79,21 @@ public class AliyunOssUtils {
|
||||
return ossProperties.getPublicReadUrl() + "/" + directory + "/" + filename;
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除文件
|
||||
*
|
||||
* @param url 图片访问链接
|
||||
*/
|
||||
public void deleteFile(String url) {
|
||||
try {
|
||||
String prefixUrl = ossProperties.getPublicReadUrl() + "/";
|
||||
String key = url.replace(prefixUrl, "");
|
||||
OSS ossClient = new OSSClientBuilder().build(ossProperties.getEndpoint(), ossProperties.getAccessKey(), ossProperties.getAccessSecret());
|
||||
ossClient.deleteObject(ossProperties.getBucket(), key);
|
||||
ossClient.shutdown();
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException("删除文件失败,请检查配置信息:[" + e.getMessage() + "]");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user