3

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?

John Rotenstein
  • 241,921
  • 22
  • 380
  • 470
kumar
  • 8,207
  • 20
  • 85
  • 176
  • Feel free to edit your question to add more information (eg which operating system, what limitations you are under) if the answers don't provide enough details. – John Rotenstein Jul 11 '20 at 02:57

2 Answers2

3

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.

John Rotenstein
  • 241,921
  • 22
  • 380
  • 470
  • "or you could code it yourself without using libraries (it's a bit more complex)." Do you happen to have a reference for how to do it? I'm having trouble finding one. – Mike B Jun 08 '23 at 22:30
  • 1
    @MikeB [Sample Python code to generate presigned URL - AWS IoT Core](https://docs.aws.amazon.com/iot/latest/developerguide/connect-iot-lorawan-network-analyzer-request-sample.html) – John Rotenstein Jun 08 '23 at 22:38
0

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

Qaiser Mehmood
  • 202
  • 2
  • 10