[SWIFT] [IOS] How to get the table name from AWS DynamoDB

Introduction

I decided to develop an application linked with AWS in the project, but unexpectedly there were few articles to refer to, so I had a hard time, so I summarized the procedure. This time I will try to get the table name from DynamoDB.

Development environment

Introduction of Cocoapods

Follow the README procedure in aws-amplify/aws-sdk-ios.

First, install cocoapods.

Terminal


$ gem install cocoapods
$ pod setup

Pod init directly under the project directory

Terminal


$ pod init

Since the podfile has been created, I will describe the AWS service to use

Podfile


target 'aws-sdk' do
  # Comment the next line if you don't want to use dynamic frameworks
  use_frameworks!

  pod 'AWSDynamoDB'
end

After installing the pod, reopen Xcode with xed ..

Terminal


$ pod install --repo-uodate
$ xed .

Settings for connecting to AWS

You need to authenticate with AWS to connect to AWS. As far as I have investigated, there are the following two types.

  1. Authentication using Coginito (this is written in the README of aws-sdk)
  2. Authentication using AWS access-key and secret e-key

I tried both, but Cognito's authentication was cumbersome to set up, so I decided to authenticate with method 2. Please refer to the following for issuing the Key.

How do I create an AWS access key? Create Qiita AWS Access Key

Authentication is set in AppDelegate.swift.

AppDelegate.swift


import UIKit
import AWSCore

@main
class AppDelegate: UIResponder, UIApplicationDelegate {

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {

     //Add the following
        let accessKey = Constans.accessKey
        let secretKey = Constans.secretKey
        let provider = AWSStaticCredentialsProvider(accessKey: accessKey, secretKey: secretKey)

        let configuration = AWSServiceConfiguration(
            region: AWSRegionType.APNortheast1,  //Please change the region as appropriate
            credentialsProvider: provider)

        AWSServiceManager.default().defaultServiceConfiguration = configuration

        return true
    }
}

The management of the accessKey and secretKey of AWSStaticCredentialsProvider (accessKey: accessKey, secretKey: secretKey) is left to you individually. (Please be careful not to publish it.)

In my case, I managed it with cocoapods-keys in the case, but this time I created a constant class simply. The created file is set to .gitignore so that it will not be pushed. orta/cocoapods-keys

Constants.swift


import Foundation

class Constans {
    static let accessKey = "AWS access-key"
    static let secretKey = "AWS secret-key"
}

Get table name from AWS DynamoDB

ViewController.swift


import UIKit
import AWSDynamoDB

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        fetchDynamoDBTableName()
    }
    
    private func fetchDynamoDBTableName() {
        let dynamoDB = AWSDynamoDB.default()
        let listTableInput = AWSDynamoDBListTablesInput()
        dynamoDB.listTables(listTableInput!).continueWith { (task:AWSTask<AWSDynamoDBListTablesOutput>) -> Any? in
            if let error = task.error as? NSError {
            print("Error occurred: \(error)")
                return nil
            }

            let listTablesOutput = task.result

            for tableName in listTablesOutput!.tableNames! {
                print("\(tableName)")
            }

            return nil
        }
    }
}

When you build with this, you should see the table name that exists in DynamoDB on the console. If there are multiple tables, multiple are displayed. I described the method in ViewController, but since ViewController becomes Fat, I think it is better to create a class for DynamoDM.

This repository

For your reference. https://github.com/shungo0525/aws-sdk

in conclusion

This time I only get the table name of DynamoDB, but I would like to post about the acquisition of DB data and the cooperation with S3.

Recommended Posts

[IOS] How to get the table name from AWS DynamoDB
[IOS] How to get data from DynamoDB
[Java] How to extract the file name from the path
[Rails] How to change the column name of the table
How to get the class name / method name running in Java
How to get the longest information from Twitter as of 12/12/2016
[IOS14] How to get Data type image data directly from PHPickerViewController?
[Swift UI] How to get the startup status of the application [iOS]
[For beginners] How to get the Ruby delayed railway line name
[Java] How to convert from String to Path type and get the path
Get the column name from the Model instance
[Java] How to get the current directory
How to get the date in java
How to get the setting value (property value) from the database in Spring Framework
How to get a heapdump from a Docker container
How to get Class from Element in Java
How to get today's day of the week
How to get SIMD optimization from HotSpot JavaVM
[Java] How to get the redirected final URL
[Java] How to get the authority of the folder
graphql-ruby: How to get the name of query or mutation in controller Note
How to get the date from the JavaScript Date type that C # developers are addicted to
[Java] How to get the URL of the transition source
From the introduction of devise to the creation of the users table
How to write Scala from the perspective of Java
List how to learn from Docker to AKS on AWS
[Ruby on Rails] How to change the column name
[Java] How to get the maximum value of HashMap
[Android] How to get the setting language of the terminal
How to determine the look-ahead request (Prefetch) from the browser
How to download images from AWS S3 (rails, carrierwave)
[Rails] How to get the contents of strong parameters
[Ruby] How to get the tens place and the ones place
[Swift] How to get the document ID of Firebase
How to get jdk etc from oracle with cli
How to get the class name of the argument of LoggerFactory.getLogger when using SLF4J in Java
Use remotes :: install_github ("github repository name") to get resources from GitHub to the R runtime environment
[Ruby On Rails] How to search and save the data of the parent table from the child table
[AWS] How to check logs
How to transition from the [Swift5] app to the iPhone settings screen
How to dynamically change the column name acquired by MyBatis
Moving from AWS to PaizaCloud
Get to the abbreviations from 5 examples of iterating Java lists
[iOS] [Objective-C] How to update a widget from an Objective-C app
How to get inside a container running on AWS Fargate
How to create a form to select a date from the calendar
How to disable Set-Cookie from API on the front side
How to get the log when install4j does not start
[iOS] How to read Carthage
Learn how to customize the Navigation Bar from the sample app
How to migrate from JUnit4 to JUnit5
[jsoup] How to get the full amount of a document
[Docker] How to access the host from inside the container. http://host.docker.internal:
How to change the file name with Xcode (Refactor Rename)
How to apply C code format from the command line
[Rails] How to get the URL of the transition source and redirect
How to create a registration / update function where the table crosses
How to set the IP address and host name of CentOS8
How to get the contents of Map using for statement Memorandum
How to run a Kotlin Coroutine sample from the command line
How to get the id of PRIMAY KEY auto_incremented in MyBatis