· 9 years ago · Dec 25, 2016, 06:56 AM
1var express = require('express')
2, app = express()
3, s3 = require('./s3')
4 // Remember to fill in your own values here:
5, s3config = {
6 filenameLength: 32, // (files which are uploaded to S3 will have
7 // random file names of this length)
8 policyLifespan: (5 * 60 * 1000), // (5 minutes [measured in milliseconds])
9 accessKey: '********************',
10 secretKey: '****************************************',
11 bucket: '<MY-BUCKET-NAME>',
12 region: '<REGION>' // eg 'us-east-2'
13 };
14
15/* Start serving the public directory */
16app.use('/', express.static(__dirname + '/public'));
17
18/* Issue temporary policies */
19app.post('/_policy', function(req, res) {
20 console.log(req.body);
21 if (req.body.filename) {
22 var filename = crypto.randomBytes(filenameLength).toString('hex') + path.extname(req.body.filename);
23
24 res.json(s3.makeResponse(s3Config, {
25 filename: filename,
26 contentType: req.body.content_type
27 }));
28 } else {
29 res.status(400).send('Filename required');
30 }
31});