List file properties
You can list files without having to download all the files. You can do this by using the list API from the Amplify Library for Storage.
List Files
This will list all files located under path album that:
- have
privateaccess level - are in the root of
album/(the result doesn't include files under any sub path)
Future<void> listAlbum() async { try { String? nextToken; bool hasNextPage; do { final result = await Amplify.Storage.list( path: const StoragePath.fromString('public/album/'), options: StorageListOptions( pageSize: 50, nextToken: nextToken, pluginOptions: const S3ListPluginOptions( excludeSubPaths: true, ), ), ).result; safePrint('Listed items: ${result.items}'); nextToken = result.nextToken; hasNextPage = result.hasNextPage; } while (hasNextPage); } on StorageException catch (e) { safePrint(e.message); }}Pagination is enabled by default. The default pageSize is 1000 if it is not set in the StorageListOptions.
List all files without pagination
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):
Future<void> listAllUnderPublicPath() async { try { final result = await Amplify.Storage.list( path: const StoragePath.fromString('public/'), options: const StorageListOptions( pluginOptions: S3ListPluginOptions.listAll(), ), ).result; safePrint('Listed items: ${result.items}'); } on StorageException catch (e) { safePrint(e.message); }}List files from a specified bucket
You can also perform a list operation to a specific bucket by providing the bucket option. You can pass in a StorageBucket object representing the target bucket from the name defined in the Amplify Backend.
final result = await Amplify.Storage.list( path: const StoragePath.fromString('path/to/dir'), options: StorageListOptions( // Specify a target bucket using name assigned in Amplify Backend bucket: StorageBucket.fromOutputs('secondBucket'), ),).result;Alternatively, you can also pass in an object by specifying the bucket name and region from the console.
final result = await Amplify.Storage.list( path: const StoragePath.fromString('path/to/dir'), options: StorageListOptions( // Alternatively, provide bucket name from console and associated region bucket: StorageBucket.fromBucketInfo( BucketInfo( bucketName: 'second-bucket-name-from-console', region: 'us-east-2', ), ), ),).result;More list options
| Option | Type | Description |
|---|---|---|
| bucket | StorageBucket | The target bucket from the assigned name in the Amplify Backend or from the bucket name and region in the console Defaults to the default bucket and region from the Amplify configuration if this option is not provided. Read more at Configure additional storage buckets |
| excludeSubPaths | boolean | Whether to exclude objects under the sub paths of the path to list. Defaults to false. |
| delimiter | String | The delimiter to use when evaluating sub paths. If excludeSubPaths is false, this value has no impact on behavior. |
Get File Properties
You can also view properties of an individual file.
Future<void> getFileProperties() async { try { final result = await Amplify.Storage.getProperties( path: const StoragePath.fromString('example.txt'), ).result; safePrint('File size: ${result.storageItem.size}'); } on StorageException catch (e) { safePrint(e.message); }}As well as specify a bucket to target to view properties of a file
Future<void> getFileProperties() async { try { final result = await Amplify.Storage.getProperties( path: const StoragePath.fromString('example.txt'), options: StorageGetPropertiesOptions( StorageBucket.fromOutputs('secondBucket'), ), // Alternatively, provide bucket name from console and associated region /* options: StorageGetPropertiesOptions( bucket: StorageBucket.fromBucketInfo( BucketInfo( bucketName: 'second-bucket-name-from-console', region: 'us-east-2', ), ), ), */ ).result; safePrint('File size: ${result.storageItem.size}'); } on StorageException catch (e) { safePrint(e.message); }}