Page updated Jan 16, 2024

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.

Amplify libraries should be used for all new cloud connected applications. If you are currently using the AWS Mobile SDK for iOS, you can access the documentation here.

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:

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})
1let storageOperation = Amplify.Storage.downloadData(key: "myKey")
2let progressSink = storageOperation.progressPublisher.sink { progress in
3 print("Progress: \(progress)")
4}
5let resultSink = storageOperation.resultPublisher.sink {
6 if case let .failure(storageError) = $0 {
7 print("Failed: \(storageError.errorDescription). \(storageError.recoverySuggestion)")
8 }
9}
10receiveValue: { data in
11 print("Completed: \(data)")
12}

Download to file

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

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 })
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)
10
11let progressSink = storageOperation.progressPublisher.sink { progress in
12 print("Progress: \(progress)")
13}
14
15let resultSink = storageOperation.resultPublisher.sink {
16 if case let .failure(storageError) = $0 {
17 print("Failed: \(storageError.errorDescription). \(storageError.recoverySuggestion)")
18 }
19}
20receiveValue: {
21 print("Completed")
22}

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.

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

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

Generate a download URL

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

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}
1let sink = Amplify.Storage.getURL(key: "myKey")
2 .resultPublisher
3 .sink {
4 if case let .failure(storageError) = $0 {
5 print("Failed: \(storageError.errorDescription). \(storageError.recoverySuggestion)")
6 }
7 }
8 receiveValue: { url in
9 print("Completed: \(url)")
10 }