@aws-amplify/storage 4.4.30-unstable.1 → 4.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -22,13 +22,14 @@ import {
22
22
  S3Client,
23
23
  GetObjectCommand,
24
24
  DeleteObjectCommand,
25
- ListObjectsCommand,
25
+ ListObjectsV2Command,
26
26
  GetObjectCommandOutput,
27
27
  DeleteObjectCommandInput,
28
28
  CopyObjectCommandInput,
29
29
  CopyObjectCommand,
30
30
  PutObjectCommandInput,
31
31
  GetObjectCommandInput,
32
+ ListObjectsV2Request,
32
33
  } from '@aws-sdk/client-s3';
33
34
  import { formatUrl } from '@aws-sdk/util-format-url';
34
35
  import { createRequest } from '@aws-sdk/util-create-request';
@@ -56,6 +57,7 @@ import {
56
57
  S3ProviderPutOutput,
57
58
  ResumableUploadConfig,
58
59
  UploadTask,
60
+ S3ClientOptions,
59
61
  } from '../types';
60
62
  import { StorageErrorStrings } from '../common/StorageErrorStrings';
61
63
  import { dispatchStorageEvent } from '../common/StorageUtils';
@@ -67,6 +69,7 @@ import {
67
69
  autoAdjustClockskewMiddlewareOptions,
68
70
  createS3Client,
69
71
  } from '../common/S3ClientUtils';
72
+ import { S3ProviderListOutputWithToken } from '.././types/AWSS3Provider';
70
73
  import { AWSS3ProviderManagedUpload } from './AWSS3ProviderManagedUpload';
71
74
  import { AWSS3UploadTask, TaskEvents } from './AWSS3UploadTask';
72
75
  import { UPLOADS_STORAGE_KEY } from '../common/StorageConstants';
@@ -678,6 +681,31 @@ export class AWSS3Provider implements StorageProvider {
678
681
  throw error;
679
682
  }
680
683
  }
684
+ private async _list(
685
+ params: ListObjectsV2Request,
686
+ opt: S3ClientOptions,
687
+ prefix: string
688
+ ): Promise<S3ProviderListOutputWithToken> {
689
+ const result: S3ProviderListOutputWithToken = {
690
+ contents: [],
691
+ nextToken: '',
692
+ };
693
+ const s3 = this._createNewS3Client(opt);
694
+ const listObjectsV2Command = new ListObjectsV2Command({ ...params });
695
+ const response = await s3.send(listObjectsV2Command);
696
+ if (response && response.Contents) {
697
+ result.contents = response.Contents.map(item => {
698
+ return {
699
+ key: item.Key.substr(prefix.length),
700
+ eTag: item.ETag,
701
+ lastModified: item.LastModified,
702
+ size: item.Size,
703
+ };
704
+ });
705
+ result.nextToken = response.NextContinuationToken;
706
+ }
707
+ return result;
708
+ }
681
709
 
682
710
  /**
683
711
  * List bucket objects relative to the level and prefix specified
@@ -694,34 +722,38 @@ export class AWSS3Provider implements StorageProvider {
694
722
  if (!credentialsOK || !this._isWithCredentials(this._config)) {
695
723
  throw new Error(StorageErrorStrings.NO_CREDENTIALS);
696
724
  }
697
- const opt = Object.assign({}, this._config, config);
725
+ const opt: S3ClientOptions = Object.assign({}, this._config, config);
698
726
  const { bucket, track, maxKeys } = opt;
699
-
700
727
  const prefix = this._prefix(opt);
701
728
  const final_path = prefix + path;
702
- const s3 = this._createNewS3Client(opt);
703
729
  logger.debug('list ' + path + ' from ' + final_path);
704
-
705
- const params = {
706
- Bucket: bucket,
707
- Prefix: final_path,
708
- MaxKeys: maxKeys,
709
- };
710
-
711
- const listObjectsCommand = new ListObjectsCommand(params);
712
-
713
730
  try {
714
- const response = await s3.send(listObjectsCommand);
715
- let list: S3ProviderListOutput = [];
716
- if (response && response.Contents) {
717
- list = response.Contents.map(item => {
718
- return {
719
- key: item.Key.substr(prefix.length),
720
- eTag: item.ETag,
721
- lastModified: item.LastModified,
722
- size: item.Size,
723
- };
724
- });
731
+ const list: S3ProviderListOutput = [];
732
+ let token: string;
733
+ let listResult: S3ProviderListOutputWithToken;
734
+ const params: ListObjectsV2Request = {
735
+ Bucket: bucket,
736
+ Prefix: final_path,
737
+ MaxKeys: 1000,
738
+ };
739
+ if (maxKeys === 'ALL') {
740
+ do {
741
+ params.ContinuationToken = token;
742
+ params.MaxKeys = 1000;
743
+ listResult = await this._list(params, opt, prefix);
744
+ list.push(...listResult.contents);
745
+ if (listResult.nextToken) token = listResult.nextToken;
746
+ } while (listResult.nextToken);
747
+ } else {
748
+ maxKeys < 1000 || typeof maxKeys === 'string'
749
+ ? (params.MaxKeys = maxKeys)
750
+ : (params.MaxKeys = 1000);
751
+ listResult = await this._list(params, opt, prefix);
752
+ list.push(...listResult.contents);
753
+ if (maxKeys > 1000)
754
+ logger.warn(
755
+ "maxkeys can be from 0 - 1000 or 'ALL'. To list all files you can set maxKeys to 'ALL'."
756
+ );
725
757
  }
726
758
  dispatchStorageEvent(
727
759
  track,
@@ -12,6 +12,7 @@ import {
12
12
  UploadTaskProgressEvent,
13
13
  } from '../providers/AWSS3UploadTask';
14
14
  import { UploadTask } from './Provider';
15
+ import { ICredentials } from '@aws-amplify/core';
15
16
 
16
17
  type ListObjectsCommandOutputContent = _Object;
17
18
 
@@ -94,15 +95,24 @@ export type S3ProviderRemoveConfig = CommonStorageOptions & {
94
95
  provider?: 'AWSS3';
95
96
  };
96
97
 
98
+ export type S3ProviderListOutputWithToken = {
99
+ contents: S3ProviderListOutputItem[];
100
+ nextToken: string;
101
+ };
102
+
97
103
  export type S3ProviderRemoveOutput = DeleteObjectCommandOutput;
98
104
 
99
105
  export type S3ProviderListConfig = CommonStorageOptions & {
100
106
  bucket?: string;
101
- maxKeys?: number;
107
+ maxKeys?: number | 'ALL';
102
108
  provider?: 'AWSS3';
103
109
  identityId?: string;
104
110
  };
105
111
 
112
+ export type S3ClientOptions = StorageOptions & {
113
+ credentials: ICredentials;
114
+ } & S3ProviderListConfig;
115
+
106
116
  export interface S3ProviderListOutputItem {
107
117
  key: ListObjectsCommandOutputContent['Key'];
108
118
  eTag: ListObjectsCommandOutputContent['ETag'];