Page updated Jan 16, 2024

Upload files

Amplify iOS v1 is now in Maintenance Mode until May 31st, 2024. This means that we will continue to include updates to ensure compatibility with backend services and security. No new features will be introduced in v1.

Please use the latest version (v2) of Amplify Library for Swift to get started.

If you are currently using v1, follow these instructions to upgrade to v2.

Amplify libraries should be used for all new cloud connected applications. If you are currently using the AWS Mobile SDK for iOS, you can access the documentation here.

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

1let dataString = "My Data"
2let data = dataString.data(using: .utf8)!
3let storageOperation = Amplify.Storage.uploadData(
4 key: "ExampleKey",
5 data: data,
6 progressListener: { progress in
7 print("Progress: \(progress)")
8 }, resultListener: { event in
9 switch event {
10 case .success(let data):
11 print("Completed: \(data)")
12 case .failure(let storageError):
13 print("Failed: \(storageError.errorDescription). \(storageError.recoverySuggestion)")
14 }
15 }
16)
1let dataString = "My Data"
2let data = dataString.data(using: .utf8)!
3let storageOperation = Amplify.Storage.uploadData(key: "ExampleKey", data: data)
4let progressSink = storageOperation.progressPublisher.sink { progress in print("Progress: \(progress)") }
5let resultSink = storageOperation.resultPublisher.sink {
6 if case let .failure(storageError) = $0 {
7 print("Failed: \(storageError.errorDescription). \(storageError.recoverySuggestion)")
8 }
9}
10receiveValue: { data in
11 print("Completed: \(data)")
12}

When you have a file that you want to upload, you can specify the url to the file in the local parameter.

1let dataString = "My Data"
2let fileNameKey = "myFile.txt"
3let filename = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]
4 .appendingPathComponent(fileNameKey)
5do {
6 try dataString.write(to: filename, atomically: true, encoding: String.Encoding.utf8)
7} catch {
8 print("Failed to write to file \(error)")
9}
10
11let storageOperation = Amplify.Storage.uploadFile(
12 key: fileNameKey,
13 local: filename,
14 progressListener: { progress in
15 print("Progress: \(progress)")
16 }, resultListener: { event in
17 switch event {
18 case let .success(data):
19 print("Completed: \(data)")
20 case let .failure(storageError):
21 print("Failed: \(storageError.errorDescription). \(storageError.recoverySuggestion)")
22 }
23 }
24)
1let dataString = "My Data"
2let fileNameKey = "myFile.txt"
3let filename = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]
4 .appendingPathComponent(fileNameKey)
5do {
6 try dataString.write(to: filename, atomically: true, encoding: String.Encoding.utf8)
7} catch {
8 print("Failed to write to file \(error)")
9}
10
11let storageOperation = Amplify.Storage.uploadFile(key: fileNameKey, local: filename)
12let progressSink = storageOperation.progressPublisher.sink { progress in print("Progress: \(progress)") }
13let resultSink = storageOperation.resultPublisher.sink {
14 if case let .failure(storageError) = $0 {
15 print("Failed: \(storageError.errorDescription). \(storageError.recoverySuggestion)")
16 }
17}
18receiveValue: { data in
19 print("Completed: \(data)")
20}

Cancel, Pause, Resume

Calls to uploadData or uploadFile return a reference to the operation 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 operation.

1func cancelUpload() {
2 storageOperation.cancel()
3}

You can also pause then resume the operation.

Note that pause internally uses the suspend api of NSURLSessionTask, which suspends the task temporarily and does not fully pause the transfer. Complete pausing of task is tracked in this Github issue - https://github.com/aws-amplify/aws-sdk-ios/issues/3668

1storageOperation.pause()
2storageOperation.resume()

MultiPart upload

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