Skip to content

Commit 81e0b3b

Browse files
committed
Add support for recusively listing all revisions in a bucket
`s3.listObjects` has a `MaxKeys` parameter which defaults to `1000`. Unfortunately this also appears to be the highest value that you can set it to. When you have more than 1000 items matching the prefix in your bucket then some items were not being returned. Since there is no sorting options this could result in the plugin declaring "REVISION NOT FOUND!" when passed a value revision id. This commit makes sure that we page through the results from the call to `s3.listObjects` so that all results are loaded.
1 parent 6843149 commit 81e0b3b

File tree

1 file changed

+28
-3
lines changed

1 file changed

+28
-3
lines changed

lib/s3.js

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -125,14 +125,13 @@ module.exports = CoreObject.extend({
125125
var prefix = options.prefix;
126126
var revisionPrefix = joinUriSegments(prefix, options.filePattern + ":");
127127
var indexKey = joinUriSegments(prefix, options.filePattern);
128-
var listObjects = RSVP.denodeify(client.listObjects.bind(client));
129128

130129
return RSVP.hash({
131-
revisions: listObjects({ Bucket: bucket, Prefix: revisionPrefix }),
130+
revisions: this.listAllObjects({ Bucket: bucket, Prefix: revisionPrefix }),
132131
current: headObject(client, { Bucket: bucket, Key: indexKey }),
133132
})
134133
.then(function(data) {
135-
return data.revisions.Contents.sort(function(a, b) {
134+
return data.revisions.All.sort(function(a, b) {
136135
return new Date(b.LastModified) - new Date(a.LastModified);
137136
}).map(function(d) {
138137
var revision = d.Key.substr(revisionPrefix.length);
@@ -141,4 +140,30 @@ module.exports = CoreObject.extend({
141140
});
142141
});
143142
},
143+
144+
listAllObjects: function(options) {
145+
var client = this._client;
146+
var listObjects = RSVP.denodeify(client.listObjects.bind(client));
147+
var allRevisions = [];
148+
149+
return new Promise(function(resolve, reject) {
150+
function listObjectRecursively(options) {
151+
listObjects(options).then(function(response) {
152+
[].push.apply(allRevisions, response.Contents);
153+
154+
if (response.IsTruncated) {
155+
var nextMarker = response.NextMarker || response.Contents[response.Contents.length - 1].Key;
156+
options.Marker = nextMarker;
157+
listObjectRecursively(options);
158+
} else {
159+
response.All = allRevisions;
160+
resolve(response);
161+
}
162+
}).catch(reject);
163+
}
164+
165+
listObjectRecursively(options);
166+
});
167+
168+
}
144169
});

0 commit comments

Comments
 (0)