Skip to content

Instantly share code, notes, and snippets.

@eftakhairul
Created December 25, 2023 22:21
Show Gist options
  • Select an option

  • Save eftakhairul/89cb912b8f2eeef4fb97dec134e90ced to your computer and use it in GitHub Desktop.

Select an option

Save eftakhairul/89cb912b8f2eeef4fb97dec134e90ced to your computer and use it in GitHub Desktop.

Revisions

  1. eftakhairul created this gist Dec 25, 2023.
    30 changes: 30 additions & 0 deletions s3_download.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,30 @@
    #!/usr/bin/env bash

    # Download a file from s3 without any 3rd party tools
    # thanks https://www.gyanblog.com/aws/how-upload-aws-s3-curl/

    file_path=$1
    bucket=$2
    set -eu pipefail
    # about the file
    filepath="/${bucket}/${file_path}"

    # metadata
    contentType="application/octet-stream"
    dateValue=`date -R`
    signature_string="GET\n\n${contentType}\n${dateValue}\n${filepath}"

    #s3 keys
    s3_access_key=$AWS_ACCESS_KEY_ID
    s3_secret_key=$AWS_SECRET_ACCESS_KEY

    #prepare signature hash to be sent in Authorization header
    signature_hash=`echo -en ${signature_string} | openssl sha1 -hmac ${s3_secret_key} -binary | base64`

    # actual curl command to do PUT operation on s3
    curl -sSo ${file_path} \
    -H "Host: ${bucket}.s3.amazonaws.com" \
    -H "Date: ${dateValue}" \
    -H "Content-Type: ${contentType}" \
    -H "Authorization: AWS ${s3_access_key}:${signature_hash}" \
    https://${bucket}.s3.amazonaws.com/${file_path}