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
:
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)") }})
let storageOperation = Amplify.Storage.downloadData(key: "myKey")let progressSink = storageOperation.progressPublisher.sink { progress in print("Progress: \(progress)")}let resultSink = storageOperation.resultPublisher.sink { if case let .failure(storageError) = $0 { print("Failed: \(storageError.errorDescription). \(storageError.recoverySuggestion)") }}receiveValue: { data in print("Completed: \(data)")}
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)") } })
let downloadToFileName = FileManager.default.urls( for: .documentDirectory, in: .userDomainMask)[0].appendingPathComponent("myFile.txt")
let storageOperation = Amplify.Storage.downloadFile( key: "myKey", local: downloadToFileName)
let progressSink = storageOperation.progressPublisher.sink { progress in print("Progress: \(progress)")}
let resultSink = storageOperation.resultPublisher.sink { if case let .failure(storageError) = $0 { print("Failed: \(storageError.errorDescription). \(storageError.recoverySuggestion)") }}receiveValue: { print("Completed")}
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()}
You can also pause and then resume the operation.
storageOperation.pause()storageOperation.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)") }}
let sink = Amplify.Storage.getURL(key: "myKey") .resultPublisher .sink { if case let .failure(storageError) = $0 { print("Failed: \(storageError.errorDescription). \(storageError.recoverySuggestion)") } } receiveValue: { url in print("Completed: \(url)") }