spring引导多部分上载不适用于aws ec2安装程序,多部分/表单数据请求处理失败设备上没有剩余空间

bkkx9g8r  于 2021-10-10  发布在  Java
关注(0)|答案(0)|浏览(259)

以下是我的spring引导代码,用于将文件上载到aws s3:

  1. @RequestMapping(method = RequestMethod.POST, value = "/scripts/upload")
  2. public UploadStatus uploadFile(@RequestParam("file") MultipartFile file) {
  3. return storageService.uploadFile(file);
  4. }

这是我的服务代码:

  1. public UploadStatus uploadFile(MultipartFile file) {
  2. LOGGER.warn("Starting to upload the file");
  3. return uploadFileById(file, null);
  4. }
  5. public UploadStatus uploadFileById(MultipartFile file, String fileId) {
  6. LOGGER.warn("Starting to upload in by ID method for the file");
  7. try {
  8. if (file.isEmpty()) {
  9. throw new StorageException("Failed to store an empty file");
  10. }
  11. Regions regions = Regions.valueOf(S3_REGION);
  12. String filename = file.getOriginalFilename();
  13. AmazonS3 s3Client = AmazonS3ClientBuilder.standard()
  14. .withRegion(regions)
  15. .withCredentials(new ProfileCredentialsProvider())
  16. .build();
  17. String fileUUId;
  18. LOGGER.warn("Verifying the filename: " + filename);
  19. if (fileId == null) {
  20. fileUUId = UUID.randomUUID().toString();
  21. } else {
  22. fileUUId = fileId;
  23. Optional<FileUpload> optional = fileUploadRepository.findById(fileId);
  24. if (optional.isPresent()) {
  25. } else {
  26. throw new StorageException("The fileId you sent is not present. Please send a valid fileId");
  27. }
  28. deleteFile(fileId);
  29. }
  30. InputStream inputStream = file.getInputStream();
  31. ObjectMetadata metadata = new ObjectMetadata();
  32. metadata.setContentLength(inputStream.available());
  33. LOGGER.warn("Verifying the filename: " + file.getName());
  34. int maxUploadThreads = 5;
  35. TransferManager tm = TransferManagerBuilder.standard()
  36. .withS3Client(s3Client)
  37. .withMultipartUploadThreshold((long) (500 * 1024))
  38. .withExecutorFactory(() -> Executors.newFixedThreadPool(maxUploadThreads))
  39. .build();
  40. LOGGER.warn("Upload Starting");
  41. PutObjectRequest request = new PutObjectRequest(S3_BUCKET, fileUUId + File.separator + filename, inputStream, metadata);
  42. Upload upload = tm.upload(request);
  43. LOGGER.warn("Upload In Progress");
  44. upload.waitForCompletion();
  45. //Transfer
  46. LOGGER.warn("Upload Done");
  47. String url = s3Client.getUrl(S3_BUCKET, fileUUId + File.separator + filename).toString();
  48. FileUpload fileUpload = new FileUpload();
  49. fileUpload.setFileUploadId(fileUUId);
  50. fileUpload.setName(filename);
  51. fileUpload.setUrl(url);
  52. fileUploadRepository.save(fileUpload);
  53. LOGGER.warn("Saved to file upload repository");
  54. UploadStatus uploadStatus = new UploadStatus();
  55. uploadStatus.setFileId(fileUUId);
  56. uploadStatus.setMessage("File uploaded successfully");
  57. uploadStatus.setName(filename);
  58. uploadStatus.setStatus("UPLOADED");
  59. uploadStatus.setUrl(url);
  60. tm.shutdownNow();
  61. LOGGER.warn("Shutting down the TM connection");
  62. return uploadStatus;
  63. } catch (AmazonServiceException ex) {
  64. throw new StorageException("Some problem with the contacting the S3 bucket", ex);
  65. } catch (SdkClientException ex) {
  66. throw new StorageException("Some problem with the Amazon S3 SDK", ex);
  67. } catch (AmazonClientException | InterruptedException ex) {
  68. throw new StorageException("Error while uploading the file", ex);
  69. } catch (IOException ex) {
  70. throw new StorageException("Error reading the file", ex);
  71. }
  72. }
  73. public UploadStatus deleteFile(String fileId) {
  74. try {
  75. Regions regions = Regions.valueOf(S3_REGION);
  76. AmazonS3 s3Client = AmazonS3ClientBuilder.standard()
  77. .withRegion(regions)
  78. .withCredentials(new ProfileCredentialsProvider())
  79. .build();
  80. ListObjectsRequest listObjectsRequest = new ListObjectsRequest()
  81. .withBucketName(S3_BUCKET)
  82. .withPrefix(fileId);
  83. ObjectListing objectList = s3Client.listObjects(listObjectsRequest);
  84. List<S3ObjectSummary> objectSummaryList = objectList.getObjectSummaries();
  85. String[] keysList = new String[objectSummaryList.size()];
  86. int count = 0;
  87. for (S3ObjectSummary objectSummary : objectSummaryList) {
  88. keysList[count++] = objectSummary.getKey();
  89. }
  90. for (String key : keysList) {
  91. DeleteObjectRequest deleteObjectRequest = new DeleteObjectRequest(S3_BUCKET, fileId).withKey(key);
  92. s3Client.deleteObject(deleteObjectRequest);
  93. }
  94. s3Client.deleteObject(S3_BUCKET, fileId);
  95. fileUploadRepository.deleteById(fileId);
  96. UploadStatus uploadStatus = new UploadStatus();
  97. uploadStatus.setFileId(fileId);
  98. uploadStatus.setMessage("File deleted successfully");
  99. uploadStatus.setStatus("DELETED");
  100. return uploadStatus;
  101. } catch (AmazonServiceException ex) {
  102. throw new StorageException("Some problem with the contacting the S3 bucket", ex);
  103. } catch (SdkClientException ex) {
  104. throw new StorageException("Some problem with the Amazon S3 SDK", ex);
  105. }
  106. }

这在本地运行得非常好,我能够上传和删除aws s3中的文件。但是,当我在aws ec2示例中部署它并运行相同的服务时,它会给出以下错误响应:

  1. {
  2. "timestamp": "2021-05-24T03:53:25.220+0000",
  3. "status": 500,
  4. "error": "Internal Server Error",
  5. "message": "Failed to parse multipart servlet request; nested exception is java.io.IOException: org.apache.tomcat.util.http.fileupload.FileUploadBase$IOFileUploadException: Processing of multipart/form-data request failed. No space left on device",
  6. "path": "/test-script-services/rest/api/stories/scripts/upload"
  7. }

我检查了ec2示例的空间,没有问题:
这里的空间看起来不错
非常感谢您的帮助。
更新:结果 df :

  1. Filesystem Size Used Avail Use% Mounted on
  2. overlay 194G 29G 166G 15% /
  3. tmpfs 64M 0 64M 0% /dev
  4. tmpfs 16G 0 16G 0% /sys/fs/cgroup
  5. /dev/xvda1 194G 29G 166G 15% /etc/hosts
  6. shm 64M 0 64M 0% /dev/shm
  7. tmpfs 16G 0 16G 0% /proc/acpi
  8. tmpfs 16G 0 16G 0% /proc/scsi
  9. tmpfs 16G 0 16G 0% /sys/firmware

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题