java - java.lang.OutOfMemoryError:使用 TransferManager 将大文件上传到 Amazon AWS S3 存储桶时的 Java 堆空间
问题描述
我想将大文件 (>1GB) 上传到我的亚马逊 AWS S3 存储桶。因此我使用的是 AWS 提供的 TransferManager。我还指定了文件长度,如本文所述Java Heap Space is enough to upload files on AWS S3中所述,但我仍然面临 Java 堆空间错误。有谁知道,我在我的代码中做错了什么?或者一般来说可能是什么问题?先感谢您!
public String uploadMultiPartFilePublicRead(MultipartFile multipartFile) throws IOException {
String fileUrl = "";
String fileName= "";
File file = null;
try {
file = convertMultiPartToFile(multipartFile);
fileName = generateFileName(multipartFile);
fileUrl = endpointUrl + "/" + bucketName + "/" + fileName;
// uploadFileTos3bucketPublicRead(fileName, file);
} catch (Exception e) {
e.printStackTrace();
}
if(!file.exists()) return "File does not exits";
int maxUploadThreads = 5;
TransferManager tm = TransferManagerBuilder
.standard()
.withS3Client(s3client)
.withMultipartUploadThreshold((long) (5 * 1024 * 1024))
.withExecutorFactory(() -> Executors.newFixedThreadPool(maxUploadThreads))
.build();
ObjectMetadata metadata = new ObjectMetadata();
metadata.setContentLength(file.length());
ProgressListener progressListener =
progressEvent -> System.out.println("Transferred bytes: " + progressEvent.getBytesTransferred());
PutObjectRequest request = new PutObjectRequest(this.bucketName, fileName, file)
.withCannedAcl(CannedAccessControlList.PublicRead)
.withMetadata(metadata);
request.setGeneralProgressListener(progressListener);
Upload upload = tm.upload(request);
try {
upload.waitForCompletion();
System.out.println("Upload complete.");
} catch (AmazonClientException e) {
System.out.println("Error occurred while uploading file: AmazonClientExeception");
e.printStackTrace();
}
catch(InterruptedException i)
{
System.out.println("Erorr occured while uploading file: InterruptedExecpetion");
i.printStackTrace();
}
try{
file.delete();
}
catch(Exception e)
{
System.out.println("Error occured while deleting file.");
e.printStackTrace();
}
return fileUrl;
}
private File convertMultiPartToFile(MultipartFile file) throws IOException {
File convFile = new File(file.getOriginalFilename());
FileOutputStream fos = new FileOutputStream(convFile);
fos.write(file.getBytes());
fos.close();
return convFile;
}
解决方案
推荐阅读
- google-analytics - 无法接收来自 Google Measurement Protocol (Google Analytics v4) 的事件
- azure-data-explorer - Kusto 创建用于测试的内存表
- android - 如何将 CSV 文件加载到 Flutter 应用程序中?
- javascript - 为什么我提交新搜索时之前的结果没有清除?
- python - 用颜色填充两条线之间的间隙
- kubernetes - Kubernetes 入口 502
- json - 使用 JSON-B 从 JSON 中提取特定元素
- java - FCM 和自我证书问题
- java - Java 流如何 .stream()
- javascript - 将 formcontrol、formgrops 和 formarray 添加到重复的 html 角度