Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can i download a file from a private bucket using wget or curl? I have only access and secret key credentials

I have a private bucket that has a single zip file, a need to download it but i can't use aws cli or aws cmd. Can i do it using wget ou curl?

like image 720
Gabriel Melo Avatar asked Oct 23 '25 08:10

Gabriel Melo


1 Answers

I managed to do this with this script. If you call it download.sh assuming you have a bucket called my-bucket and a file in it called file.zip, and your aws key environment variables set, you should be able to download the file by calling.

./download.sh file.zip my-bucket

I adapted this from a similar script to upload a file I found here

#!/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}
like image 156
Will Munn Avatar answered Oct 25 '25 23:10

Will Munn



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!