Skip to content

Instantly share code, notes, and snippets.

@seanbehan
Last active December 29, 2017 00:52
Show Gist options
  • Save seanbehan/3c4b26fb3047f7550502f5f6e65bd903 to your computer and use it in GitHub Desktop.
Save seanbehan/3c4b26fb3047f7550502f5f6e65bd903 to your computer and use it in GitHub Desktop.
Upload files to S3 via bash
#!/bin/sh
#
# Usage
# ./s3upload.bash path/to/file.txt name-of-file-on-s3.txt
#
file="$1"
path="$2"
key_id="s3 key goes here"
key_secret="s3 secret goes here"
bucket="bucket name goes here"
content_type="application/octet-stream"
date="$(LC_ALL=C date -u +"%a, %d %b %Y %X %z")"
md5="$(openssl md5 -binary < "$file" | base64)"
sig="$(printf "PUT\n$md5\n$content_type\n$date\n/$bucket/$path" | openssl sha1 -binary -hmac "$key_secret" | base64)"
curl -T $file https://$bucket.s3.amazonaws.com/$path \
-H "Date: $date" \
-H "Authorization: AWS $key_id:$sig" \
-H "Content-Type: $content_type" \
-H "Content-MD5: $md5"
@seanbehan
Copy link
Author

seanbehan commented Dec 29, 2017

For public read of files in bucket add this as the bucket policy .. and rename NAME_OF_YOUR_BUCKET_GOES_HERE to your actual bucket name.

{
    "Version": "2008-10-17",
    "Statement": [
        {
            "Sid": "AllowPublicRead",
            "Effect": "Allow",
            "Principal": {
                "AWS": "*"
            },
            "Action": "s3:GetObject",
            "Resource": "arn:aws:s3:::NAME_OF_YOUR_BUCKET_GOES_HERE/*"
        }
    ]
}

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