Skip to content

Instantly share code, notes, and snippets.

@sh1n0b1
Last active May 30, 2023 16:59
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save sh1n0b1/f4ed6b341e86cf8a03950618e70e5ed2 to your computer and use it in GitHub Desktop.
Save sh1n0b1/f4ed6b341e86cf8a03950618e70e5ed2 to your computer and use it in GitHub Desktop.
AWS S3 basic operations via AWS Access Key & Session Token
# You don't need Fog in Ruby or some other library to upload to S3 -- shell works perfectly fine
# This is how I upload my new Sol Trader builds (http://soltrader.net)
# Based on a modified script from here: http://tmont.com/blargh/2014/1/uploading-to-s3-in-bash
# ====================================================================================
# Aug 25, 2016 sh1n0b1
# Modified this script to support AWS session token
# More work will be done on this.
#
# S3KEY="ASIAJLFN####################"
# S3SECRET="4gacEIX5s/ZTyLT+0K################"
# S3SESSION="FQoDYXdzELT//////////###########################################"
# ====================================================================================
S3KEY=""
S3SECRET=""
S3SESSION=""
# No need to modify the function below:
function putS3
{
bucket=$1
path=$2
file=$3
aws_path=$4
date=$(date +"%a, %d %b %Y %T %z")
acl="x-amz-acl:public-read"
content_type='application/x-compressed-tar'
token="x-amz-security-token:"${S3SESSION}
string="PUT\n\n$content_type\n$date\n$acl\n$token\n/$bucket$aws_path$file"
signature=$(echo -en "${string}" | openssl sha1 -hmac "${S3SECRET}" -binary | base64)
# Executing
# curl -v -X PUT -T "$path/$file" \
# -H "Host: $bucket.s3.amazonaws.com" \
# -H "Date: $date" \
# -H "Content-Type: $content_type" \
# -H "$acl" \
# -H "$token" \
# -H "Authorization: AWS ${S3KEY}:$signature" \
# "https://$bucket.s3.amazonaws.com$aws_path$file"
# Print the curl command
echo "curl -v -X PUT -T \"$path/$file\" \\"
echo " -H \"Host: $bucket.s3.amazonaws.com\" \\"
echo " -H \"Date: $date\" \\"
echo " -H \"Content-Type: $content_type\" \\"
echo " -H \"$acl\" \\"
echo " -H \"$token\" \\"
echo " -H \"Authorization: AWS ${S3KEY}:$signature\" \\"
echo " \"https://$bucket.s3.amazonaws.com$aws_path$file\""
}
function getObject
{
bucket=$1
filepath=$2
date=$(date +"%a, %d %b %Y %T %z")
acl="x-amz-acl:public-read"
content_type='application/x-compressed-tar'
token="x-amz-security-token:"${S3SESSION}
string="GET\n\n$content_type\n$date\n$acl\n$token\n/$bucket$filepath"
signature=$(echo -en "${string}" | openssl sha1 -hmac "${S3SECRET}" -binary | base64)
# #Executing
# curl -v -X GET \
# -H "Host: $bucket.s3.amazonaws.com" \
# -H "Date: $date" \
# -H "Content-Type: $content_type" \
# -H "$acl" \
# -H "$token" \
# -H "Authorization: AWS ${S3KEY}:$signature" \
# "https://$bucket.s3.amazonaws.com$filepath"
# Print the curl command
echo "curl -v -X GET \\"
echo " -H \"Host: $bucket.s3.amazonaws.com\" \\"
echo " -H \"Date: $date\" \\"
echo " -H \"Content-Type: $content_type\" \\"
echo " -H \"$acl\" \\"
echo " -H \"$token\" \\"
echo " -H \"Authorization: AWS ${S3KEY}:$signature\" \\"
echo " \"https://$bucket.s3.amazonaws.com$filepath\""
}
function listBucket
{
bucket=$1
filepath=$2
date=$(date +"%a, %d %b %Y %T %z")
acl="x-amz-acl:public-read"
content_type='application/x-compressed-tar'
token="x-amz-security-token:"${S3SESSION}
string="GET\n\n$content_type\n$date\n$acl\n$token\n/$bucket$filepath"
signature=$(echo -en "${string}" | openssl sha1 -hmac "${S3SECRET}" -binary | base64)
# #Executing
# curl -v -X GET \
# -H "Host: $bucket.s3.amazonaws.com" \
# -H "Date: $date" \
# -H "Content-Type: $content_type" \
# -H "$acl" \
# -H "$token" \
# -H "Authorization: AWS ${S3KEY}:$signature" \
# "https://$bucket.s3.amazonaws.com$filepath?list-type=2"
# Print the curl command
echo "curl -v -X GET \\"
echo " -H \"Host: $bucket.s3.amazonaws.com\" \\"
echo " -H \"Date: $date\" \\"
echo " -H \"Content-Type: $content_type\" \\"
echo " -H \"$acl\" \\"
echo " -H \"$token\" \\"
echo " -H \"Authorization: AWS ${S3KEY}:$signature\" \\"
echo " \"https://$bucket.s3.amazonaws.com$filepath?list-type=2\""
}
# ====================================================================================
# putS3 "bucket_name" "/local_filepath" "upload_file.txt" "/s3_dir"
# Just replace the parameters above^
# ====================================================================================
# ====================================================================================
# getObject "bucket_name" "/file_path.txt"
# ====================================================================================
# ====================================================================================
#listBucket "bucket_name" "/dir"
# ====================================================================================
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment