Skip to content

Instantly share code, notes, and snippets.

@bhanuc

bhanuc/sign.js Secret

Created January 21, 2015 14:50
Show Gist options
  • Save bhanuc/bda0c4b48aa39fdf8aa2 to your computer and use it in GitHub Desktop.
Save bhanuc/bda0c4b48aa39fdf8aa2 to your computer and use it in GitHub Desktop.
S3 Server singing Code
router.get('/sign_s3', function * () {
var object_name = this.request.query.s3_object_name;
var mime_type = this.request.query.s3_object_type;
var now = new Date();
var expires = Math.ceil((now.getTime() + 1000000) / 1000); // 1000 seconds from now
var amz_headers = "x-amz-acl:public-read";
var put_request = "PUT\n\n" + mime_type + "\n" + expires + "\n" + amz_headers + "\n/" + S3_BUCKET + "/" + object_name;
var signature = crypto.createHmac('sha1', AWS_SECRET_KEY).update(put_request).digest('base64');
signature = signature.replace('+','%2B').replace('/','%2F').replace('=','%3D');
signature = encodeURIComponent(signature.trim());
var url = 'https://' + S3_BUCKET + '.s3.amazonaws.com/' + object_name;
var credentials = {
signed_request: url + "?AWSAccessKeyId=" + AWS_ACCESS_KEY + "&Expires=" + expires + "&Signature=" + signature,
url: url
};
this.body = JSON.stringify(credentials);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment