Download files
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 in4 print("Progress: \(progress)")5 }, resultListener: { (event) in6 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
:
1let downloadToFileName = FileManager.default.urls(2 for: .documentDirectory,3 in: .userDomainMask4)[0].appendingPathComponent("myFile.txt")5
6let storageOperation = Amplify.Storage.downloadFile(7 key: "myKey",8 local: downloadToFileName,9 progressListener: { progress in10 print("Progress: \(progress)")11 }, resultListener: { event in12 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.
1func cancelDownload() {2 storageOperation.cancel()3}
You can also pause and then resume the operation.
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 in2 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}