Amplify has re-imagined the way frontend developers build fullstack applications. Develop and deploy without the hassle.

Page updated May 16, 2024

List file properties

Storage for Gen 2 is not yet available for Flutter

You can list files without having to download all the files. You can do this by using the listFile API from the Amplify Library for Storage. You can also get properties individually for a file using the getProperties API.

List Files

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)
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);
}
}

More list options

OptionTypeDescription
excludeSubPathsbooleanWhether to exclude objects under the sub paths of the path to list. Defaults to false.
delimiterStringThe 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);
}
}

To get the metadata in result for all APIs when building against a web target, you have to configure user defined metadata in CORS.

Learn more about how to setup an appropriate CORS Policy.