Reputation: 196
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
Upvotes: 0
Views: 3747
Reputation: 5225
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.
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.
Upvotes: 1