Skip to content

Instantly share code, notes, and snippets.

@wilmoore
Last active May 4, 2024 05:57
Show Gist options
  • Select an option

  • Save wilmoore/1637198 to your computer and use it in GitHub Desktop.

Select an option

Save wilmoore/1637198 to your computer and use it in GitHub Desktop.
Software Engineering :: Cloud :: AWS :: Amazon S3 :: Creating S3 Virtual Directories w/ s3cmd and curl

Software Engineering :: Cloud :: AWS :: Amazon S3 :: Creating S3 Virtual Directories w/ s3cmd and curl

⪼ Made with 💜 by Polyglot.

{"expiration": "2015-01-01T00:00:00Z",
"conditions": [
{"bucket": "filemanager-curl"},
["starts-with", "$key", ""],
{"acl": "public-read"},
["starts-with", "$Content-Type", ""],
["content-length-range", 0, 1048576]
]
}
#!/usr/bin/env sh
export ACCESS_KEY=$(s3cmd --dump-config | grep -P '^access_key' | cut -d' ' -f3)
export SECRET_KEY=$(s3cmd --dump-config | grep -P '^secret_key' | cut -d' ' -f3)
export POLICY_EXP=$(date -v "+1d" +"%Y-%m-%dT%H:%M:%SZ")
export POLICY_DOC=$(printf '{"expiration": "%s", "conditions": [ {"bucket": "filemanager-curl"}, ["starts-with", "$key", ""], {"acl": "public-read"}, ["starts-with", "$Content-Type", ""], ["content-length-range", 0, 1048576] ]}' ${POLICY_EXP})
POLICY_B64=$(echo ${POLICY_DOC} | openssl base64 | tr -d "\n")
SIGNATURE=$(echo ${POLICY_DOC} | openssl base64 | tr -d "\n" | openssl sha1 -hmac "${SECRET_KEY}" -binary | openssl base64)
echo "SECRET KEY (bash)"
echo ${SECRET_KEY}
echo ""
echo "POLICY DOCUMENT (bash)"
echo ${POLICY_DOC}
echo ""
echo "POLICY DOCUMENT (base64: bash)"
echo ${POLICY_B64}
echo ""
echo "SIGNATURE (bash)"
echo ${SIGNATURE}
echo ""
echo ""
echo "CURL (BASH)"
echo "LIST (START)"
s3cmd ls s3://filemanager-curl/
curl -F "key=images-$(date +"%Y-%m-%d-%H%M%S" | tr -d '-')/" \
-F "acl=public-read" \
-F "policy=$POLICY_B64" \
-F "signature=$SIGNATURE" \
-F "AWSAccessKeyId=$ACCESS_KEY" \
-F "Content-Type=binary/octet-stream" \
-F "file=@/dev/null" \
http://filemanager-curl.s3.amazonaws.com
echo "LIST (START)"
s3cmd ls s3://filemanager-curl/
echo ""
echo "SECRET KEY (PHP)"
php -r "echo getenv('SECRET_KEY');"
echo ""
echo ""
echo "POLICY (PHP)"
php -r "echo str_replace(PHP_EOL, '', getenv('POLICY_DOC'));"
echo ""
echo ""
echo "POLICY (base64: PHP)"
php -r "echo str_replace(PHP_EOL, '', base64_encode(getenv('POLICY_DOC')));"
echo ""
echo ""
echo "SIGNATURE (PHP)"
php -r "echo base64_encode(hash_hmac('sha1', str_replace(PHP_EOL, '', base64_encode(getenv('POLICY_DOC'))), getenv('SECRET_KEY'), true));"
echo ""
echo ""
echo "CURL (PHP)"
echo "LIST (START)"
s3cmd ls s3://filemanager-curl/
curl -F "key=images-$(date +"%Y-%m-%d-%H%M%S" | tr -d '-')/" \
-F "acl=public-read" \
-F "policy=$(php -r "echo str_replace(PHP_EOL, '', base64_encode(getenv('POLICY_DOC')));")" \
-F "signature=$(php -r "echo base64_encode(hash_hmac('sha1', str_replace(PHP_EOL, '', base64_encode(getenv('POLICY_DOC'))), getenv('SECRET_KEY'), true));")" \
-F "AWSAccessKeyId=$ACCESS_KEY" \
-F "Content-Type=binary/octet-stream" \
-F "file=@/dev/null" \
http://filemanager-curl.s3.amazonaws.com
echo "LIST (START)"
s3cmd ls s3://filemanager-curl/
echo ""
echo "CURL (BASH)"
echo "LIST (START)"
s3cmd ls s3://filemanager-curl/
curl -F "key=images-$(date +"%Y-%m-%d-%H%M%S" | tr -d '-')/" \
-F "acl=public-read" \
-F "policy=$POLICY_B64" \
-F "signature=$SIGNATURE" \
-F "AWSAccessKeyId=$ACCESS_KEY" \
-F "Content-Type=binary/octet-stream" \
-F "file=@/dev/null" \
http://filemanager-curl.s3.amazonaws.com
echo "LIST (START)"
s3cmd ls s3://filemanager-curl/
echo ""
@wilmoore
Copy link
Copy Markdown
Author

For a more complete solution, check out s3mkdirs:
https://github.com/Net-Results/s3dd/blob/master/bin/s3mkdirs

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment