List files
You can list all files uploaded under a given path.
This will list all files located under path album
that:
- have
private
access level - are in the root of
album/
(the result doesn't include files under any sub path)
1import 'package:amplify_flutter/amplify_flutter.dart';2import 'package:amplify_storage_s3/amplify_storage_s3.dart';3
4Future<void> listAlbum() async {5 try {6 String? nextToken;7 bool hasNextPage;8 do {9 final result = await Amplify.Storage.list(10 path: 'album/',11 options: StorageListOptions(12 accessLevel: StorageAccessLevel.private,13 pageSize: 50,14 nextToken: nextToken,15 pluginOptions: const S3ListPluginOptions(16 excludeSubPaths: true,17 ),18 ),19 ).result;20 safePrint('Listed items: ${result.items}');21 nextToken = result.nextToken;22 hasNextPage = result.hasNextPage;23 } while (hasNextPage);24 } on StorageException catch (e) {25 safePrint('Error listing files: ${e.message}');26 rethrow;27 }28}
Pagination is enabled by default. The default pageSize
is 1000
if it is not set in the StorageListOptions
.
You can also list all files under a given path without pagination by using the pluginOptions
and S3ListPluginOptions.listAll()
constructor.
This will list all public files (i.e. those with guest
access level):
1import 'package:amplify_flutter/amplify_flutter.dart';2import 'package:amplify_storage_s3/amplify_storage_s3.dart';3
4Future<void> listAllWithGuestAccessLevel() async {5 try {6 final result = await Amplify.Storage.list(7 options: const StorageListOptions(8 accessLevel: StorageAccessLevel.guest,9 pluginOptions: S3ListPluginOptions.listAll(),10 ),11 ).result;12
13 safePrint('Listed items: ${result.items}');14 } on StorageException catch (e) {15 safePrint('Error listing files: ${e.message}');16 rethrow;17 }18}