diff --git a/index.js b/index.js index d0589ee..ef1201b 100644 --- a/index.js +++ b/index.js @@ -139,16 +139,23 @@ class S3Adapter { // For a given config object, filename, and data, store a file in S3 // Returns a promise containing the S3 object creation response - async createFile(filename, data, contentType, options = {}) { + async createFile(filename, data, contentType, options = {}, config = {}) { + + let key_without_prefix = filename; + if (this._generateKey instanceof Function) { + try { + key_without_prefix = this._generateKey(filename, contentType, options); + }catch(e){ + throw new Error(e); // throw error if generateKey function fails + } + } + const params = { Bucket: this._bucket, - Key: this._bucketPrefix + filename, + Key: this._bucketPrefix + key_without_prefix, Body: data, }; - - if (this._generateKey instanceof Function) { - params.Key = this._bucketPrefix + this._generateKey(filename); - } + if (this._fileAcl) { if (this._fileAcl === 'none') { delete params.ACL; @@ -180,7 +187,17 @@ class S3Adapter { const endpoint = this._endpoint || `https://${this._bucket}.s3.${this._region}.amazonaws.com`; const location = `${endpoint}/${params.Key}`; - return Object.assign(response || {}, { Location: location }); + let url; + if (Object.keys(config).length != 0) { // if config is passed, we can generate a presigned url here + url = await this.getFileLocation(config, key_without_prefix); + } + + return { + location: location, // actual upload location, used for tests + name: key_without_prefix, // filename in storage, consistent with other adapters + s3_response: response, // raw s3 response + ...url ? { url: url } : {} // url (optionally presigned) or non-direct access url + }; } async deleteFile(filename) { diff --git a/lib/optionsFromArguments.js b/lib/optionsFromArguments.js index f56c9bc..b05c1fc 100644 --- a/lib/optionsFromArguments.js +++ b/lib/optionsFromArguments.js @@ -42,12 +42,12 @@ const optionsFromArguments = function optionsFromArguments(args) { } else if (args.length === 2) { options.bucket = stringOrOptions; if (typeof args[1] !== 'object') { - throw new Error("Failed to configure S3Adapter. Arguments don't make sense"); + throw new Error('Failed to configure S3Adapter. Arguments don\'t make sense'); } otherOptions = args[1]; } else if (args.length > 2) { if (typeof args[1] !== 'string' || typeof args[2] !== 'string') { - throw new Error("Failed to configure S3Adapter. Arguments don't make sense"); + throw new Error('Failed to configure S3Adapter. Arguments don\'t make sense'); } options.accessKey = args[0]; options.secretKey = args[1]; @@ -81,7 +81,7 @@ const optionsFromArguments = function optionsFromArguments(args) { options.bucket = s3overrides.params.Bucket; } } else if (args.length > 2) { - throw new Error("Failed to configure S3Adapter. Arguments don't make sense"); + throw new Error('Failed to configure S3Adapter. Arguments don\'t make sense'); } options = fromOptionsDictionaryOrDefault(options, 's3overrides', s3overrides); diff --git a/spec/test.spec.js b/spec/test.spec.js index 28193d0..a75ff73 100644 --- a/spec/test.spec.js +++ b/spec/test.spec.js @@ -696,7 +696,7 @@ describe('S3Adapter tests', () => { const s3 = getMockS3Adapter(options); const fileName = 'randomFileName.txt'; const response = s3.createFile(fileName, 'hello world', 'text/utf8').then(value => { - const url = new URL(value.Location); + const url = new URL(value.location); expect(url.pathname.indexOf(fileName) > 13).toBe(true); }); promises.push(response); @@ -707,7 +707,7 @@ describe('S3Adapter tests', () => { const s3 = getMockS3Adapter(options); const fileName = 'foo/randomFileName.txt'; const response = s3.createFile(fileName, 'hello world', 'text/utf8').then(value => { - const url = new URL(value.Location); + const url = new URL(value.location); expect(url.pathname.substring(1)).toEqual(options.bucketPrefix + fileName); }); promises.push(response); @@ -717,7 +717,7 @@ describe('S3Adapter tests', () => { const s3 = getMockS3Adapter(options); const fileName = 'foo/randomFileName.txt'; const response = s3.createFile(fileName, 'hello world', 'text/utf8').then(value => { - const url = new URL(value.Location); + const url = new URL(value.location); expect(url.pathname.indexOf('foo/')).toEqual(6); expect(url.pathname.indexOf('random') > 13).toBe(true); });