Skip to content

Commit

Permalink
fix(@aws-amplify/storage) correctly handle empty S3 list results (#5509)
Browse files Browse the repository at this point in the history
  • Loading branch information
Amplifiyer committed Apr 24, 2020
1 parent fd45012 commit d6fcb6a
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 8 deletions.
25 changes: 25 additions & 0 deletions packages/storage/__tests__/providers/AWSS3Provider-unit-test.ts
Expand Up @@ -23,6 +23,9 @@ import { S3RequestPresigner } from '@aws-sdk/s3-request-presigner';

S3Client.prototype.send = jest.fn(async command => {
if (command instanceof ListObjectsCommand) {
if (command.input.Prefix === 'public/emptyListResultsPath') {
return {};
}
return {
Contents: [
{
Expand Down Expand Up @@ -729,6 +732,28 @@ describe('StorageProvider test', () => {
spyon.mockClear();
});

test('list objects with zero results', async () => {
jest.spyOn(Credentials, 'get').mockImplementationOnce(() => {
return new Promise((res, rej) => {
res({});
});
});

const storage = new StorageProvider();
storage.configure(options);
const spyon = jest.spyOn(S3Client.prototype, 'send');

expect.assertions(2);
expect(
await storage.list('emptyListResultsPath', { level: 'public' })
).toEqual([]);
expect(spyon.mock.calls[0][0].input).toEqual({
Bucket: 'bucket',
Prefix: 'public/emptyListResultsPath',
});
spyon.mockClear();
});

test('list object with track', async () => {
jest.spyOn(Credentials, 'get').mockImplementationOnce(() => {
return new Promise((res, rej) => {
Expand Down
19 changes: 11 additions & 8 deletions packages/storage/src/providers/AWSS3Provider.ts
Expand Up @@ -405,14 +405,17 @@ export class AWSS3Provider implements StorageProvider {

try {
const response = await s3.send(listObjectsCommand);
const list = (response as any).Contents.map(item => {
return {
key: item.Key.substr(prefix.length),
eTag: item.ETag,
lastModified: item.LastModified,
size: item.Size,
};
});
let list = [];
if (response && response.Contents) {
list = response.Contents.map(item => {
return {
key: item.Key.substr(prefix.length),
eTag: item.ETag,
lastModified: item.LastModified,
size: item.Size,
};
});
}
dispatchStorageEvent(
track,
'list',
Expand Down

0 comments on commit d6fcb6a

Please sign in to comment.