Preparing the environment
Examples in this chapter use cURL for authentication as well as GET
, PUT
, POST
, and DELETE
requests run in Bash. To make sending requests easier, you can create the following script ~/.s3_environment, replacing s3_key
with S3AcessKeyId
and s3_secret
with S3SecretAccessKey
of a system user:
# S3 login variables. s3_key="a14040e0b2ef8b28FZZ8" s3_secret="dbwTnQTW602aAAdq8DQVFzB6yrTCFTNiGB8C8RFA" # Sign S3 requests and run curl. function s3_curl() { # Parse command line. if [ -z "${2}" ]; then echo "Usage: ${FUNCNAME[0]} <request_type> <s3_url> [<body_file>]" return 1 fi # Prepare a signature. s3_url="${2%/*}" s3_host="${s3_url##*://}" s3_query="${2##*/}" s3_date="$(date -R)" # Use UNSIGNED-PAYLOAD for the body. payload_hash="x-amz-content-sha256:UNSIGNED-PAYLOAD" # Generate a signature. s3_signature="$(echo -en "${1}\n\n\n${s3_date}\n${payload_hash}\n/${s3_query%%&*}" |\ openssl dgst -sha1 -hmac ${s3_secret} -binary | base64)" content_length="0" # Check if a body file is provided. if [ -n "${3}" ]; then content_length=$(cat ${3} | wc -c ) body_option="--data-binary @${3}" else body_option="" fi # Make the request. curl -H "Host: ${s3_host}" \ -H "Accept: */*" \ -H "Date: ${s3_date}" \ -H "${payload_hash}" \ -H "Authorization: AWS ${s3_key}:${s3_signature}" \ -H "Content-Type:" \ -H "Content-Length: ${content_length}" \ -X "${1}" \ ${body_option} \ "${s3_url}/${s3_query}" }
Load the script into your default environment to make the s3_curl
function available.
# source ~/.s3_environment
Once the script is loaded, you can make S3 requests using s3_curl
.