Code Monkey home page Code Monkey logo

aws-sdk-swift's Introduction

aws-sdk-swift

AWS SDK for the Swift programming language. This library doesn't depend on Objective-C Runtime, So you can use this with Linux.

Supported Platforms and Swift Versions

Swift 4.2 Swift 5.0
macOS
Ubuntu 14.04
Ubuntu 16.04
Ubuntu 18.04

Documentation

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

Installation

Swift Package Manager

Package.swift

import PackageDescription

let package = Package(
    name: "MyAWSApp",
    dependencies: [
        .package(url: "https://github.com/swift-aws/aws-sdk-swift.git", from: "3.0.0")
    ],
    targets: [
      .target(
          name: "MyAWSApp",
          dependencies: ["S3", "SES", "CloudFront", "ELBV2", "IAM", "Kinesis"]),
      .testTarget(
          name: "MyAWSToolTests",
          dependencies: ["MyAWSApp"]),
    ]
)

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.

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

If you find a security vulnerability, please contact [email protected] 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.

Via EC2 Instance Profile

If you are running your code on an AWS EC2 instance, you can setup an IAM role as the server's Instance Profile to automatically grant credentials via the metadata service.

There are no code changes or configurations to specify in the code, it will automatically pull and use them.

Via ECS Container credentials

If you are running your code as an AWS ECS container task, you can setup an IAM role for your container task to automatically grant credentials via the metadata service.

There are no code changes or configurations to specify in the code, it will automatically pull and use them.

Load Credentials from shared credential file.

You can set shared credentials in the home directory for the user running the app

in ~/.aws/credentials,

[default]
aws_access_key_id = YOUR_AWS_ACCESS_KEY_ID
aws_secret_access_key = YOUR_AWS_SECRET_ACCESS_KEY

Load Credentials from Environment Variable

Alternatively, you can set the following environment variables:

AWS_ACCESS_KEY_ID=YOUR_AWS_ACCESS_KEY_ID
AWS_SECRET_ACCESS_KEY=YOUR_AWS_SECRET_ACCESS_KEY

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) }

upgrading from <3.0.x

The simplest way to upgrade from an existing 1.0 or 2.0 implementation is to call .wait() on existing synchronous calls.

However it is recommend to rewrite your synchronous code to work with the returned future objects. It is no longer necessary to use a DispatchQueue.

Using the aws-sdk-swift with Vapor

Integration with vapor is pretty straight forward.

import Vapor
import HTTP
import SES

final class MyController {

     func sendUserEmailFromJSON(_ req: Request) throws -> Future<HTTPStatus> {

          return try req.content.decode(EmailData.self).map { emailData in
              return emailData
          }.flatMap(to: HTTPStatus.self) { emailData -> Future<HTTPStatus> in
              do {
                  let client = SES(
                      accessKeyId: "Your-Access-Key",
                      secretAccessKey: "Your-Secret-Key",
                      region: .uswest1
                  )

                  let sendEmailRequest = SES.SendEmailRequest(
                      destination: emailData.address,
                      message: emailData.message
                  )

                  return try client.sendEmail(sendEmailRequest)
                    .hopTo(eventLoop: req.eventLoop)
                    .map { response -> HTTPResponseStatus in
                      // print(response)
                      return HTTPStatus.ok
                  }
              } catch {
                  // print(error)
                  return req.eventLoop.newSucceededFuture(result: HTTPStatus.internalServerError)
              }
          }
     }
}

Using the aws-sdk-swift with the swift REPL (os X)

$ brew install libressl
$ swift -I .build/debug -L/usr/local/Cellar/libressl/2.7.2/lib -lssl -lcrypto -I/usr/local/Cellar/libressl/2.7.2/include
1> import Foundation
2> import S3

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
        print(response)
    }
} catch { print(error) }

Speed Up Compilation

By specifying only those modules necessary for your application, only those modules will compile which makes for fast compilation.

If you want to create a module for your service, you can try using the module-exporter to build a separate repo for any of the modules.

License

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

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.