Swiftpack.co - Package - swift-aws/aws-sdk-appleos

AWSSDK AppleOS

Version of the AWS SDK for the Swift programming language that supports Apple platforms (including iOS) as first class citizens. This repository is based off the aws-sdk-swift repository. As this version is reliant on Network.framework it only works for Apple platforms, but it does support iOS unlike aws-sdk-swift.

Swift 5.0

Supported Platforms and Swift Versions

| Platform | Version | |---|:---:| |iOS | v12.2 | |macOS | v10.14 | |tvOS | v12.2 |

Documentation

Visit the aws-sdk-swift documentation for instructions and browsing api references.

Swift NIO

This client utilizes Swift NIO to power its interactions with AWS. It returns an EventLoopFuture in order to allow non-blocking frameworks to use this code. This version of aws-adk-swift uses the NIOTransportServices to provide network connectivity. The NIOTransportServices package is reliant on Network.framework. This means it can support all Apple platforms but is not available for Linux. Please see the Swift NIO documentation for more details, and please let us know via an Issue if you have questions!

Installation

Swift Package Manager

AWS SDK Apple OS uses the Swift Package manager. It is recommended you create a swift package that only includes the AWS services you are interested in. The easiest method to do this is create an empty folder, enter this folder and type swift package init. This will create a Package.swift file. Add the package dependency on package https://github.com/swift-aws/aws-sdk-appleos.git. Add the services you are using in the dependency list for your target. See below for an example. Make sure you add the line indicating the platforms you are targetting. In future versions of XCode it will be possible to include Swift Package files directly into your project. In the meantime the method to add aws-sdk-appleos into your project is as follows.

  • Create an xcodeproj file. Run swift package generate-xcodeproj in the same folder as your Package.swift file.
  • Open your project and add the generated xcodeproj in to your project.
  • Include the framework for the services you require in the Embedded Binaries for the project.

Example Package.swift

import PackageDescription

let package = Package(
    name: "MyAWSLib",
    platforms: [.iOS("12.2"), .macOS(.v10_14)],
    dependencies: [
        .package(url: "https://github.com/swift-aws/aws-sdk-appleos.git", from: "0.1.0")
    ],
    targets: [
      .target(
          name: "MyAWSLib",
          dependencies: ["S3", "SES", "CloudFront", "ELBV2", "IAM", "Kinesis"]),
      .testTarget(
          name: "MyAWSToolTests",
          dependencies: ["MyAWSLib"]),
    ]
)

Carthage

Not supported yet

Cocoapods

Not supported yet

Contributing

All developers should feel welcome and encouraged to contribute to aws-sdk-swift.

As contributors and maintainers of this project, and in the interest of fostering an open and welcoming community, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.

To contribute a feature or idea to aws-sdk-swift, submit an issue and fill in the template. If the request is approved, you or one of the members of the community can start working on it. It is prefereable that pull requests are made in the original swift repositories and not the appleos versions of the code. If you have a change that is specific to apple OS then make a pull request to the appleos branch in repositories aws-sdk-swift or aws-sdk-swift-core.

If you find a bug, please submit an issue with a failing test case displaying the bug or create an issue.

If you find a security vulnerability, please contact yuki@miketokyo.com and reach out on the #aws channel on the Vapor Discord as soon as possible. We take these matters seriously.

Configuring Credentials

Before using the SDK, ensure that you've configured credentials.

Pass the Credentials to the AWS Service struct directly

All of the AWS Services's initializers accept accessKeyId and secretAccessKey

let ec2 = EC2(
    accessKeyId: "YOUR_AWS_ACCESS_KEY_ID",
    secretAccessKey: "YOUR_AWS_SECRET_ACCESS_KEY"
)

Using the aws-sdk-swift

AWS Swift Modules can be imported into any swift project. Each module provides a struct that can be initialized, with instance methods to call aws services. See documentation for details on specific services.

The underlying aws-sdk-swift httpclient returns a swift-nio EventLoopFuture object. An EvenLoopFuture is not the response, but rather a container object that will be populated with the response sometime later. In this manner calls to aws do not block the main thread.

However, operations that require inspection or use of the response require code to be written in a slightly different manner that equivalent synchronous logic. There are numerous references available online to aid in understanding this concept.

The recommended manner to interact with futures is chaining.

import S3 //ensure this module is specified as a dependency in your package.swift
import NIO

