Created
May 22, 2018 10:42
-
-
Save eldyvoon/ca80812e2f0074c605ea0e7ef7b83b13 to your computer and use it in GitHub Desktop.
sign s3
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
const moment = require('moment') | |
const crypto = require('crypto') | |
exports.sign = (req, res) => { | |
const s3Config = { | |
bucket: 'assets.example.com', | |
region: 's3-ap-southeast-1', | |
keyStart: 'uploads/', | |
params: { | |
acl: 'public-read', | |
AWSAccessKeyId: process.env.S3_ACCESSKEY_ID | |
} | |
} | |
s3Config.params.policy = { | |
expiration: moment() | |
.add(1, 'days') | |
.toISOString(), | |
conditions: [ | |
{ bucket: s3Config.bucket }, | |
{ acl: s3Config.params.acl }, | |
{ success_action_status: '201' }, | |
{ 'x-requested-with': 'xhr' }, | |
['starts-with', '$key', s3Config.keyStart], | |
['starts-with', '$Content-Type', ''] | |
] | |
} | |
s3Config.params.policy = new Buffer( | |
JSON.stringify(s3Config.params.policy) | |
).toString('base64') | |
const hash = crypto.createHmac('sha1', process.env.S3_SECRET_KEY) | |
s3Config.params.signature = new Buffer( | |
hash.update(s3Config.params.policy).digest() | |
).toString('base64') | |
res.json(s3Config) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment