How to download a file using from s3 private bucket without AWS cli

9k Views Asked by At

Is it possible to download a file from AWS s3 without AWS cli? In my production server I would need to download a config file which is in S3 bucket.

I was thinking of having Amazon Systems Manger run a script that would download the config (YAML files) from the S3. But we do not want to install AWS cli on the production machines. How can I go about this?

2

There are 2 best solutions below

2
On BEST ANSWER

You would need some sort of program to call the Amazon S3 API to retrieve the object. For example, a PowerShell script (using AWS Tools for Windows PowerShell) or a Python script that uses the AWS SDK.

You could alternatively generate an Amazon S3 pre-signed URL, which would allow a private object to be downloaded from Amazon S3 via a normal HTTPS call (eg curl). This can be done easily using the AWS SDK for Python, or you could code it yourself without using libraries (it's a bit more complex).

In all examples above, you would need to provide the script/program with a set of IAM Credentials for authenticating with AWS.

0
On

Just adding notes for any C# code lovers to solve problem with .Net

  1. Firstly write(C#) code to download private file as string

     public string DownloadPrivateFileS3(string fileKey)
     {
         string accessKey = "YOURVALUE";
         string accessSecret = "YOURVALUE";;
         string bucket = "YOURVALUE";;
         using (s3Client = new AmazonS3Client(accessKey, accessSecret, "YOURVALUE"))
         {
                 var folderPath = "AppData/Websites/Cases";
                 var fileTransferUtility = new TransferUtility(s3Client);
                 Stream stream = fileTransferUtility.OpenStream(bucket, folderPath + "/" + fileKey);
                 using (var memoryStream = new MemoryStream())
                 {
                      stream.CopyTo(memoryStream);
                     var response = memoryStream.ToArray();
                     return Convert.ToBase64String(response);
                 }
    
                 return "";
        }
    }
    
  2. Second Write JQuery Code to download string as Base64

function downloadPrivateFile() {
$.ajax({url: 'DownloadPrivateFileS3?fileName=' + fileName, success: function(result){
        var link = this.document.createElement('a');
        link.download = fileName;
        link.href = "data:application/octet-stream;base64," + result;
        this.document.body.appendChild(link);
        link.click();
        this.document.body.removeChild(link);
  }});
}

Call downloadPrivateFile method from anywhere of HTML/C#/JQuery -

Enjoy Happy Coding and Solutions of Complex Problems