Skip to content

Commit

Permalink
Merge pull request #683 from blackflux/dev
Browse files Browse the repository at this point in the history
[Gally]: master <- dev
  • Loading branch information
simlu authored Oct 2, 2019
2 parents 931124e + 2279ed9 commit f6e5054
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 3 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ Retrieves only the metadata from an object in an Amazon S3 bucket. Uses [s3:head
#### s3.deleteObject({ bucket: String, key: String })
Delete object from an Amazon S3 bucket at key. Uses [s3:deleteObject](https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/S3.html#deleteObject-property).

#### s3.listObjects({ bucket: String, limit: Number, startAfter: String })
#### s3.listObjects({ bucket: String, limit: Number, startAfter: String, prefix: String })
List objects keys in an Amazon S3 bucket. Internally this pages until the
limit is reached or no more keys are available. Uses [s3:listObjectsV2](https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/S3.html#listObjectsV2-property).

Expand Down
12 changes: 10 additions & 2 deletions src/module/s3.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,20 +27,28 @@ module.exports.S3 = ({ call }) => {
{ expectedErrorCodes }
);

const listObjects = async ({ bucket, limit, startAfter = undefined }) => {
const listObjects = async ({
bucket,
limit,
startAfter = undefined,
prefix = undefined
}) => {
const result = [];
let continuationToken;
let isTruncated;
do {
// eslint-disable-next-line no-await-in-loop
const response = await call('s3:listObjectsV2', {
Bucket: bucket,
MaxKeys: Math.min(1000, limit - result.length),
...(prefix === undefined ? {} : { Prefix: prefix }),
...(continuationToken === undefined && startAfter !== undefined ? { StartAfter: startAfter } : {}),
...(continuationToken === undefined ? {} : { ContinuationToken: continuationToken })
});
result.push(...response.Contents);
continuationToken = response.NextContinuationToken;
} while (continuationToken !== undefined && result.length < limit);
isTruncated = response.IsTruncated;
} while (isTruncated === true && result.length < limit);
return result;
};

Expand Down
14 changes: 14 additions & 0 deletions test/module/s3.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,20 @@ describe('Testing s3 Util', { useNock: true, timestamp: 1569876020 }, () => {
}]);
});

it('Testing "listObjects" with "Prefix"', async () => {
const result = await aws.s3.listObjects({
bucket,
limit: 1,
prefix: 'prefix'
});
expect(result).to.deep.equal([{
ETag: '"a32d8ca2be8b6454d40b230fcc4a2fc4"',
Key: 'prefix',
Size: 135,
StorageClass: 'STANDARD'
}]);
});

it('Testing "listObjects" with "ContinuationToken"', async () => {
const result = await aws.s3.listObjects({
bucket,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[
{
"scope": "https://test-bucket-name.s3.us-west-2.amazonaws.com:443",
"method": "GET",
"path": "/?list-type=2&max-keys=1&prefix=prefix",
"body": "",
"status": 200,
"response": "<?xml version=\"1.0\" encoding=\"UTF-8\"?><ListBucketResult xmlns=\"http://s3.amazonaws.com/doc/2006-03-01/\"><Name>test-bucket-name</Name><Prefix>prefix</Prefix>><KeyCount>1</KeyCount><MaxKeys>1</MaxKeys><Contents><Key>prefix</Key><ETag>&quot;a32d8ca2be8b6454d40b230fcc4a2fc4&quot;</ETag><Size>135</Size><StorageClass>STANDARD</StorageClass></Contents></ListBucketResult>"
}
]

0 comments on commit f6e5054

Please sign in to comment.