Skip to content

Commit

Permalink
Fixed the presignedGet response headers as I had misunderstood how to…
Browse files Browse the repository at this point in the history
… include them in the url.
  • Loading branch information
bitcomposer committed May 21, 2021
1 parent 7741bce commit 3d81b90
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 23 deletions.
2 changes: 1 addition & 1 deletion examples/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
'use strict'

require('./simple/aws-s3.service')
require('./simple/file.service')
require('./simple/api.service')
6 changes: 3 additions & 3 deletions examples/simple/api.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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',
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down
40 changes: 27 additions & 13 deletions src/service.js
Original file line number Diff line number Diff line change
Expand Up @@ -597,38 +597,52 @@ 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 = {}
requestDate = new Date()
}

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()
})
}
},
Expand Down
6 changes: 3 additions & 3 deletions test/unit/service/actions/presignedGetObject.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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(
Expand Down

0 comments on commit 3d81b90

Please sign in to comment.