Package Exports
- hapi-serve-s3
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (hapi-serve-s3) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
hapi-serve-s3
Easily serve files from an S3 bucket.
Plugin Usage
Register the plugin to your hapi server:
server.register({
register: require('hapi-serve-s3'),
}, function(err) {
if (err) {
throw err;
}
});Plugin options
None so far.
Route Definition
Use s3 as a handler:
// Serve a file from s3://my-awesome-bucket/path/to/file.pdf
serve.route({
method: 'GET',
route: '/file.pdf',
handler: {
s3: {
bucket: 'my-awesome-bucket',
mode: 'attachment',
filename: function(request) { // when downloaded from a browser, this will be the recommended download name
return Promise.resolve('awesome-pdf.pdf');
},
key: function(request) {
return Promise.resolve('path/to/file.pdf');
},
overrideContentTypes: {
// application/octet-stream is the default that S3 serves if you don't
// tell them the MIME type when uploading the file
'application/octet-stream': 'application/pdf',
},
},
},
});// Serve files from s3://my-awesome-bucket/path/to/*.pdf
serve.route({
method: 'GET',
route: '/files/{path*}',
handler: {
s3: {
bucket: 'my-awesome-bucket',
mode: 'attachment',
key: 'path/to',
overrideContentTypes: {
// application/octet-stream is the default that S3 serves if you don't
// tell them the MIME type when uploading the file
'application/octet-stream': 'application/pdf',
},
},
},
});// Serve files from s3 with custom authentication strategy
serve.route({
method: 'GET',
route: '/files/{path*}',
handler: {
s3: {
bucket: 'my-awesome-bucket',
mode: 'attachment',
key: function(request) {
return request.pre.authPath
}
},
},
config: {
pre: [
{
assign: 'authPath',
method: function(request, reply) {
// ... auth strategy here
if (!ok) {
return reply(Boom.unauthorized())
}
return replay(key)
}
}
]
}
});Handler Options:
mode(Bool|String) default=false- Specifies whether to include the Content-Disposition header.
- must be one of:
false,'attachment','inline'
filename([Function])- If provided, the function will receive the request and it should return a promise
that resolves the mapped
filename.filenamewill then be added to the Content-Disposition header. @see Content-Dispostion - If mode is not
falsebut no function is givenfilenamewill be set to the key's filename.
- If provided, the function will receive the request and it should return a promise
that resolves the mapped
overrideContentTypes(Object) default={}- If S3's reported content-type is a key, it will be replaced with the mapped value example: { "application/octet-stream" : "application/pdf" }
region(String) default='us-east-1'bucket(String|Function)- If a string is provided it will be used as bucket name.
- If a function is proviced, it will receive the request and should return
a promise that resolves the mapped
bucket.
key([String|Function])- If a string is provided, then it will be used to look up the key:
- if the route contains a parameter called "path", the key will be treated as a prefix
- otherwise, the key will be treated as a literal S3 key
- If a function is provided, it will receive the request and it should return a promise
that resolves the mapped
key.
- If a string is provided, then it will be used to look up the key:
sslEnabled(Bool) default=trueaccessKeyId([String]) default=process.env.AWS_ACCESS_KEY_IDsecretAccessKey([String]) default=process.env.AWS_SECRET_ACCESS_KEYs3Params([Object])- additional aws s3 options @see nodejs aws-sdk