Soto Swift SDK for AWS

repository·main·Indexed 21 days ago

https://github.com/soto-project/soto

A community-supported Swift SDK for Amazon Web Services (AWS) providing a direct mapping of AWS REST APIs. It supports Linux, macOS, and iOS, utilizing an AWSClient for communication and service-specific objects for API interactions. Includes the SotoCodeGenerator plugin to reduce binary size by generating code only for required services.

Tokens
1.2K
Snippets
3
Records
5
Agent score
27%

What's inside Soto

  1. How AWSClient and Service Objects work together

    main

    To interact with AWS, you must use two primary abstractions:

    1. AWSClient: Manages the underlying communication with AWS, including HTTP client management and request signing. You typically create one client for your application lifecycle.
    2. Service Object: A service-specific object (e.g., S3, DynamoDB) that you initialize with an AWSClient and a specific region. The service object provides the high-level API for that specific AWS service.

    Every service call is a direct mapping of the corresponding AWS REST API.

    import SotoS3
    
    let client = AWSClient(credentialProvider: .static(accessKeyId: "ID", secretAccessKey: "KEY"), httpClientProvider: .createNew)
    let s3 = S3(client: client, region: .uswest2)
  2. Reduce binary size using SotoCodeGenerator

    main
    Because Soto is a large package containing many AWS services, you can use the SotoCodeGenerator Swift Package Manager build plugin. This allows you to generate Swift source code only for the specific services and operations your project actually uses, rather than including the entire library as a dependency.
  3. Install Soto via Swift Package Manager

    main

    To use Soto in your Swift project, add it as a dependency in your Package.swift file. You should then add specific service products (e.g., SotoS3, SotoSES) to your target dependencies to avoid including the entire library if you only need specific services.

        dependencies: [
            .package(url: "https://github.com/soto-project/soto.git", from: "7.0.0")
        ],
    
        targets: [
            .target(name: "MyApp", dependencies: [
                .product(name: "SotoS3", package: "soto"),
                .product(name: "SotoSES", package: "soto"),
                .product(name: "SotoIAM", package: "soto")
            ]),
        ]
  4. Configure AWS Credentials for Soto

    main

    Soto requires AWS credentials to sign requests. You can provide credentials using several methods:

    • Environment Variables: Set AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY.
    • ECS: Use ECS container IAM policies.
    • EC2: Use EC2 IAM instance profiles.
    • Shared Credentials File: Use the standard AWS credentials file in your home directory.
    • Static Credentials: Provide them directly in code at runtime using a .static credential provider.
  5. Example: Using Soto S3 to manage objects

    main

    This example demonstrates how to initialize an AWSClient, create an S3 service object, and perform basic operations like creating a bucket, uploading a string as an object, and retrieving it.

    import SotoS3 //ensure this module is specified as a dependency in your package.swift
    
    let bucket = "my-bucket"
    
    let client = AWSClient(
        credentialProvider: .static(accessKeyId: "Your-Access-Key", secretAccessKey: "Your-Secret-Key"),
        httpClientProvider: .createNew
    )
    let s3 = S3(client: client, region: .uswest2)
    
    func createBucketPutGetObject() async throws -> S3.GetObjectOutput {
        // Create Bucket, Put an Object, Get the Object
        _ = try await s3.createBucket(bucket: bucket)
        // Upload text file to the s3
        let bodyData = "hello world"
        _ = try await s3.putObject(
            acl: .publicRead,
            body: .string(bodyData),
            bucket: bucket,
            key: "hello.txt"
        )
        // download text file just uploaded to S3
        let response = try await s3.getObject(bucket: bucket, key: "hello.txt")
        // print contents of response
        if let body = response.body?.asString() {
            print(body)
        }
        return response
    }