Skip to content

Commit 6e07065

Browse files
authored
Merge pull request #39 from QuantStack/maxKeys
Add limit to object listings in helping functions
2 parents cfdcb8b + 7f7937d commit 6e07065

File tree

1 file changed

+17
-18
lines changed

1 file changed

+17
-18
lines changed

src/s3.ts

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -376,23 +376,21 @@ export const checkS3Object = async (
376376
path?: string,
377377
isDir?: boolean
378378
): Promise<void> => {
379-
// checking the existance of an S3 object
380-
if (path) {
381-
await s3Client.send(
382-
new HeadObjectCommand({
383-
Bucket: bucketName,
384-
Key: PathExt.join(root, path) + (isDir === true ? '/' : '')
385-
})
386-
);
387-
}
388-
// checking if the root folder exists
389-
else {
390-
await s3Client.send(
391-
new ListObjectsV2Command({
392-
Bucket: bucketName,
393-
Prefix: root + '/'
394-
})
395-
);
379+
// checking the existance of an S3 object or if root folder exists
380+
const prefix: string = path
381+
? PathExt.join(root, path) + (isDir === true ? '/' : '')
382+
: root + '/';
383+
const { Contents } = await s3Client.send(
384+
new ListObjectsV2Command({
385+
Bucket: bucketName,
386+
Prefix: prefix,
387+
MaxKeys: 1
388+
})
389+
);
390+
if (Contents) {
391+
Promise.resolve();
392+
} else {
393+
Promise.reject();
396394
}
397395
};
398396

@@ -696,7 +694,8 @@ export async function isDirectory(
696694
const command = new ListObjectsV2Command({
697695
Bucket: bucketName,
698696
Prefix:
699-
objectPath[objectPath.length - 1] === '/' ? objectPath : objectPath + '/'
697+
objectPath[objectPath.length - 1] === '/' ? objectPath : objectPath + '/',
698+
MaxKeys: 1
700699
});
701700

702701
const { Contents } = await s3Client.send(command);

0 commit comments

Comments
 (0)