1
0

More than 3 years have passed since last update.

Cognito認証を経てS3バケットにアップロード

Last updated at Posted at 2021-08-08

はじめに

Cognitoでユーザープールでの認証完了後、IDプールと連携してS3バケットにアップロードするために必要なAWS設定を調べてみました。Cognitoユーザープールの設定やSwiftでのCognitoによるサインインやS3バケットへのアップロードを行うコードについては大幅に割愛していますのでご了承ください。

AWS設定手順

S3:バケットの作成

デフォルトの設定でバケットを作成します。

Cognito:IDプールの設定

CognitoのIDプールの管理画面から新しい ID プールの作成を押してIDプールを作成します。IDプール名を入力後、認証プロバイダーに作成済みのユーザープールIDとアプリクライアントIDを指定しプールの作成を押します。
名称未設定.png

次に表示される画面ではロールを作成します。上の段(Your authenticated identities...)のポリシードキュメントを表示し、編集ボタンを押して編集します。今回はアップロードすることが目的なので、"s3:PutObject*"を追加します。編集が終わったら画面下方の許可ボタンを押して次に進みます。
名称未設定2.png

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "s3:PutObject*",
                "mobileanalytics:PutEvents",
                "cognito-sync:*",
                "cognito-identity:*"
            ],
            "Resource": [
                "*"
            ]
        }
    ]
}

サンプルコードが表示されひとまず設定終了です。identityPoolIdが後々必要になりますが、この画面は後からでも確認することができます。
名称未設定3.png

Swiftによる実装

パッケージの追加

podで以下のパッケージを追加します。

  # For Amazon Cognito.
  pod 'AWSCognitoIdentityProvider', '~> 2.12.0'
  pod 'AWSS3'

AppDelegate

リージョンやIDなどを指定します。

import AWSCognitoIdentityProvider
import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    private let REGION: AWSRegionType = リージョン
    private let USER_POOL_ID: String = ユーザープールID
    private let ID_POOL_ID: String = IDプールのサンプルコードのidentityPoolId
    private let APP_CLIENT_ID: String = アプリクライアントID
    private let APP_CLIENT_SECRET: String? = nil
    private let KEY: String = "UserPool"

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

        let serviceConfiguration = AWSServiceConfiguration(
            region: REGION,
            credentialsProvider: nil
        )

        let userPoolConfigration = AWSCognitoIdentityUserPoolConfiguration(
            clientId: APP_CLIENT_ID,
            clientSecret: APP_CLIENT_SECRET,
            poolId: USER_POOL_ID
        )

        AWSCognitoIdentityUserPool.register(
            with: serviceConfiguration,
            userPoolConfiguration: userPoolConfigration,
            forKey: KEY
        )

        // Amazon Cognito 認証情報プロバイダーを初期化します
        let pool = AWSCognitoIdentityUserPool(forKey: KEY)
        // IDプールで表示されるサンプルはidentityProviderManagerは指定されていませんが、指定した方が良いようです
        let credentialsProvider = AWSCognitoCredentialsProvider(
            regionType: REGION,
            identityPoolId: ID_POOL_ID,
            identityProviderManager:pool
        )
        let configuration = AWSServiceConfiguration(
            region: REGION,
            credentialsProvider:credentialsProvider
        )
        AWSServiceManager.default().defaultServiceConfiguration = configuration

        return true
    }

CognitoユーザーによるサインインやS3アップロードのコードは割愛させていただきます。

その他

認証しないでアップロードを試みるとtransferUtility.uploadDataの実行時に以下のようなエラーが発生するのでおそらくうまく行っているのではないかと思っています。

Authentication delegate not set

ツッコミどころ満載と思いますが、不備等あればご指摘いただければ幸いです。

参考文献

Amazon Cognito デベロッパーガイド

1
0
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0