I would like to be able to download a file from an S3 bucket to my local computer using a Java Lambda Function. I have written the code below in a Java Lambda Function which is able to download the file to the /tmp directory of AWS. But then what are my options? How do I get the file downloaded to my local computer? Thank you.
S3TransferManager transferManager = S3TransferManager.create();
DownloadFileRequest downloadFileRequest = DownloadFileRequest.builder()
.getObjectRequest(b -> b.bucket(bucketName).key(key))
.addTransferListener(LoggingTransferListener.create())
.destination(Paths.get(downloadedFileWithPath))
.build();
FileDownload downloadFile = transferManager.downloadFile(downloadFileRequest);
CompletedFileDownload downloadResult = downloadFile.completionFuture().join();
logger.info("Content length [{}]", downloadResult.response().contentLength());
return downloadResult.response().contentLength();
2
Answers
One option is to modify your Java Lambda function to upload the downloaded file to a different S3 bucket that you have access to in your local computer. Then, you can use the AWS CLI (Command Line Interface) to download the file from the S3 bucket to your local computer.
I can provide with code example that will allows you to download files from AWS Lambda.
Modify that as you wish, but consider next for your file distributions:
Response is only allowing body to be
String
. This is not optimal and just an example. You should useInputStream
instead in real application. For that you need to looks for the lib that can produce HTTP responses from input stream.By analogy you shouldn’t save file from S3 to file, you can use
S3Client
to getInputStream
, buffer it and pass to HTTP response.Don’t stream file via AWS Lambda and use pre-signed URL as @jarmod proposed. This will require you downloading file manually though.