Created
October 20, 2018 19:21
-
-
Save merin83/80a19476a77734641671f5617f64f24a to your computer and use it in GitHub Desktop.
Generate an S3 signature using Node.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var crypto = require("crypto") | |
, util = require("util") | |
, express = require("express"); | |
var s3 = { | |
access_key_id: "<access_key>" | |
, secret_key: "<your_secret_key>" | |
, bucket: "<your_bucket>" | |
, acl: "public-read" | |
, https: "false" | |
, error_message: "" | |
, pad: function(n) { | |
if ((n+"").length == 1) { | |
return "0" + n; | |
} | |
return ""+n; | |
}, expiration_date: function() { | |
var now = new Date(); | |
var date = new Date( now.getTime() + (3600 * 1000) ); | |
var ed = date.getFullYear() + "-" + this.pad(date.getMonth()+1) + "-" + this.pad(date.getDate()); | |
ed += "T" + this.pad(date.getHours()) + ":" + this.pad(date.getMinutes()) + ":" + this.pad(date.getSeconds()) + ".000Z"; | |
return ed; | |
} | |
}; | |
var app = express.createServer(); | |
app.get("/crossdomain.xml", function(req,res) { | |
res.header("Content-Type", "text/xml"); | |
res.end("<?xml version=\"1.0\"?><!DOCTYPE cross-domain-policy SYSTEM \"http://www.adobe.com/xml/dtds/cross-domain-policy.dtd\"><cross-domain-policy><allow-access-from domain=\"*\" to-ports=\"*\" secure=\"false\" /></cross-domain-policy>"); | |
}); | |
app.get("/", function(req,res) { | |
var expiry_date = s3.expiration_date(); | |
var signatureString = "{\n 'expiration': '" + expiry_date + "',\n" | |
+ " 'conditions': [\n" | |
+ " {'bucket': '" + s3.bucket + "'}," | |
+ " {'key': '" + req.param("key", null) + "'}," | |
+ " {'acl': '" + s3.acl + "'}," | |
+ " {'Content-Type': '" + req.param("content_type", null) + "'}," | |
+ " {'Content-Disposition': 'attachment'}," | |
+ " ['starts-with', '$Filename', '']," | |
+ " ['eq', '$success_action_status', '201']\n ]\n}"; | |
var policy = new Buffer(signatureString).toString('base64').replace(/\n|\r/, ''); | |
var hmac = crypto.createHmac("sha1", s3.secret_key); | |
var hash2 = hmac.update(policy); | |
var signature = hmac.digest(encoding="base64"); | |
var xmlString = "<hash>"; | |
xmlString += "<policy>" + policy + "</policy>"; | |
xmlString += "<signature>" + signature + "</signature>"; | |
xmlString += "<bucket>" + s3.bucket + "</bucket>"; | |
xmlString += "<accesskeyid>" + s3.access_key_id + "</accesskeyid>"; | |
xmlString += "<acl>" + s3.acl + "</acl>"; | |
xmlString += "<expirationdate>" + expiry_date + "</expirationdate>"; | |
xmlString += "<https>" + s3.https + "</https>"; | |
xmlString += "<errorMessage>" + s3.error_message + "</errorMessage>"; | |
xmlString += "</hash>"; | |
res.end(xmlString); | |
}); | |
app.listen(1374); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment