首页 > 解决方案 > 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;
}

标签: javaamazon-web-servicesamazon-s3file-uploadout-of-memory

解决方案


推荐阅读