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

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 iOS and macOS development, there is often a need to integrate cloud storage compatible with the Amazon S3 (Simple Storage Service) protocol. This can be either official AWS S3 or self-hosted solutions like MinIO or DigitalOcean Spaces. For Swift, there is a powerful tool — the AWSS3 library from the AWS SDK for Swift, which allows you to easily manage objects in buckets: upload, download, delete, and retrieve metadata.



In this article, we will cover how to configure AWSS3 in a project, perform basic file operations, and handle possible errors. The material is suitable for iOS and macOS applications written in Swift 5.7+.



Installing and Configuring AWSS3



To work with AWSS3, you need to add the dependency via Swift Package Manager (SPM) or CocoaPods. Let's consider SPM — the most modern method.



  1. Open Xcode, go to File → Add Packages...
  2. Enter the repository URL: https://github.com/awslabs/aws-sdk-swift.git
  3. Select the AWSS3 package and specify the version (latest stable is recommended).
  4. Add the package to the target.


After installation, import the module into the file where storage operations will occur:



import AWSS3


S3 Client Configuration



To connect to an S3-compatible storage, you need to create a configuration with credentials (Access Key, Secret Key) and an endpoint. For AWS S3, the endpoint can be omitted — the SDK will automatically determine the region. For MinIO or other services, the endpoint is required.



// Example configuration for MinIO (local server)let endpoint = "http://localhost:9000"let accessKey = "YOUR_ACCESS_KEY"let secretKey = "YOUR_SECRET_KEY"

let credentials = AWSCredentials(accessKey: accessKey, secretAccessKey: secretKey)let region = AWSRegion.useast1 // or another region

let configuration = AWSS3Configuration( region: region, endpoint: endpoint, credentialsProvider: .static(credentials), useFIPS: false, useDualStack: false)

let s3Client = try await AWSS3Client(configuration: configuration)


For AWS S3, the configuration is simpler — just specify the region and obtain credentials from an IAM role or environment variables:



let configuration = AWSS3Configuration(    region: .useast1,    credentialsProvider: .default)let s3Client = try await AWSS3Client(configuration: configuration)


Basic S3 Object Operations



1. Uploading a File



To upload data to a bucket, use the putObject method. The data can be Data, String, or a stream.



func uploadFile(bucket: String, key: String, data: Data) async throws {    let putObjectInput = PutObjectInput(        body: .data(data),        bucket: bucket,        key: key    )    let response = try await s3Client.putObject(input: putObjectInput)    print("File uploaded. ETag: \\(response.eTag ?? "unknown")")}


2. Downloading a File



To retrieve an object, use getObject:



func downloadFile(bucket: String, key: String) async throws -> Data {    let getObjectInput = GetObjectInput(        bucket: bucket,        key: key    )    let response = try await s3Client.getObject(input: getObjectInput)    guard let body = response.body else {        throw NSError(domain: "S3Error", code: -1, userInfo: [NSLocalizedDescriptionKey: "Empty response body"])    }    let data = try await body.readData()    return data}


3. Listing Objects in a Bucket



The listObjectsV2 method returns a list of keys (file names) in the bucket:




                
    
    

Recommendations