当前位置:   article > 正文

阿里云对象存储OSS批量上传,单个上传,批量删除,单个删除!

阿里云对象存储OSS批量上传,单个上传,批量删除,单个删除!

请自行替换秘钥:

#阿里云 OSS src/main/resources/application.properties
#不同的服务器,地址不同
aliyun.oss.file.endpoint=https://oss-cn-hangzhou.aliyuncs.com
aliyun.oss.file.accessKeyId=LTAI5t9wUqCoD42qPGRy8S
aliyun.oss.file.accessKeySecret=WV9W6erbdF2oqH4KIlo5BzNrwaP
#bucket可以在控制台创建,也可以使用java代码创建
aliyun.oss.file.bucketName=zhurenjin-workspace

多个文件批量上传,单个上传,工具类:

  1. public Map<String, Object> Upload(MultipartFile file) throws IOException {
  2. InputStream inputStream = file.getInputStream();
  3. // 避免文件覆盖
  4. String originalFilename = file.getOriginalFilename();
  5. int iPoint = originalFilename.lastIndexOf(".");
  6. String fileTypePre = originalFilename.substring(0,iPoint);
  7. String fileType = originalFilename.substring(iPoint);
  8. String fileName = UUID.randomUUID().toString()+fileTypePre+fileType;
  9. OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId,accessKeySecret);
  10. // PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, fileName, inputStream);
  11. // 创建PutObject请求。
  12. ossClient.putObject(bucketName,fileName,inputStream);
  13. // https://zhurenjin-workspace.oss-cn-hangzhou.aliyuncs.com/exampledir/exampleobject.jpg
  14. String imgUrl = endpoint.split("//")[0] + "//" + bucketName + "." + endpoint.split("//")[1] + "/"+fileName;
  15. ossClient.shutdown();
  16. Map<String ,Object> re = new HashMap<String ,Object>();
  17. Map<String ,Object> imgList = new HashMap<String ,Object>();
  18. imgList.put("imageUrl",imgUrl);
  19. re.put("code",200);
  20. re.put("msg","上传成功");
  21. re.put("data",imgList);
  22. return re;
  23. }

单个文件上传

  1. @PostMapping("uploadAliyunOSSByUtils")
  2. public AjaxResult uploadAliyunOSSByUtils( @RequestParam("files") MultipartFile img) throws IOException {
  3. if (!(img.getOriginalFilename().endsWith(".png")) && !(img.getOriginalFilename().endsWith(".jpg")) && !(img.getOriginalFilename().endsWith(".PNG"))) {
  4. try {
  5. return AjaxResult.error("文件类型错误,只能为png或者jpg");
  6. } catch (Exception e) {
  7. throw new RuntimeException(e);
  8. }
  9. }
  10. // 调用阿里云工具类进行文件上传
  11. AliOSSUtils aliOSSUtils = new AliOSSUtils();
  12. Map<String, Object> upload = aliOSSUtils.Upload(img);
  13. return AjaxResult.success(upload);
  14. }

多个文件上传

  1. // 阿里云存储(多张图片)
  2. @PostMapping("uploadAliyunOSSByUtilsArrayFils")
  3. public AjaxResult uploadAliyunOSSByUtilsArrayFils( MultipartFile[] imgList) throws IOException {
  4. Map<String, Object> objectObjectHashMap = new HashMap<>();
  5. // List<Object> objectsImages = new ArrayList<>();
  6. List<Map<String, Object>> objectsImages = new ArrayList<>();
  7. // 调用阿里云工具类进行文件上传
  8. for (MultipartFile img : imgList){
  9. Map<String, Object> uploadImd = aliOSSUtils.Upload(img);
  10. Map<String, Object> New_ = new HashMap<>();
  11. New_.put("strImg",uploadImd);
  12. objectsImages.add(New_);
  13. // objectsImages.add(uploadImd);
  14. }
  15. return AjaxResult.success(objectsImages);
  16. }

 单个删除

  1. // 阿里云删除单张图片
  2. @PostMapping("deleteAliyunOSSByUtils")
  3. public AjaxResult deleteAliyunOSSByUtils(@RequestBody Map<String,String> img) throws IOException {
  4. // 创建OSSClient实例。
  5. System.out.println(img);
  6. boolean b = aliOSSUtils.deleteFile(img);
  7. return AjaxResult.success(b);
  8. }

多个删除

  1. @PostMapping("deleteAliyunOSSByUtilsArrayList")
  2. public AjaxResult deleteAliyunOSSByUtilsArrayList(@RequestBody List<String> imgList) throws IOException {
  3. // 创建OSSClient实例。
  4. boolean b = aliOSSUtils.deleteAllFile(imgList);
  5. return AjaxResult.success(b);
  6. }

工具类

  1. /*删除工具类《单个》*/
  2. public boolean deleteFile(Map<String,String> fileUrl){
  3. OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
  4. String img = fileUrl.get("img");
  5. int begin = (endpoint.split("//")[0] + "//" + bucketName + "." + endpoint.split("//")[1] + "/").length(); //找到文件路径的开始下标
  6. String deleteUrl = img.substring(begin);
  7. try {
  8. // 删除文件请求
  9. VoidResult voidResult = ossClient.deleteObject(bucketName, deleteUrl);
  10. System.out.println(voidResult+"删除chengg11111??222");
  11. return true;
  12. } catch (Exception e) {
  13. e.printStackTrace();
  14. return false;
  15. } finally {
  16. if (ossClient != null) {
  17. ossClient.shutdown();
  18. }
  19. }
  20. }
  21. /*删除工具类《多个》,主要就是要获取到相应的文件后缀,String deleteUrl = objImg.substring(begin);*/
  22. public boolean deleteAllFile(List<String> keys){
  23. OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
  24. List<String> keysBox= new ArrayList<>();
  25. int begin = (endpoint.split("//")[0] + "//" + bucketName + "." + endpoint.split("//")[1] + "/").length(); //找到文件路径的开始下标
  26. for(String objImg : keys) {
  27. String deleteUrl = objImg.substring(begin);
  28. keysBox.add(deleteUrl);
  29. }
  30. try {
  31. DeleteObjectsResult deleteObjectsResult = ossClient.deleteObjects(new DeleteObjectsRequest(bucketName).withKeys(keysBox).withEncodingType("url"));
  32. List<String> deletedObjects = deleteObjectsResult.getDeletedObjects();
  33. try {
  34. for(String obj : deletedObjects) {
  35. String deleteObj = URLDecoder.decode(obj, "UTF-8");
  36. System.out.println(deleteObj);
  37. }
  38. } catch (UnsupportedEncodingException e) {
  39. e.printStackTrace();
  40. }
  41. return true;
  42. } catch (Exception e) {
  43. e.printStackTrace();
  44. return false;
  45. } finally {
  46. if (ossClient != null) {
  47. ossClient.shutdown();
  48. }
  49. }
  50. }

学以致用,多做记录

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Gausst松鼠会/article/detail/434910
推荐阅读
相关标签
  

闽ICP备14008679号