From 07be59c97667edee559498ddfe557df343363cae Mon Sep 17 00:00:00 2001 From: Eugene Girshov Date: Wed, 4 Oct 2017 10:23:36 +0300 Subject: [PATCH 1/2] uniqueFilename option to rename objects using uuid If 'uniqueFilename' option is specified, newly created object will be named after generated UUID and original filename is not used at all. --- s3router.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/s3router.js b/s3router.js index 4f0d76f..17e3cc3 100644 --- a/s3router.js +++ b/s3router.js @@ -78,7 +78,7 @@ function S3Router(options, middleware) { * give temporary access to PUT an object in an S3 bucket. */ router.get('/sign', middleware, function(req, res) { - var filename = (options.uniquePrefix ? uuidv4() + "_" : "") + req.query.objectName; + var filename = options.uniqueFilename ? uuidv4() : ((options.uniquePrefix ? uuidv4() + "_" : "") + req.query.objectName); var mimeType = req.query.contentType; var fileKey = checkTrailingSlash(getFileKeyDir(req)) + filename; // Set any custom headers From 7884bbe18be9f8b63a28c0905449f8217d5a58b2 Mon Sep 17 00:00:00 2001 From: Eugene Girshov Date: Wed, 4 Oct 2017 10:23:42 +0300 Subject: [PATCH 2/2] communicate S3 website URL to the client In case S3 bucket is configured in static website hosting mode, it is convenient to tell the client the public URL of newly created object to access it directly (instead of going through s3router redirection). Also, providing the function instead, it is possible to generate custom URL for example, if there a CDN or custom domain name configured. --- s3router.js | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/s3router.js b/s3router.js index 17e3cc3..d5313e0 100644 --- a/s3router.js +++ b/s3router.js @@ -99,12 +99,18 @@ function S3Router(options, middleware) { console.log(err); return res.send(500, "Cannot create S3 signed URL"); } - res.json({ + var json = { signedUrl: data, publicUrl: '/s3/uploads/' + filename, filename: filename, fileKey: fileKey, - }); + }; + if (typeof options.getWebsiteUrl === 'function') { + json.websiteUrl = options.getWebsiteUrl(fileKey); + } else if (options.getWebsiteUrl === true && options.region) { + json.websiteUrl = 'http://' + S3_BUCKET + '.s3-website.' + options.region + '.amazonaws.com/' + fileKey; + } + res.json(json); }); });