1

Is it possible stream a zip file through a Java (spring boot) service from Amazon S3 without having to actually download the whole file in the service?

For context, imagine having a large file in S3 (in gigs), that you want to let your users download it, but you want to authorize those users before giving them access to the file, without making the bucket or the object publicly available.

One way to do this, is to download the file in your java service, then send it in the response. But if the file is too big, the disk space sometime will not be enough.

Thus the question, is there a way to somehow stream the file from S3 (the java service would act as a sort of proxy) to the client, without actually downloading it.

PS: I am aware of the other alternative would use short-lived pre-signed URLs

Ouerghi Yassine
  • 1,835
  • 7
  • 43
  • 72

1 Answers1

1

You could try use StreamingOutput (JAX-RS/Jersey).

var s3Object = baseTransferManager.getAmazonS3Client().getObject(file);
StreamingOutput stream = (outputStream -> {
    IOUtils.copy(s3Object.getObjectContent(), outputStream);
});
return Response.ok(stream, MediaType.APPLICATION_OCTET_STREAM)
    .header("Content-Disposition", "attachment; filename=\"" + fileName + \"")
    .build();

This answer was based on the information given in JAX-RS/Jersey 2 file download - Is there a common API for server and generated client proxy