Download files
To further customize your in-app experience, you can use the getUrl
or downloadData
API from the Amplify Library for Storage.
Get or download file from a URL
With the getUrl
API, you can get a presigned URL which is valid for 900 seconds or 15 minutes by default. You can use this URL to create a download link for users to click on. The expiresAt
property is a Date
object that represents the time at which the URL will expire.
let url = try await Amplify.Storage.getURL( path: .fromString("public/example/path"))print("Completed: \(url)")
Check the existence of a file
When creating a downloadable URL, you can choose to check if the file exists by setting validateObjectExistence
to
true
in AWSStorageGetURLOptions
. If the file is inaccessible or does not exist, a StorageError
is thrown.
This allows you to check if an object exists during generating the presigned URL, which you can then use to download
that object.
let url = try await Amplify.Storage.getURL( path: .fromString("public/example/path"), options: .init( pluginOptions: AWSStorageGetURLOptions( validateObjectExistence: true ) ))
All getURL
options
Option | Type | Description |
---|---|---|
expires | Int | Number of seconds before the URL expires |
bucket | StorageBucket | The bucket in which the object is stored |
Download files
Download to a local file
Use the downloadFile
API to download the file locally on the client.
You can download to a file URL with Amplify.Storage.downloadFile
:
let downloadToFileUrl = FileManager.default.urls( for: .documentDirectory, in: .userDomainMask)[0].appendingPathComponent("myFile.txt")
let downloadTask = Amplify.Storage.downloadFile( path: .fromString("public/example/path"), local: downloadToFileUrl, options: nil)Task { for await progress in await downloadTask.progress { print("Progress: \(progress)") }}try await downloadTask.valueprint("Completed")
let downloadToFileUrl = FileManager.default.urls( for: .documentDirectory, in: .userDomainMask)[0].appendingPathComponent("myFile.txt")
let downloadTask = Amplify.Storage.downloadFile( path: .fromString("public/example/path"), local: downloadToFileUrl, options: nil)let progressSink = downloadTask .inProcessPublisher .sink { progress in print("Progress: \(progress)") }
let resultSink = downloadTask .resultPublisher .sink { if case let .failure(storageError) = $0 { print("Failed: \(storageError.errorDescription). \(storageError.recoverySuggestion)") } } receiveValue: { print("Completed") }
Download to data in memory
You can download to in-memory buffer Data object with Amplify.Storage.downloadData
:
let downloadTask = Amplify.Storage.downloadData( path: .fromString("public/example/path"))Task { for await progress in await downloadTask.progress { print("Progress: \(progress)") }}let data = try await downloadTask.valueprint("Completed: \(data)")
let downloadTask = Amplify.Storage.downloadData( path: .fromString("public/example/path"))let progressSink = downloadTask .inProcessPublisher .sink { progress in print("Progress: \(progress)") }
let resultSink = downloadTask .resultPublisher .sink { if case let .failure(storageError) = $0 { print("Failed: \(storageError.errorDescription). \(storageError.recoverySuggestion)") } } receiveValue: { data in print("Completed: \(data)") }
Download from a specified bucket
You can also perform a download operation from a specific bucket by providing the bucket
option
You can use .fromOutputs(name:)
to provide a string representing the target bucket's assigned name in the Amplify Backend.
// Download to Filelet downloadTask = Amplify.Storage.downloadFile( path: .fromString("public/example/path"), local: downloadToFileUrl, options: .init( bucket: .fromOutputs(name: "secondBucket") ))
// Download to Datalet downloadTask = Amplify.Storage.downloadData( path: .fromString("public/example/path"), options: .init( bucket: .fromOutputs(name: "secondBucket") ))
You can also use .fromBucketInfo(_:)
to provide a bucket name and region directly.
// Download to Filelet downloadTask = Amplify.Storage.downloadFile( path: .fromString("public/example/path"), local: downloadToFileUrl, options: .init( bucket: .fromBucketInfo(.init( bucketName: "another-bucket-name", region: "another-bucket-region") ) ))
// Download to Datalet downloadTask = Amplify.Storage.downloadData( path: .fromString("public/example/path"), options: .init( bucket: .fromBucketInfo(.init( bucketName: "another-bucket-name", region: "another-bucket-region") ) ))
Pause, resume, and cancel downloads
Calls to downloadData
or downloadFile
return a reference to the task that is actually performing the download.
You can pause then resume the task or cancel a task as shown below.
downloadTask.pause()downloadTask.resume()downloadTask.cancel()