Skip to content

Instantly share code, notes, and snippets.

@hangj
Created November 24, 2022 07:07
Show Gist options
  • Save hangj/2491f848b3fd0fe502dfbcc090894aee to your computer and use it in GitHub Desktop.
Save hangj/2491f848b3fd0fe502dfbcc090894aee to your computer and use it in GitHub Desktop.
s3 upload files with shell script
#!/usr/bin/env bash
# hangj.cnblogs.com
# https://www.cnblogs.com/hangj/p/16921863.html
s3_key="Q3AM3UQ867SPQQA43P2F"
s3_secret="zuf+tfteSlswRu7BJ86wekitnifILbZam1KYY3TG"
session_token="Security-Token"
bucket="bucket-log"
tar_file="hello.log"
host="play.min.io"
region="cn-default"
service_name="s3"
method="PUT"
path="/${bucket}/${tar_file}"
query_string=""
content_type="application/octet-stream"
content_sha256="UNSIGNED-PAYLOAD"
amz_date=`date -u +%Y%m%dT%H%M%SZ`
signer_date=`echo ${amz_date} | cut -c1-8`
aws4_request="aws4_request"
scope="$signer_date/cn-default/s3/$aws4_request"
content_length=`wc -c $tar_file | awk '{print $1}'`
content_md5=`openssl dgst -md5 -binary $tar_file | base64`
date_key=`echo -en ${signer_date} | openssl dgst -sha256 -binary -hmac "AWS4${s3_secret}"`
date_region_key=`echo -en $region | openssl dgst -sha256 -binary -hmac "${date_key}"`
date_region_service_key=`echo -en $service_name | openssl dgst -sha256 -binary -hmac "${date_region_key}"`
signing_key=`echo -en $aws4_request | openssl dgst -sha256 -binary -hmac "${date_region_service_key}"`
headers="content-length:$content_length\ncontent-md5:$content_md5\ncontent-type:$content_type\nhost:$host\nx-amz-content-sha256:$content_sha256\nx-amz-date:$amz_date\nx-amz-security-token:$session_token"
signed_headers="content-length;content-md5;content-type;host;x-amz-content-sha256;x-amz-date;x-amz-security-token"
canonical_request="$method\n$path\n$query_string\n$headers\n\n$signed_headers\n$content_sha256"
canonical_request_hash=`echo -en "$canonical_request" |openssl sha256`
string_to_sign="AWS4-HMAC-SHA256\n$amz_date\n$scope\n$canonical_request_hash"
printf "string_to_sign: $string_to_sign"
signature=`echo -en "${string_to_sign}" | openssl dgst -sha256 -hmac "${signing_key}"`
echo
echo $signature
curl -v -X ${method} -T "${tar_file}" \
-H "Host: $host" \
-H "Accept-Encoding: identity" \
-H "Content-Length: ${content_length}" \
-H "Content-MD5: ${content_md5}" \
-H "x-amz-content-sha256: ${content_sha256}" \
-H "X-Amz-Security-Token: ${session_token}" \
-H "x-amz-date: ${amz_date}" \
-H "Content-Type: ${content_type}" \
-H "Authorization: AWS4-HMAC-SHA256 Credential=${s3_key}/${scope}, SignedHeaders=$signed_headers, Signature=${signature}" \
https://$host${path}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment