diff --git a/lib/s3.js b/lib/s3.js index f4bb131..c821371 100644 --- a/lib/s3.js +++ b/lib/s3.js @@ -83,10 +83,9 @@ module.exports = CoreObject.extend({ params.ContentEncoding = 'br'; } - return this.fetchRevisions(options) - .then(function(revisions) { - var found = revisions.map(function(element) { return element.revision; }).indexOf(options.revisionKey); - if (found >= 0 && !allowOverwrite) { + return this.findRevision(options) + .then(function(found) { + if (found !== undefined && !allowOverwrite) { return RSVP.reject("REVISION ALREADY UPLOADED! (set `allowOverwrite: true` if you want to support overwriting revisions)"); } return RSVP.resolve(); @@ -131,9 +130,8 @@ module.exports = CoreObject.extend({ params.ServerSideEncryption = serverSideEncryption; } - return this.fetchRevisions(options).then(function(revisions) { - var found = revisions.map(function(element) { return element.revision; }).indexOf(options.revisionKey); - if (found >= 0) { + return this.findRevision(options).then(function(found) { + if (found !== undefined) { return copyObject(params).then(function() { plugin.log('✔ ' + revisionKey + " => " + indexKey); }); @@ -143,6 +141,17 @@ module.exports = CoreObject.extend({ }); }, + findRevision: function(options) { + var client = this._client; + var listObjects = RSVP.denodeify(client.listObjects.bind(client)); + var bucket = options.bucket; + var prefix = options.prefix; + var revisionPrefix = joinUriSegments(prefix, options.filePattern + ":" + options.revisionKey); + + return listObjects({ Bucket: bucket, Prefix: revisionPrefix }) + .then((response) => response.Contents.find((element) => element.Key === revisionPrefix)); + }, + fetchRevisions: function(options) { var client = this._client; var bucket = options.bucket;