do {
    let bucket = "my-bucket"

    let s3 = S3(
        accessKeyId: "Your-Access-Key",
        secretAccessKey: "Your-Secret-Key",
        region: .uswest2
    )

    // Create Bucket, Put an Object, Get the Object
    let createBucketRequest = S3.CreateBucketRequest(bucket: bucket)

    try s3.createBucket(createBucketRequest).thenThrowing { response -> Future<S3.PutObjectOutput> in
        // Upload text file to the s3
        let bodyData = "hello world".data(using: .utf8)!
        let putObjectRequest = S3.PutObjectRequest(acl: .publicRead, bucket: bucket, contentLength: Int64(bodyData.count), body: bodyData, key: "hello.txt")
        return try s3.putObject(putObjectRequest)
    }.thenThrowing { response -> Future<S3.GetObjectOutput> in
        let getObjectRequest = S3.GetObjectRequest(bucket: bucket, key: "hello.txt")
        return try s3.getObject(getObjectRequest)
    }.whenSuccess { futureResponse in
        futureResponse.whenSuccess { response in
          if let body = response.body {
              print(String(data: body, encoding: .utf8))
          }
        }
    }
} catch {
    print(error)
}

Or you can use the nested method

import S3 //ensure this module is specified as a dependency in your package.swift

do {
    let bucket = "my-bucket"

    let s3 = S3(
        accessKeyId: "Your-Access-Key",
        secretAccessKey: "Your-Secret-Key",
        region: .uswest1
    )

    // Create Bucket, Put an Object, Get the Object
    let createBucketRequest = S3.CreateBucketRequest(bucket: bucket)

    try s3.createBucket(createBucketRequest).whenSuccess { response in
        do {
            let bodyData = "hello world".data(using: .utf8)!
            let putObjectRequest = S3.PutObjectRequest(acl: .publicRead, key: "hello.txt", body: bodyData, contentLength: Int64(bodyData.count), bucket: bucket)

            try s3.putObject(putObjectRequest).whenSuccess { response in
                do {
                    let getObjectRequest = S3.GetObjectRequest(bucket: bucket, key: "hello.txt")
                    try s3.getObject(getObjectRequest).whenSuccess { response in
                        if let body = response.body {
                            print(String(data: body, encoding: .utf8))
                        }
                    }
                } catch { print(error) }
            }

        } catch { print(error) }
     }
} catch { print(error) }

License

aws-sdk-swift is released under the Apache License, Version 2.0. See LICENSE for details.

Github

link
Stars: 6
Help us keep the lights on

Used By

Total:

Releases

0.3.1 - Jul 8, 2019

  • Using release 0.2.2 of aws-sdk-appleos-core
  • Fixed Backup and DirectoryService name clashes with system frameworks. Appended "AWS" at front of name.
  • Fixed PlatformValues enum in EC2.

0.3.0 - Jul 1, 2019

  • Using release v0.2.1 of aws-sdk-appleos-core
  • Sync service models files to v1.20.11. See aws-sdk-go versions 1.19.44 to 1.20.11 for details of changes.
  • Added new services ApiGatewayManagementApi, ApiGatewayV2 ApplicationInsights DocDB EC2InstanceConnect GroundStation IoTEvents, IoTEventsData, IoTThingsGraph ManagedBlockchain MediaPackageVod Personalize, PersonalizeEvents, PersonalizeRuntime ServiceQuotas Textract WorkLink

0.2.0 - Jun 26, 2019

  • Output an empty body when the payload body is nil
  • Added collection encoding to shape member. Use this to make decisions on encoding/decoding of arrays and dictionaries in XML and queries (fixes multiple issues across XML and query based services)
  • XML can decode dictionaries with enum keys (fixes SQS.GetQueueAttributesRequest)
  • Percent encode more characters placed in query body (fixes SQS.DeleteMessage, SNS.CreatePlatformApplication or any query based request that requires the '+' sign.)
  • Add ability to flatten all arrays in query encoder (fixes multiple issues with EC2)

0.1.0 - Jun 18, 2019

Initial version of aws-sdk-appleos

  • Based on NIO2.0 version of aws-sdk-swift
  • Compiles for macOS, iOS, tvOS
  • Added custom HttpClient that is setup to use NIOTransportServices
  • Added custom version of XMLNode library for iOS, tvOS as it is not available for them