Page updated Jan 16, 2024

List files

You can list all of the objects uploaded under a given prefix by setting the pageSize. If the pageSize is set lower than the total file size available, A single Storage.list call only returns a subset of all the files. To list all the files with multiple calls, the user can use the nextToken from the previous call response.

This will list all public files:

1let options = StorageListRequest.Options(pageSize: 1000)
2let listResult = try await Amplify.Storage.list(options: options)
3listResult.items.forEach { item in
4 print("Key: \(item.key)")
5}
1let sink = Amplify.Publisher.create {
2 let options = StorageListRequest.Options(pageSize: 1000)
3 try await Amplify.Storage.list(options: options)
4}.sink {
5 if case let .failure(error) = $0 {
6 print("Failed: \(error)")
7 }
8}
9receiveValue: { listResult in
10 print("Completed")
11 listResult.items.forEach { item in
12 print("Key: \(item.key)")
13 }
14}

You can also list private or protected files by passing options. For example, to list all protected files owned by a user identified by the ID otherUserID:

1let options = StorageListRequest.Options(accessLevel: .protected, targetIdentityId: "otherUserID", pageSize: 1000)
2let listResult = try await Amplify.Storage.list(options: options)
3listResult.items.forEach { item in
4 print("Key: \(item.key)")
5}
1let sink = Amplify.Publisher.create {
2 let options = StorageListRequest.Options(accessLevel: .protected, targetIdentityId: "otherUserID", pageSize: 1000)
3 try await Amplify.Storage.list(options: options)
4}.sink {
5 if case let .failure(error) = $0 {
6 print("Failed: \(error)")
7 }
8}
9receiveValue: { listResult in
10 print("Completed")
11 listResult.items.forEach { item in
12 print("Key: \(item.key)")
13 }
14}

If you like limit the response to keys that begin with the specified path provide the path options:

1let options = StorageListRequest.Options(path: "path")
2let listResult = try await Amplify.Storage.list(options: options)
3listResult.items.forEach { item in
4 print("Key: \(item.key)")
5}
1let sink = Amplify.Publisher.create {
2 let options = StorageListRequest.Options(path: "path")
3 try await Amplify.Storage.list(options: options)
4}.sink {
5 if case let .failure(error) = $0 {
6 print("Failed: \(error)")
7 }
8}
9receiveValue: { listResult in
10 print("Completed")
11 listResult.items.forEach { item in
12 print("Key: \(item.key)")
13 }
14}