Skip to content

Instantly share code, notes, and snippets.

@amcclosky
Forked from andsens/sign_s3.gs
Created February 10, 2016 15:29
Show Gist options
  • Save amcclosky/ea9b6ee3705bfb7d4ff0 to your computer and use it in GitHub Desktop.
Save amcclosky/ea9b6ee3705bfb7d4ff0 to your computer and use it in GitHub Desktop.
Google spreadsheet function for signing S3 URLs (very handy for CSV cost allocation reports when combined with the ImportData() function)
function sign_s3(access_key, private_key, bucket, object_name, validity, base_url) {
if(!base_url) {
base_url = "http://s3.amazonaws.com";
}
if(!validity) {
validity = 60;
}
expires = Math.floor((new Date()).getTime() / 1000) + validity;
object_name = encodeURIComponent(object_name);
stringToSign = "GET\n\n\n"+expires+"\n/"+bucket+"/"+object_name;
signature = encodeURIComponent(Utilities.base64Encode(Utilities.computeHmacSignature(Utilities.MacAlgorithm.HMAC_SHA_1, stringToSign, private_key)));
url = base_url+"/"+bucket+"/"+object_name+"?AWSAccessKeyId="+access_key+"&Expires="+expires+"&Signature="+signature;
return url;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment