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

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

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

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

这是我的服务代码:

public UploadStatus uploadFile(MultipartFile file) {
        LOGGER.warn("Starting to upload the file");
        return uploadFileById(file, null);
    }

    public UploadStatus uploadFileById(MultipartFile file, String fileId) {
        LOGGER.warn("Starting to upload in by ID method for the file");
        try {
            if (file.isEmpty()) {
                throw new StorageException("Failed to store an empty file");
            }
            Regions regions = Regions.valueOf(S3_REGION);
            String filename = file.getOriginalFilename();
            AmazonS3 s3Client = AmazonS3ClientBuilder.standard()
                    .withRegion(regions)
                    .withCredentials(new ProfileCredentialsProvider())
                    .build();
            String fileUUId;
            LOGGER.warn("Verifying the filename: " + filename);
            if (fileId == null) {
                fileUUId = UUID.randomUUID().toString();
            } else {
                fileUUId = fileId;
                Optional<FileUpload> optional = fileUploadRepository.findById(fileId);
                if (optional.isPresent()) {
                } else {
                    throw new StorageException("The fileId you sent is not present. Please send a valid fileId");
                }
                deleteFile(fileId);
            }
            InputStream inputStream = file.getInputStream();
            ObjectMetadata metadata = new ObjectMetadata();
            metadata.setContentLength(inputStream.available());
            LOGGER.warn("Verifying the filename: " + file.getName());
            int maxUploadThreads = 5;
            TransferManager tm = TransferManagerBuilder.standard()
                    .withS3Client(s3Client)
                    .withMultipartUploadThreshold((long) (500 * 1024))
                    .withExecutorFactory(() -> Executors.newFixedThreadPool(maxUploadThreads))
                    .build();
            LOGGER.warn("Upload Starting");
            PutObjectRequest request = new PutObjectRequest(S3_BUCKET, fileUUId + File.separator + filename, inputStream, metadata);
            Upload upload = tm.upload(request);
            LOGGER.warn("Upload In Progress");
            upload.waitForCompletion();
            //Transfer
            LOGGER.warn("Upload Done");
            String url = s3Client.getUrl(S3_BUCKET, fileUUId + File.separator + filename).toString();
            FileUpload fileUpload = new FileUpload();
            fileUpload.setFileUploadId(fileUUId);
            fileUpload.setName(filename);
            fileUpload.setUrl(url);
            fileUploadRepository.save(fileUpload);
            LOGGER.warn("Saved to file upload repository");
            UploadStatus uploadStatus = new UploadStatus();
            uploadStatus.setFileId(fileUUId);
            uploadStatus.setMessage("File uploaded successfully");
            uploadStatus.setName(filename);
            uploadStatus.setStatus("UPLOADED");
            uploadStatus.setUrl(url);
            tm.shutdownNow();
            LOGGER.warn("Shutting down the TM connection");
            return uploadStatus;
        } catch (AmazonServiceException ex) {
            throw new StorageException("Some problem with the contacting the S3 bucket", ex);
        } catch (SdkClientException ex) {
            throw new StorageException("Some problem with the Amazon S3 SDK", ex);
        } catch (AmazonClientException | InterruptedException ex) {
            throw new StorageException("Error while uploading the file", ex);
        } catch (IOException ex) {
            throw new StorageException("Error reading the file", ex);
        }
    }

    public UploadStatus deleteFile(String fileId) {
        try {
            Regions regions = Regions.valueOf(S3_REGION);
            AmazonS3 s3Client = AmazonS3ClientBuilder.standard()
                    .withRegion(regions)
                    .withCredentials(new ProfileCredentialsProvider())
                    .build();
            ListObjectsRequest listObjectsRequest = new ListObjectsRequest()
                    .withBucketName(S3_BUCKET)
                    .withPrefix(fileId);
            ObjectListing objectList = s3Client.listObjects(listObjectsRequest);
            List<S3ObjectSummary> objectSummaryList = objectList.getObjectSummaries();
            String[] keysList = new String[objectSummaryList.size()];
            int count = 0;
            for (S3ObjectSummary objectSummary : objectSummaryList) {
                keysList[count++] = objectSummary.getKey();
            }
            for (String key : keysList) {
                DeleteObjectRequest deleteObjectRequest = new DeleteObjectRequest(S3_BUCKET, fileId).withKey(key);
                s3Client.deleteObject(deleteObjectRequest);
            }
            s3Client.deleteObject(S3_BUCKET, fileId);
            fileUploadRepository.deleteById(fileId);
            UploadStatus uploadStatus = new UploadStatus();
            uploadStatus.setFileId(fileId);
            uploadStatus.setMessage("File deleted successfully");
            uploadStatus.setStatus("DELETED");
            return uploadStatus;
        } catch (AmazonServiceException ex) {
            throw new StorageException("Some problem with the contacting the S3 bucket", ex);
        } catch (SdkClientException ex) {
            throw new StorageException("Some problem with the Amazon S3 SDK", ex);
        }
    }

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

{
  "timestamp": "2021-05-24T03:53:25.220+0000",
  "status": 500,
  "error": "Internal Server Error",
  "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",
  "path": "/test-script-services/rest/api/stories/scripts/upload"
}

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

Filesystem      Size  Used Avail Use% Mounted on
overlay         194G   29G  166G  15% /
tmpfs            64M     0   64M   0% /dev
tmpfs            16G     0   16G   0% /sys/fs/cgroup
/dev/xvda1      194G   29G  166G  15% /etc/hosts
shm              64M     0   64M   0% /dev/shm
tmpfs            16G     0   16G   0% /proc/acpi
tmpfs            16G     0   16G   0% /proc/scsi
tmpfs            16G     0   16G   0% /sys/firmware

暂无答案!

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

相关问题