-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
wip: Allow the API s3 endpoint to be used with multiple storage provi…
…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.
There are no files selected for viewing
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
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'] | ||
} | ||
}; |