Skip to content

Instantly share code, notes, and snippets.

@andsens
Last active August 9, 2016 18:21
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save andsens/5107971 to your computer and use it in GitHub Desktop.
Save andsens/5107971 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