Skip to content

Instantly share code, notes, and snippets.

@eldyvoon
Created May 22, 2018 10:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eldyvoon/ca80812e2f0074c605ea0e7ef7b83b13 to your computer and use it in GitHub Desktop.
Save eldyvoon/ca80812e2f0074c605ea0e7ef7b83b13 to your computer and use it in GitHub Desktop.
sign s3
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