Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support s3 directories with more than 1000 keys #315

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 46 additions & 27 deletions src/backend/content-providers/s3/s3.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,39 +40,58 @@ function createS3Service(config: Object) {
format: null
});
const listObjects = (path: string, callback: Function) => {
const params = {
var params = {
Prefix: s3Prefix(path),
Delimiter: config.s3PathDelimiter, // Maximum allowed by S3 API
MaxKeys: 2147483647, //remove the folder name from listing
StartAfter: s3Prefix(path)
};
s3.listObjectsV2(params, (err, data) => {
if (err || !data) {
callback(err);
return;
}
if (!data.Contents) {
callback(new Error("Missing contents from S3 Response"));
return;
}
if (!data.CommonPrefixes) {
callback(new Error("Missing CommonPrefixes from S3 Response"));
return;
}
const files = data.Contents.map(fileObject);
const dirs = data.CommonPrefixes.map(dirObject);
callback(null, {
name: fileName(path),
path: path,
type: "directory",
writable: true,
created: null,
last_modified: null,
mimetype: null,
content: [...files, ...dirs],
format: "json"
var files = [];
var dirs = [];
function listAllKeys(cb) {
s3.listObjectsV2(params, (err, data) => {
if (err || !data) {
cb(err);
return;
}
if (!data.Contents) {
cb(new Error("Missing contents from S3 Response"));
return;
}
if (!data.CommonPrefixes) {
cb(new Error("Missing CommonPrefixes from S3 Response"));
return;
}
else {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think the else here is necessary. Once we've completed the null checks on the CommonPrefixes, Contents, and data then we'll always do the processing below.

var contents = data.Contents;
var prefixes = data.CommonPrefixes;
contents.forEach(content => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any reason for not using map here and for prefixes?

files.push(fileObject(content));
});
prefixes.forEach(content => {
dirs.push(dirObject(content));
});

if (data.IsTruncated) {
params.ContinuationToken = data.NextContinuationToken;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is NextContinunationToken guaranteed to be set if IsTruncated is true (seems like yes based on the docs) or do we need an additional null-check?

listAllKeys(cb);
return
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return
return;

}
}
cb(null, {
name: fileName(path),
path: path,
type: "directory",
writable: true,
created: null,
last_modified: null,
mimetype: null,
content: [...files, ...dirs],
format: "json"
});
});
});
}
listAllKeys(callback);
};
const getObject = (path: string, callback: Function) => {
s3.getObject({ Key: s3Prefix(path) }, (err, data) => {
Expand Down