Need to configure your backend?See Build a Backend →
ファイルプロパティをリストする
すべてのファイルをダウンロードすることなく、ファイルをリストできます。これは、Amplify Library for Storageのlist APIを使用して実行できます。
ファイルをリストする
これは、以下のパスalbumの下にあるすべてのファイルをリストします:
privateアクセスレベルを持つalbum/のルートにある(結果はサブパス下のファイルを含まない)
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); }}ページネーションはデフォルトで有効です。デフォルトのpageSizeは、StorageListOptionsで設定されていない場合は1000です。
ページネーションなしですべてのファイルをリストする
pluginOptionsとS3ListPluginOptions.listAll()コンストラクタを使用して、ページネーションなしで指定されたパス下のすべてのファイルをリストすることもできます。
これはすべての公開ファイル(つまり、guestアクセスレベルを持つファイル)をリストします:
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); }}指定されたバケットからファイルをリストする
bucketオプションを指定することで、特定のバケットに対してリスト操作を実行することもできます。Amplify Backendで定義された名前から、ターゲットバケットを表すStorageBucketオブジェクトを渡すことができます。
final result = await Amplify.Storage.list( path: const StoragePath.fromString('path/to/dir'), options: StorageListOptions( // Amplify Backendで割り当てられた名前を使用してターゲットバケットを指定する bucket: StorageBucket.fromOutputs('secondBucket'), ),).result;または、コンソールからバケット名とリージョンを指定することで、オブジェクトを渡すこともできます。
final result = await Amplify.Storage.list( path: const StoragePath.fromString('path/to/dir'), options: StorageListOptions( // または、コンソールからバケット名と関連するリージョンを指定します bucket: StorageBucket.fromBucketInfo( BucketInfo( bucketName: 'second-bucket-name-from-console', region: 'us-east-2', ), ), ),).result;その他のlistオプション
| オプション | 型 | 説明 |
|---|---|---|
| bucket | StorageBucket | Amplify Backendで割り当てられた名前から、またはコンソール内のバケット名とリージョンからターゲットバケット このオプションが指定されていない場合、Amplify設定のデフォルトバケットとリージョンにデフォルト設定されます。 追加のストレージバケットを構成するで詳細を確認できます |
| excludeSubPaths | boolean | パスのサブパス下のオブジェクトを除外するかどうか。デフォルトはfalseです。 |
| delimiter | String | サブパスを評価するときに使用する区切り文字。excludeSubPathsがfalseの場合、この値は動作に影響を与えません。 |
ファイルプロパティを取得する
個別のファイルのプロパティを表示することもできます。
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); }}また、ターゲットバケットを指定してファイルのプロパティを表示することもできます
Future<void> getFileProperties() async { try { final result = await Amplify.Storage.getProperties( path: const StoragePath.fromString('example.txt'), options: StorageGetPropertiesOptions( StorageBucket.fromOutputs('secondBucket'), ), // または、コンソールからバケット名と関連するリージョンを指定します /* 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); }}