0

I have my application that downloads the file form AWS S3 bucket. I am using below code:

public void downloadFileFromS3(String key) throws IOException, InterruptedException{
        LOG.info("Start - downloadFileFromS3 for key: {} and bucket name: {}", key, bucketName);
        initAWS();
        final GetObjectRequest request = new GetObjectRequest(bucketName, key);
        Download download = transferMgr.download(request, new File("D:\\rohit\\"+key));
        download.waitForCompletion();
}

Now, as we see the file destination path at client's end is hardcoded as D:\rohit. But, in the real world application when user clicks on download a file option, the file gets downloaded directly at client end's download folder.

Could someone please help me with file destination path. I am using Java & Spring Boot.

I tried searching on the net and found various solutions on stakeoverflow as well. But, the solutions didn't help:

Download file from Amazon S3 using REST API

Javascript to download a file from amazon s3 bucket?

download a file to particular location in client system

Rohit
  • 188
  • 2
  • 18

1 Answers1

1

Browser Handles the download. I'll give you a example. when you have html that says

<!-- Browser will download the file to user configured location instead of opening -->
<a href="http://example.org/mypdf.pdf" download>Download PDF</a>

Instead of using a anchor tag to download. you can trigger download when a request is received by following process.

  1. Generate a presigned url with content-disposition
  2. redirect to that URL

Generate an S3 Presigned URL with content-disposition.

GeneratePresignedUrlRequest req = new GeneratePresignedUrlRequest(bucketName, key);
ResponseHeaderOverrides overrides = new ResponseHeaderOverrides();
overrides.setContentDisposition("attachment; filename=\"give file name how you want your client to save\"");
req.setResponseHeaders(overrides);
URL url = this.client.generatePresignedUrl(req);

now redirect your user to the url you generated and let the browser handle the download.

Hope it helps.

Sahith Vibudhi
  • 4,935
  • 2
  • 32
  • 34
  • I have successfully returned URL back to UI. On click, file is getting downloaded, but my xls file is getting downloaded as file type. I have set the content-type as well. But not able to get the correct file. – Rohit May 17 '18 at 06:09
  • what do you mean by downloaded as file type? when did you set the content-type? before downloading or before uploading? – Sahith Vibudhi May 18 '18 at 06:05