Page updated Nov 12, 2023

Download files

Amplify iOS v1 is now in Maintenance Mode until May 31st, 2024. This means that we will continue to include updates to ensure compatibility with backend services and security. No new features will be introduced in v1.

Please use the latest version (v2) of Amplify Library for Swift to get started.

If you are currently using v1, follow these instructions to upgrade to v2.

There are three ways of getting data that was previously uploaded:

Download data

You can download to in-memory buffer Data object with Amplify.Storage.downloadData:

let storageOperation = Amplify.Storage.downloadData( key: "myKey", progressListener: { progress in print("Progress: \(progress)") }, resultListener: { (event) in switch event { case let .success(data): print("Completed: \(data)") case let .failure(storageError): print("Failed: \(storageError.errorDescription). \(storageError.recoverySuggestion)") } })
1let storageOperation = Amplify.Storage.downloadData(
2 key: "myKey",
3 progressListener: { progress in
4 print("Progress: \(progress)")
5 }, resultListener: { (event) in
6 switch event {
7 case let .success(data):
8 print("Completed: \(data)")
9 case let .failure(storageError):
10 print("Failed: \(storageError.errorDescription). \(storageError.recoverySuggestion)")
11 }
12})

Download to file

You can download to a file URL with Amplify.Storage.downloadFile:

let downloadToFileName = FileManager.default.urls( for: .documentDirectory, in: .userDomainMask )[0].appendingPathComponent("myFile.txt") let storageOperation = Amplify.Storage.downloadFile( key: "myKey", local: downloadToFileName, progressListener: { progress in print("Progress: \(progress)") }, resultListener: { event in switch event { case .success: print("Completed") case .failure(let storageError): print("Failed: \(storageError.errorDescription). \(storageError.recoverySuggestion)") } })
1let downloadToFileName = FileManager.default.urls(
2 for: .documentDirectory,
3 in: .userDomainMask
4)[0].appendingPathComponent("myFile.txt")
5
6let storageOperation = Amplify.Storage.downloadFile(
7 key: "myKey",
8 local: downloadToFileName,
9 progressListener: { progress in
10 print("Progress: \(progress)")
11 }, resultListener: { event in
12 switch event {
13 case .success:
14 print("Completed")
15 case .failure(let storageError):
16 print("Failed: \(storageError.errorDescription). \(storageError.recoverySuggestion)")
17 }
18 })

Cancel, Pause, Resume

Calls to downloadData or downloadFile return a reference to the operation that is actually performing the download.

To cancel the download (for example, in response to the user pressing a Cancel button), you simply call cancel() on the download operation.

func cancelDownload() { storageOperation.cancel() }
1func cancelDownload() {
2 storageOperation.cancel()
3}

You can also pause and then resume the operation.

Note that pause internally uses the suspend api of NSURLSessionTask, which suspends the task temporarily and does not fully pause the transfer. Complete pausing of task is tracked in this Github issue - https://github.com/aws-amplify/aws-sdk-ios/issues/3668

storageOperation.pause() storageOperation.resume()
1storageOperation.pause()
2storageOperation.resume()

Generate a download URL

You can also retrieve a URL for the object in storage:

Amplify.Storage.getURL(key: "myKey") { event in switch event { case let .success(url): print("Completed: \(url)") case let .failure(storageError): print("Failed: \(storageError.errorDescription). \(storageError.recoverySuggestion)") } }
1Amplify.Storage.getURL(key: "myKey") { event in
2 switch event {
3 case let .success(url):
4 print("Completed: \(url)")
5 case let .failure(storageError):
6 print("Failed: \(storageError.errorDescription). \(storageError.recoverySuggestion)")
7 }
8}