diff --git a/examples/index.js b/examples/index.js index 49cb878..aafe746 100644 --- a/examples/index.js +++ b/examples/index.js @@ -1,4 +1,4 @@ 'use strict' -require('./simple/aws-s3.service') +require('./simple/file.service') require('./simple/api.service') diff --git a/examples/simple/api.service.js b/examples/simple/api.service.js index 4137cf7..1ea8848 100644 --- a/examples/simple/api.service.js +++ b/examples/simple/api.service.js @@ -31,7 +31,7 @@ broker.createService({ authentication: true, mappingPolicy: 'restrict', aliases: { - 'POST /putObject': 'stream:aws-s3.putObject' + 'POST /putObject': 'stream:file.putObject' }, bodyParsers: { json: false, @@ -41,8 +41,8 @@ broker.createService({ { path: '/api', whitelist: [ - // Access any actions in 'aws-s3' service - 'aws-s3.*' + // Access any actions in 'file' service + 'file.*' ], bodyParsers: { json: true diff --git a/examples/simple/aws-s3.service.js b/examples/simple/file.service.js similarity index 95% rename from examples/simple/aws-s3.service.js rename to examples/simple/file.service.js index 2096722..c2cee42 100644 --- a/examples/simple/aws-s3.service.js +++ b/examples/simple/file.service.js @@ -5,7 +5,7 @@ const AwsS3Service = require('../../index') // Create broker let broker = new ServiceBroker({ - name: 'aws-s3', + name: 'file', nodeID: 'aws-s3-node', logger: console, tracing: true, @@ -16,7 +16,7 @@ let broker = new ServiceBroker({ // Load services broker.createService({ - name: 'aws-s3', + name: 'file', mixins: AwsS3Service, settings: { endPoint: 'http://s3.devmonkey.uk', diff --git a/package.json b/package.json index c683405..7f0abbc 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@bitcomposer/moleculer-aws-s3", - "version": "0.1.1", + "version": "0.1.2", "description": "An aws s3 sdk wrapper as a service for the moleculer framework", "main": "index.js", "scripts": { diff --git a/src/service.js b/src/service.js index af487d2..dd4df43 100644 --- a/src/service.js +++ b/src/service.js @@ -597,10 +597,11 @@ module.exports = { }, handler(ctx) { const { bucketName, objectName, expires, reqParams, requestDate } = ctx.params - const command = new GetObjectCommand({ + + const params = { Bucket: bucketName, Key: objectName - }) + } if (isFunction(reqParams)) { reqParams = {} @@ -608,27 +609,40 @@ module.exports = { } var validRespHeaders = [ - 'response-content-type', - 'response-content-language', - 'response-expires', - 'response-cache-control', - 'response-content-disposition', - 'response-content-encoding' + { key: 'response-content-type', value: 'ResponseContentType' }, + { key: 'response-content-language', value: 'ResponseContentLanguage' }, + { key: 'response-expires', value: 'ResponseExpires' }, + { key: 'response-cache-control', value: 'ResponseCacheControl' }, + { key: 'response-content-disposition', value: 'ResponseContentDisposition' }, + { key: 'response-content-encoding', value: 'ResponseContentEncoding' } ] + + // Precheck for header types and error if they aren't string. validRespHeaders.forEach(header => { if ( reqParams !== undefined && - reqParams[header] !== undefined && - !isString(reqParams[header]) + reqParams[header.key] !== undefined && + !isString(reqParams[header.key]) ) { - throw new TypeError(`response header ${header} should be of type "string"`) + throw new TypeError(`response header ${header.key} should be of type "string"`) } }) + validRespHeaders.forEach(header => { + if ( + reqParams !== undefined && + reqParams[header.key] !== undefined && + isString(reqParams[header.key]) + ) { + params[header.value] = reqParams[header.key] + } + }) + + const command = new GetObjectCommand(params) + return getSignedUrl(this.client, command, { expiresIn: expires ?? 3600, - signingDate: requestDate ?? new Date(), - signableHeaders: reqParams + signingDate: requestDate ?? new Date() }) } }, diff --git a/test/unit/service/actions/presignedGetObject.spec.js b/test/unit/service/actions/presignedGetObject.spec.js index 1b2d517..5aefc37 100644 --- a/test/unit/service/actions/presignedGetObject.spec.js +++ b/test/unit/service/actions/presignedGetObject.spec.js @@ -24,8 +24,7 @@ describe('Service', () => { const requestDate = 'Mon, 01 Jan 2018 00:00:00 GMT' const expectedParams = { expiresIn: expires, - signingDate: requestDate, - signableHeaders: reqParams + signingDate: requestDate } return Service() @@ -41,7 +40,8 @@ describe('Service', () => { .then(r => { const command = new GetObjectCommand({ Bucket: bucketName, - Key: objectName + Key: objectName, + ResponseContentType: 'bar' }) expect(r).toEqual(url) expect(JSON.stringify(mockGetSignedUrl.mock.calls[0][0])).toBe(