繁体   English   中英

亚马逊 sdk 从 spring 启动 Z93F725A074233D1C889F4F48B 中的 s3 存储桶下载大型 zip 文件

[英]Amazon sdk dowload large zip file from s3 bucket in spring boot java

我所做的是尝试从 s3 存储桶下载大数据 zip 文件

        S3ObjectInputStream inputStreams = s3object.getObjectContent(); 
        File newFile = new File(zipFileTempLocation + File.separator + CommonConstant.FILE_NAME);
        FileOutputStream fileOutputStream = new FileOutputStream(newFile);
        GZIPInputStream gzipInputStream = new GZIPInputStream(inputStream);
        LOGGER.info("staring to write {}", newFile.toPath());
        byte[] buffer = new byte[5000];
        int le
        while ((len = gzipInputStream.read(buffer)) > 0) {
            fileOutputStream.write(buffer, 0, len);
        }
        gzipInputStream.close();
        String newFileURl = newFile.getAbsolutePath();
        Path path = Paths.get(url);
        return Files.readAllBytes(path);

}

当我尝试运行我的服务时,它说堆外 memory 错误。 你能帮我解决这个问题吗?

您可以查看答案和 stream 它,因此它不会加载到 memory 中。

  @GetMapping(value = "/downloadfile/**", produces = { MediaType.APPLICATION_OCTET_STREAM_VALUE })
    public ResponseEntity<S3ObjectInputStream> downloadFile(HttpServletRequest request) {
       //reads the content from S3 bucket and returns a S3ObjectInputStream
       S3Object object = publishAmazonS3.getObject("12345bucket", "/logs/file1.log");
       S3ObjectInputStream finalObject = object.getObjectContent();

        final StreamingResponseBody body = outputStream -> {
            int numberOfBytesToWrite = 0;
            byte[] data = new byte[1024];
            while ((numberOfBytesToWrite = finalObject.read(data, 0, data.length)) != -1) {
                System.out.println("Writing some bytes..");
                outputStream.write(data, 0, numberOfBytesToWrite);
            }
            finalObject.close();
        };
        return new ResponseEntity<>(body, HttpStatus.OK);
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM