Skip to content

Commit

Permalink
wip: Allow the API s3 endpoint to be used with multiple storage provi…
Browse files Browse the repository at this point in the history
…ders #262
  • Loading branch information
cnouguier committed Sep 27, 2021
1 parent cc313d5 commit e7e9db3
Showing 1 changed file with 58 additions and 0 deletions.
58 changes: 58 additions & 0 deletions build/express-gateway/plugins/storage.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
const logger = require('express-gateway/lib/logger').createLoggerWithLabel('[EG:s3]');
const aws = require('aws-sdk');

let storageProxies = {}

module.exports = {
version: '1.2.0',
init: function (pluginContext) {
pluginContext.registerAdminRoute((app) => {
for (const [provider, options] of Object.entries(pluginContext.settings.providers)) {
logger.debug('Creating storage proxy ' + provider)
storageProxies[provider] = new aws.S3(options)
}
app.get(pluginContext.settings.endpointName + '/:provider/:bucket/*', (req, res) => {
const provider = req.params.provider
const storageProxy = storageProxies[provider]
if (storageProxy) {
storageProxy.getObject({
Bucket: req.params.bucket,
Key: req.params[0],
Range: req.headers.range // Forward range requests
})
.on('httpHeaders', (statusCode, headers) => {
// Avoid catching event raised by connection initialization
// https://stackoverflow.com/questions/35782434/streaming-file-from-s3-with-express-including-information-on-length-and-filetype
if (headers['content-length']) {
res.status(statusCode);
res.set(headers);
} else logger.debug('Invalid response with status ' + statusCode)
})
.createReadStream()
.on('error', (err) => {
logger.debug(err);
return res.status(404).send(err);
})
.pipe(res);
} else {
logger.debug('Unknown provider: ' + provider)
}
});
});
},
schema: {
$id: 'http://express-gateway.io/plugins/storage.json',
type: 'object',
properties: {
endpointName: {
type: 'string',
default: '/storage'
},
providers: {
type: 'object',
default: {}
}
},
required: ['endpointName']
}
};

0 comments on commit e7e9db3

Please sign in to comment.