建立 Amazon Cognito 身分集區 - AWSSDK 程式碼範例

AWS文件 AWS SDK 範例 GitHub 存放庫中提供了更多 SDK 範例

本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。

建立 Amazon Cognito 身分集區

下列程式碼範例示範如何建立 Amazon Cognito 身份集區。

CLI
AWS CLI

若要使用 Cognito 身分集區提供者建立身分集區

此範例會建立名為的身分集區 MyIdentityPool。它具有 Cognito 身分集區提供者。不允許使用未驗證的身分。

命令:

aws cognito-identity create-identity-pool --identity-pool-name MyIdentityPool --no-allow-unauthenticated-identities --cognito-identity-providers ProviderName="cognito-idp.us-west-2.amazonaws.com/us-west-2_aaaaaaaaa",ClientId="3n4b5urk1ft4fl3mg5e62d9ado",ServerSideTokenCheck=false

輸出:

{ "IdentityPoolId": "us-west-2:11111111-1111-1111-1111-111111111111", "IdentityPoolName": "MyIdentityPool", "AllowUnauthenticatedIdentities": false, "CognitoIdentityProviders": [ { "ProviderName": "cognito-idp.us-west-2.amazonaws.com/us-west-2_111111111", "ClientId": "3n4b5urk1ft4fl3mg5e62d9ado", "ServerSideTokenCheck": false } ] }
Java
適用於 Java 2.x 的 SDK
注意

還有更多關於 GitHub。尋找完整範例,並了解如何在AWS 設定和執行程式碼範例儲存庫

import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.cognitoidentity.CognitoIdentityClient; import software.amazon.awssdk.services.cognitoidentity.model.CreateIdentityPoolRequest; import software.amazon.awssdk.services.cognitoidentity.model.CreateIdentityPoolResponse; import software.amazon.awssdk.services.cognitoidentityprovider.model.CognitoIdentityProviderException; /** * Before running this Java V2 code example, set up your development * environment, including your credentials. * * For more information, see the following documentation topic: * * https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html */ public class CreateIdentityPool { public static void main(String[] args) { final String usage = """ Usage: <identityPoolName>\s Where: identityPoolName - The name to give your identity pool. """; if (args.length != 1) { System.out.println(usage); System.exit(1); } String identityPoolName = args[0]; CognitoIdentityClient cognitoClient = CognitoIdentityClient.builder() .region(Region.US_EAST_1) .build(); String identityPoolId = createIdPool(cognitoClient, identityPoolName); System.out.println("Unity pool ID " + identityPoolId); cognitoClient.close(); } public static String createIdPool(CognitoIdentityClient cognitoClient, String identityPoolName) { try { CreateIdentityPoolRequest poolRequest = CreateIdentityPoolRequest.builder() .allowUnauthenticatedIdentities(false) .identityPoolName(identityPoolName) .build(); CreateIdentityPoolResponse response = cognitoClient.createIdentityPool(poolRequest); return response.identityPoolId(); } catch (CognitoIdentityProviderException e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } return ""; } }
Swift
適用於 Swift 的 SDK
注意

這是適用於預覽版本 SDK 的發行前版本文件。內容可能變動。

注意

還有更多關於 GitHub。尋找完整範例,並了解如何在AWS 設定和執行程式碼範例儲存庫

建立新的身分集區。

/// Create a new identity pool and return its ID. /// /// - Parameters: /// - name: The name to give the new identity pool. /// /// - Returns: A string containing the newly created pool's ID, or `nil` /// if an error occurred. /// func createIdentityPool(name: String) async throws -> String? { let cognitoInputCall = CreateIdentityPoolInput(developerProviderName: "com.exampleco.CognitoIdentityDemo", identityPoolName: name) let result = try await cognitoIdentityClient.createIdentityPool(input: cognitoInputCall) guard let poolId = result.identityPoolId else { return nil } return poolId }