Skip to content

Instantly share code, notes, and snippets.

@tariqhamid
Forked from andsens/sign_s3.gs
Created August 9, 2016 18:21
Show Gist options
  • Save tariqhamid/00347da2f5dbd07ca9f3bc773d517461 to your computer and use it in GitHub Desktop.
Save tariqhamid/00347da2f5dbd07ca9f3bc773d517461 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