Skip to content

Instantly share code, notes, and snippets.

@matb33
Created April 12, 2013 19:09
Show Gist options
  • Save matb33/5374361 to your computer and use it in GitHub Desktop.
Save matb33/5374361 to your computer and use it in GitHub Desktop.
AWS SDK Smart Package -- Exposes the SDK as AWS.SDK and provides a helper function to sign URLs
AWS = (function () {
var SDK = Npm.require("aws-sdk");
var crypto = Npm.require("crypto");
var url = Npm.require("url");
function getAuthenticatedURL(fullUrl, accessKeyID, secretAccessKey, expires) {
/*
Signature = URL-Encode( Base64( HMAC-SHA1( YourSecretAccessKeyID, UTF-8-Encoding-Of( StringToSign ) ) ) );
StringToSign = HTTP-VERB + "\n" +
Content-MD5 + "\n" +
Content-Type + "\n" +
Expires + "\n" +
CanonicalizedAmzHeaders +
CanonicalizedResource;
*/
var stringToSign, hmac, authUrl, matches, signature;
var urlObj = url.parse(fullUrl);
var hostname = urlObj.hostname;
var pathname = urlObj.pathname;
matches = hostname.match(/^(.+?)\.s3\.amazonaws\.com$/);
if (matches[1]) {
pathname = "/" + matches[1] + pathname;
}
expires = expires || 2145916800;
stringToSign = "GET\n\n\n" + expires + "\n" + pathname;
hmac = crypto.createHmac("sha1", secretAccessKey);
hmac.update(stringToSign);
signature = hmac.digest("base64");
urlObj.search = "AWSAccessKeyId=" + accessKeyID
+ "&Expires=" + expires
+ "&Signature=" + encodeURIComponent(signature);
authUrl = url.format(urlObj);
return authUrl;
}
return {
SDK: SDK,
getAuthenticatedURL: getAuthenticatedURL
};
})();
Npm.depends({"aws-sdk": "0.9.5-pre.6"});
Package.describe({
summary: "AWS SDK"
});
Package.on_use(function (api, where) {
api.add_files("aws-sdk.js", "server");
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment