Last active
August 29, 2015 14:04
-
-
Save danlmarmot/ddf8a0a8bccbc31e8b7c to your computer and use it in GitHub Desktop.
Upload files matching pattern to S3 with Bash and curl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# | |
patternMatches="foo/target/*.jar | |
bar/target/*.jar | |
baz/target/*.jar | |
" | |
bucket=my-bucketname | |
objectPath=bucketdir/subdir | |
#S3 access is via an restriced AWS IAM user | |
s3Key=AKIAiiiiiiiMYACCESSKEY | |
s3Secret=fDWmABiiiiiiMYSECRETKEY | |
function uploadFile() { | |
# echo "uploading filepath $1" | |
base_name=$(basename $1) | |
# echo "base_name is $base_name" | |
resource="/${bucket}/${objectPath}/${base_name}" | |
contentType="application/octet-stream" | |
dateValue=$(date -u +"%a, %d %b %Y %T %z") | |
stringToSign="PUT\n\n${contentType}\n${dateValue}\n${resource}" | |
signature=$(echo -en ${stringToSign} | openssl sha1 -hmac ${s3Secret} -binary | base64) | |
curl -X PUT -T "$1" \ | |
-H "Host: ${bucket}.s3.amazonaws.com" \ | |
-H "Date: ${dateValue}" \ | |
-H "Content-Type: ${contentType}" \ | |
-H "Authorization: AWS ${s3Key}:${signature}" \ | |
-L https://${bucket}.s3.amazonaws.com/${objectPath}/${base_name} | |
} | |
for p in "${patternMatches[@]}"; do | |
set -- $p | |
if [ "$#" -gt 0 ]; then | |
for f in "$@"; do | |
# files_to_upload=(${files_to_upload[@]} ${f}) | |
uploadFile ${f} | |
done | |
fi | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment