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

Page updated May 2, 2024

List files

The latest version of Amplify Storage supports specifying S3 objects as a paths.
We recommend using path instead of key to specify S3 objects.

Note: key parameter is deprecated and may be removed in next major version.

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.

With StoragePath

let options = StorageListRequest.Options(pageSize: 1000)
let listResult = try await Amplify.Storage.list(
path: .fromString("public/example/path"),
options: options
)
listResult.items.forEach { item in
print("Path: \(item.path)")
}
let sink = Amplify.Publisher.create {
let options = StorageListRequest.Options(pageSize: 1000)
try await Amplify.Storage.list(
path: .fromString("public/example/path"),
options: options
)
}.sink {
if case let .failure(error) = $0 {
print("Failed: \(error)")
}
}
receiveValue: { listResult in
print("Completed")
listResult.items.forEach { item in
print("Path: \(item.path)")
}
}

With Key (Deprecated)

let options = StorageListRequest.Options(pageSize: 1000)
let listResult = try await Amplify.Storage.list(options: options)
listResult.items.forEach { item in
print("Key: \(item.key)")
}
let sink = Amplify.Publisher.create {
let options = StorageListRequest.Options(pageSize: 1000)
try await Amplify.Storage.list(options: options)
}.sink {
if case let .failure(error) = $0 {
print("Failed: \(error)")
}
}
receiveValue: { listResult in
print("Completed")
listResult.items.forEach { item in
print("Key: \(item.key)")
}
}

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 (This behavior is deprecated):

let options = StorageListRequest.Options(accessLevel: .protected, targetIdentityId: "otherUserID", pageSize: 1000)
let listResult = try await Amplify.Storage.list(options: options)
listResult.items.forEach { item in
print("Path: \(item.path)")
}
let sink = Amplify.Publisher.create {
let options = StorageListRequest.Options(accessLevel: .protected, targetIdentityId: "otherUserID", pageSize: 1000)
try await Amplify.Storage.list(options: options)
}.sink {
if case let .failure(error) = $0 {
print("Failed: \(error)")
}
}
receiveValue: { listResult in
print("Completed")
listResult.items.forEach { item in
print("Path: \(item.path)")
}
}

If you like limit the response to keys that begin with the specified path provide the path options (This behavior is deprecated):

let options = StorageListRequest.Options(path: "path")
let listResult = try await Amplify.Storage.list(options: options)
listResult.items.forEach { item in
print("Key: \(item.key)")
}
let sink = Amplify.Publisher.create {
let options = StorageListRequest.Options(path: "path")
try await Amplify.Storage.list(options: options)
}.sink {
if case let .failure(error) = $0 {
print("Failed: \(error)")
}
}
receiveValue: { listResult in
print("Completed")
listResult.items.forEach { item in
print("Key: \(item.key)")
}
}