当前位置:   article > 正文

springboot对象存储亚马逊aws集成(s3协议)_ozone文件上传springboot aws

ozone文件上传springboot aws
  1. POM文件

  1. <!-- AmazonS3对象存储 -->
  2. <dependency>
  3. <groupId>com.amazonaws</groupId>
  4. <artifactId>aws-java-sdk-s3</artifactId>
  5. <version>1.11.76</version>
  6. </dependency>
  7. <dependency>
  8. <groupId>com.amazonaws</groupId>
  9. <artifactId>aws-java-sdk-core</artifactId>
  10. <version>1.11.779</version>
  11. </dependency>

上传upload

  1. /**
  2. * 收集并上传至S3服务器
  3. * 返回ID
  4. * @param identify
  5. * @param fileInput
  6. * @return
  7. */
  8. public OperationResult upload(@RequestParam(required=false) String identify, @RequestParam("fileInput") MultipartFile fileInput)
  9. {
  10. if (Objects.isNull(fileInput)) {
  11. String errorMsg = "fileInput参数为空";
  12. logger.error(errorMsg);
  13. return OperationResult.buildFailureResult(errorMsg);
  14. }
  15. String fileName = fileInput.getOriginalFilename();
  16. logger.error("-----fileName-----"+fileName);
  17. if (StringUtils.isBlank(fileName)) {
  18. String errorMsg = "文件名称为空";
  19. logger.error(errorMsg);
  20. return OperationResult.buildFailureResult(errorMsg);
  21. }
  22. String ext = fileName.substring(fileName.lastIndexOf(".") + 1);
  23. String realName = String.format("%s.%s", new Object[] { IDUtil.createUUID(), ext });
  24. try { InputStream in = fileInput.getInputStream(); Throwable localThrowable3 = null;
  25. try { identify = org.springframework.util.StringUtils.cleanPath(identify);
  26. ObjectMetadata objectMetadata = new ObjectMetadata();
  27. objectMetadata.setContentLength(fileInput.getSize());
  28. Map map = new HashMap(1);
  29. map.put("fileName", URLEncoder.encode(fileName, StandardCharsets.UTF_8.toString()));
  30. objectMetadata.setUserMetadata(map);
  31. this.upload(identify, in, objectMetadata);
  32. Map resultMap = new HashMap(1);
  33. // resultMap.put("identify", identify);
  34. logger.error("-----fileId-----"+identify);
  35. OperationResult localOperationResult = OperationResult.buildSuccessResult("对象存储上传文件成功", resultMap);
  36. return localOperationResult;
  37. }
  38. catch (Throwable localThrowable1)
  39. {
  40. localThrowable3 = localThrowable1; throw localThrowable1;
  41. }
  42. finally
  43. {
  44. if (in != null) if (localThrowable3 != null) try { in.close(); } catch (Throwable localThrowable2) { localThrowable3.addSuppressed(localThrowable2); } else in.close();
  45. }
  46. } catch (Exception e) {
  47. String errorMsg = "对象存储上传文件失败";
  48. logger.error(errorMsg, e);
  49. return OperationResult.buildFailureResult("对象存储上传文件失败");
  50. }
  51. }

下载download

  1. /**
  2. * 文件下载
  3. * @param request
  4. * @param
  5. * @param
  6. * @return
  7. */
  8. public Object fileDownloadByFileId(HttpServletRequest request, HttpServletResponse response,String storeId , String orgfileName) throws Exception {
  9. ResponseEntity entity=null;
  10. if(StringUtils.isEmpty(storeId)){
  11. throw new Exception("请上传文件信息");
  12. }
  13. if(StringUtils.isEmpty(orgfileName)){
  14. throw new Exception("请上传文件名信息");
  15. }
  16. try {
  17. ByteArrayOutputStream bos = new ByteArrayOutputStream(); Throwable localThrowable6 = null;
  18. try { InputStream in = download(storeId); Throwable localThrowable7 = null;
  19. try { ObjectMetadata objectMetadata = getObjectMetadata(storeId);
  20. long contentLength = objectMetadata.getContentLength();
  21. String fileName =orgfileName==null? URLDecoder.decode(objectMetadata.getUserMetaDataOf("fileName"), StandardCharsets.UTF_8.toString()):orgfileName;
  22. HttpHeaders headers = new HttpHeaders();
  23. headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
  24. headers.add("Accept-Ranges", "bytes");
  25. headers.add("Content-Length", Long.toString(contentLength));
  26. headers.add("Content-Disposition", "attachment; filename='" + fileName + "'; filename*=utf-8''" + fileName);
  27. IOUtils.copy(in, bos);
  28. entity = new ResponseEntity(bos.toByteArray(), headers, HttpStatus.OK);
  29. } catch (Throwable localThrowable1) {
  30. localThrowable7 = localThrowable1; throw localThrowable1;
  31. } finally{
  32. if (in != null) if (localThrowable7 != null) try { in.close(); } catch (Throwable localThrowable2) { localThrowable7.addSuppressed(localThrowable2); } else in.close();
  33. }
  34. }catch (Throwable localThrowable4){
  35. localThrowable6 = localThrowable4; throw localThrowable4;
  36. }finally{
  37. if (bos != null) if (localThrowable6 != null) try { bos.close(); } catch (Throwable localThrowable5) { localThrowable6.addSuppressed(localThrowable5); } else bos.close();
  38. }
  39. }catch (AmazonS3Exception e){
  40. String errorMsg = "对象存储连接失败或者identify对应的文件不存在";
  41. logger.error(errorMsg, e);
  42. return OperationResult.buildFailureResult(errorMsg);
  43. }catch (Exception e) {
  44. String errorMsg = "对象存储下载文件失败";
  45. logger.error(errorMsg, e);
  46. return OperationResult.buildFailureResult(errorMsg, e.getMessage());
  47. }
  48. return entity;
  49. }

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

闽ICP备14008679号