14 May

Upload to S3 via Bash and Curl

Uploading to S3 in Bash

There are already a couple of ways to do this using a 3rd party library, but I didn’t really feel like including and sourcing several hundred lines of code just to run a CURL command. So here’s how you can upload a file to S3 using the REST API.

This example uploads a gzipped tarball; you’ll need to adjust the content-type accordingly. And obviously use a real API key and secret.

file=/path/to/file/to/upload.tar.gz bucket=your-bucket resource="/${bucket}/${file}" contentType="application/x-compressed-tar" dateValue=`date -R` stringToSign="PUT\n\n${contentType}\n${dateValue}\n${resource}" s3Key=xxxxxxxxxxxxxxxxxxxx s3Secret=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx signature=`echo -en ${stringToSign} | openssl sha1 -hmac ${s3Secret} -binary | base64` curl -X PUT -T "${file}" \ -H "Host: ${bucket}.s3.amazonaws.com" \ -H "Date: ${dateValue}" \ -H "Content-Type: ${contentType}" \ -H "Authorization: AWS ${s3Key}:${signature}" \ https://${bucket}.s3.amazonaws.com/${file}