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

fix(@aws-amplify/storage) correctly handle empty S3 list results #5509

Merged
merged 4 commits into from Apr 24, 2020
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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