Amplify has re-imagined the way frontend developers build fullstack applications. Develop and deploy without the hassle.

Page updated May 16, 2024

Download files

To further customize your in-app experience, you can use the getUrl or downloadData API from the Amplify Library for Storage.

Note: Refer to the Transfer Acceleration documentation to learn how to enable transfer acceleration for storage APIs.

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
)
)
)

More getURL options

OptionTypeDescription
expiresIntegerNumber of seconds before the URL expires

Download to a 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 downloadToFileName = FileManager.default.urls(
for: .documentDirectory,
in: .userDomainMask
)[0].appendingPathComponent("myFile.txt")
let downloadTask = Amplify.Storage.downloadFile(
path: .fromString("public/example/path"),
local: downloadToFileName,
options: nil
)
Task {
for await progress in await downloadTask.progress {
print("Progress: \(progress)")
}
}
try await downloadTask.value
print("Completed")
let downloadToFileName = FileManager.default.urls(
for: .documentDirectory,
in: .userDomainMask
)[0].appendingPathComponent("myFile.txt")
let downloadTask = Amplify.Storage.downloadFile(
path: .fromString("public/example/path"),
local: downloadToFileName,
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")
}

API to download 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.value
print("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)")
}

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()

Download tasks are run using URLSessionTask instances internally. You can learn more about them in Apple's official documentation.