Working with S3 and Cloud Storage in Swift (iOS/macOS): AWSS3 and Amplify

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Working with S3 and Cloud Storage in Swift (iOS/macOS)



In modern mobile and desktop application development with Swift, there is often a need to store and transfer large volumes of data: images, videos, user documents. Amazon S3 (Simple Storage Service) is one of the most popular cloud solutions for these tasks. In this article, we will explore how to integrate S3 into Swift applications for iOS and macOS using the official SDK from Amazon — AWSS3, and also consider a higher-level approach through the Amplify framework.



Why Use S3 in Swift Applications?

S3 provides scalable, secure, and cost-effective object storage. Using S3 in Swift allows you to:

  • Offload file storage from the device to the cloud.
  • Provide access to data from any user device.
  • Use built-in encryption and access control mechanisms.
  • Integrate with other AWS services (Lambda, Cognito, CloudFront).


Environment Setup

To get started, you will need:

  • An AWS account and a created S3 bucket.
  • Xcode 14+ and a Swift project (iOS 15+ / macOS 12+).
  • Installing dependencies via Swift Package Manager (SPM).


Installing AWSS3 via SPM

Add the package to Package.swift or through the Xcode interface:

// File → Add Packages → search: https://github.com/aws-amplify/aws-sdk-ios-spm// Select "AWSS3" from the list of products.

In your code, import the library:

import AWSS3


Basic Example: Uploading a File to S3

Let's consider a simple scenario — uploading an image from the user's gallery. Cognito Identity Pool is used for authentication, but in test mode, you can pass keys directly (not recommended for production).



Service Configuration

let credentialsProvider = AWSStaticCredentialsProvider(    accessKey: "YOUR_ACCESS_KEY",    secretKey: "YOUR_SECRET_KEY")let configuration = AWSServiceConfiguration(    region: .USEast1,    credentialsProvider: credentialsProvider)AWSServiceManager.default().defaultServiceConfiguration = configuration


Uploading a File (upload)

func uploadImageToS3(imageData: Data, bucketName: String, key: String) {    let request = AWSS3TransferManagerUploadRequest()!    request.bucket = bucketName    request.key = key    request.body = imageData    request.contentLength = Int64(imageData.count)

let transferManager = AWSS3TransferManager.default() transferManager?.upload(request).continueWith { task -> Any? in if let error = task.error { print("Upload error: \\(error.localizedDescription)") } else { print("File successfully uploaded: \\(bucketName)/\\(key)") } return nil }}


Downloading a File (download)

func downloadFromS3(bucketName: String, key: String, localURL: URL) {    let request = AWSS3TransferManagerDownloadRequest()!    request.bucket = bucketName    request.key = key    request.downloadingFileURL = localURL

let transferManager = AWSS3TransferManager.default() transferManager?.download(request).continueWith { task -> Any? in if let error = task.error { print("Download error: \\(error.localizedDescription)") } else { print("File saved to \\(localURL.path)") } return nil }}


Using Amplify Storage for S3

Amplify provides a simpler API, hiding the details of working with S3. It requires preliminary setup via the CLI or the amplifyconfiguration.json file.



Installing Amplify

// Add the package: https://github.com/aws-ampl                    

Recommendations