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

Page updated May 1, 2024

Upload files

The latest version of Amplify Storage supports specifying S3 objects as a paths.
We recommend using path instead of key to specify S3 objects.

Note: key parameter is deprecated and may be removed in next major version.

Upload data

To upload to S3 from a data object, specify the path and the data object to be uploaded.

With StoragePath

let dataString = "My Data"
let data = Data(dataString.utf8)
let uploadTask = Amplify.Storage.uploadData(
path: .fromString("public/example/path"),
data: data
)
Task {
for await progress in await uploadTask.progress {
print("Progress: \(progress)")
}
}
let value = try await uploadTask.value
print("Completed: \(value)")
let dataString = "My Data"
let data = Data(dataString.utf8)
let uploadTask = Amplify.Storage.uploadData(
path: .fromString("public/example/path"),
data: data
)
let progressSink = uploadTask
.inProcessPublisher
.sink { progress in
print("Progress: \(progress)")
}
let resultSink = uploadTask
.resultPublisher
.sink {
if case let .failure(storageError) = $0 {
print("Failed: \(storageError.errorDescription). \(storageError.recoverySuggestion)")
}
}
receiveValue: { data in
print("Completed: \(data)")
}

With Key (Deprecated)

let dataString = "My Data"
let data = Data(dataString.utf8)
let uploadTask = Amplify.Storage.uploadData(
key: "ExampleKey",
data: data
)
Task {
for await progress in await uploadTask.progress {
print("Progress: \(progress)")
}
}
let value = try await uploadTask.value
print("Completed: \(value)")
let dataString = "My Data"
let data = Data(dataString.utf8)
let uploadTask = Amplify.Storage.uploadData(
key: "ExampleKey",
data: data
)
let progressSink = uploadTask
.inProcessPublisher
.sink { progress in
print("Progress: \(progress)")
}
let resultSink = uploadTask
.resultPublisher
.sink {
if case let .failure(storageError) = $0 {
print("Failed: \(storageError.errorDescription). \(storageError.recoverySuggestion)")
}
}
receiveValue: { data in
print("Completed: \(data)")
}

Upload file

When you have a file that you want to upload, you can specify the url to the file in the local parameter. If a file with the same path already exists in S3, the existing S3 file will be overwritten.

With StoragePath

let dataString = "My Data"
let fileName = "myFile.txt"
guard let fileUrl = FileManager.default.urls(
for: .documentDirectory,
in: .userDomainMask
).first?.appendingPathComponent(fileName)
else { return }
try dataString.write(
to: fileUrl,
atomically: true,
encoding: .utf8
)
let uploadTask = Amplify.Storage.uploadFile(
path: .fromString("public/example/path/myFile.txt"),
local: filename
)
Task {
for await progress in await uploadTask.progress {
print("Progress: \(progress)")
}
}
let data = try await uploadTask.value
print("Completed: \(data)")
let dataString = "My Data"
let fileName = "myFile.txt"
guard let fileUrl = FileManager.default.urls(
for: .documentDirectory,
in: .userDomainMask
).first?.appendingPathComponent(fileName)
else { return }
try dataString.write(
to: fileUrl,
atomically: true,
encoding: .utf8
)
let uploadTask = Amplify.Storage.uploadFile(
path: .fromString("public/example/path/myFile.txt"),
local: filename
)
progressSink = uploadTask
.inProcessPublisher
.sink { progress in
print("Progress: \(progress)")
}
resultSink = uploadTask
.resultPublisher
.sink {
if case let .failure(storageError) = $0 {
print("Failed: \(storageError.errorDescription). \(storageError.recoverySuggestion)")
}
}
receiveValue: { data in
print("Completed: \(data)")
}

With Key (Deprecated)

let dataString = "My Data"
let fileNameKey = "myFile.txt"
guard let filename = FileManager.default.urls(
for: .documentDirectory,
in: .userDomainMask
).first?.appendingPathComponent(fileNameKey)
else { return }
try dataString.write(
to: filename,
atomically: true,
encoding: .utf8
)
let uploadTask = Amplify.Storage.uploadFile(
key: fileNameKey,
local: filename
)
Task {
for await progress in await uploadTask.progress {
print("Progress: \(progress)")
}
}
let data = try await uploadTask.value
print("Completed: \(data)")
let dataString = "My Data"
let fileNameKey = "myFile.txt"
guard let filename = FileManager.default.urls(
for: .documentDirectory,
in: .userDomainMask
).first?.appendingPathComponent(fileNameKey)
else { return }
try dataString.write(
to: filename,
atomically: true,
encoding: .utf8
)
let uploadTask = Amplify.Storage.uploadFile(
key: fileNameKey,
local: filename
)
progressSink = uploadTask
.inProcessPublisher
.sink { progress in
print("Progress: \(progress)")
}
resultSink = uploadTask
.resultPublisher
.sink {
if case let .failure(storageError) = $0 {
print("Failed: \(storageError.errorDescription). \(storageError.recoverySuggestion)")
}
}
receiveValue: { data in
print("Completed: \(data)")
}

Working with Security Scoped Resources (from iCloud)

Security scoped resources refer to files that are retrieved from iCloud or other cloud storage providers. You're likely to run into these file types when using system components that provide access to files stored in iCloud, e.g. UIDocumentBrowserViewController.

To upload security scoped resources, you'll need to:

  1. use startAccessingSecurityScopedResource() and stopAccessingSecurityScopedResource() to access the data within security scoped files
  2. temporarily persist the data from the security scoped files in your app's sandbox
  3. upload files using the temporary URLs
  4. delete temporarily persisted files (optional)
struct ScopedResourceFile {
let name: String
let data: Data
}
func getTempUrls(securityScopedUrls: [URL]) -> [URL] {
// 1. get the content of security scoped resources into ScopedResourceFile struct
let fileContents = securityScopedUrls.compactMap { url -> ScopedResourceFile? in
let startAccess = url.startAccessingSecurityScopedResource()
guard startAccess else {
print("Issue accessing security scoped resource at :\(url)")
return nil
}
defer { url.stopAccessingSecurityScopedResource() }
do {
let data = try Data(contentsOf: url)
let fileName = url.lastPathComponent
return ScopedResourceFile(name: fileName, data: data)
} catch {
print("Couldn't create Data from contents of file at url: \(url)")
return nil
}
}
// 2. write the file contents to temporary files and return the URLs of the temp files
let localFileURLs = persistTemporaryFiles(fileContents)
// 3. Now you have local URLs for the files you'd like to upload.
return localFileURLs
}

Cancel, Pause, Resume

Calls to uploadData or uploadFile return a reference to the task that is actually performing the upload.

To cancel the upload (for example, in response to the user pressing a Cancel button), you simply call cancel() on the upload task.

func cancelUpload() {
uploadTask.cancel()
}

You can also pause then resume the task.

uploadTask.pause()
uploadTask.resume()

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

MultiPart upload

Amplify will automatically perform a S3 multipart upload for objects that are larger than 5MB. For more information about S3's multipart upload, see Uploading and copying objects using multipart upload