Amplify v2 互換性
Amplify V2 認証と AWS Android SDK プラグインの使用
AWS Android SDK ではなく AWS Kotlin SDK と共に Amplify v2 を使用することをお勧めします。既存の実装をサポートするために、このガイドでは Amplify v2 で AWS Android SDK プラグインを引き続き使用する方法を説明します。
AmplifyCredentialsProvider の作成
多くの AWS Android SDK プラグインは、カスタム AWSCredentialsProvider 実装を受け入れます。Amplify Android v2 を使用して認証情報を提供する独自の AWSCredentialsProvider を実装できます。
import com.amazonaws.auth.AWSCredentials;import com.amazonaws.auth.AWSCredentialsProvider;import com.amazonaws.auth.BasicSessionCredentials;import com.amplifyframework.auth.AWSTemporaryCredentials;import com.amplifyframework.auth.cognito.AWSCognitoAuthSession;import com.amplifyframework.auth.options.AuthFetchSessionOptions;import com.amplifyframework.core.Amplify;
import java.util.concurrent.CompletableFuture;
class AmplifyCredentialsProvider implements AWSCredentialsProvider {
@Override public AWSCredentials getCredentials() { CompletableFuture<AWSCredentials> sdkCredentials = new CompletableFuture<>();
Amplify.Auth.fetchAuthSession((authSession) -> { BasicSessionCredentials credentials = null; if (authSession instanceof AWSCognitoAuthSession) { AWSCognitoAuthSession cognitoAuthSession = (AWSCognitoAuthSession) authSession; com.amplifyframework.auth.AWSCredentials awsCredentials = cognitoAuthSession.getAwsCredentialsResult().getValue(); if (awsCredentials instanceof AWSTemporaryCredentials) { AWSTemporaryCredentials temporaryAwsCredentials = (AWSTemporaryCredentials) awsCredentials; credentials = new BasicSessionCredentials( temporaryAwsCredentials.getAccessKeyId(), temporaryAwsCredentials.getSecretAccessKey(), temporaryAwsCredentials.getSessionToken() ); } }
if (credentials != null) { sdkCredentials.complete(credentials); } else { sdkCredentials.completeExceptionally( new RuntimeException("Failed to get credentials") ); } }, (exception) -> sdkCredentials.completeExceptionally( new RuntimeException("Failed to get credentials", exception) ));
return sdkCredentials.join(); }
@Override public void refresh() { CompletableFuture<Void> result = new CompletableFuture<>(); Amplify.Auth.fetchAuthSession( AuthFetchSessionOptions.builder().forceRefresh(true).build(), // We do not need to capture value if refresh succeeds (authSession) -> result.complete(null), // We do not need to throw if refresh fails (exception) -> result.complete(null) );
result.join(); }}import com.amazonaws.auth.AWSCredentialsimport com.amazonaws.auth.AWSCredentialsProviderimport com.amazonaws.auth.BasicSessionCredentialsimport com.amplifyframework.auth.AWSTemporaryCredentialsimport com.amplifyframework.auth.cognito.AWSCognitoAuthSessionimport com.amplifyframework.auth.options.AuthFetchSessionOptionsimport com.amplifyframework.core.Amplifyimport java.lang.RuntimeExceptionimport kotlin.coroutines.resumeimport kotlin.coroutines.resumeWithExceptionimport kotlin.coroutines.suspendCoroutineimport kotlinx.coroutines.runBlocking
class AmplifyCredentialsProvider : AWSCredentialsProvider {
override fun getCredentials(): AWSCredentials = runBlocking { suspendCoroutine { continuation -> Amplify.Auth.fetchAuthSession( { authSession -> val awsTemporaryCredentials = (authSession as? AWSCognitoAuthSession) ?.awsCredentialsResult?.value as? AWSTemporaryCredentials
val sdkCredentials = awsTemporaryCredentials?.let { BasicSessionCredentials(it.accessKeyId, it.secretAccessKey, it.sessionToken) }
if (sdkCredentials != null) { continuation.resume(sdkCredentials) } else { val authException = RuntimeException("Failed to get credentials") continuation.resumeWithException(authException) } }, { continuation.resumeWithException( RuntimeException("Failed to get credentials. See exception.", it) ) } ) } }
override fun refresh() = runBlocking { suspendCoroutine { continuation -> Amplify.Auth.fetchAuthSession( AuthFetchSessionOptions.builder().forceRefresh(true).build(), // We do not need to capture value if refresh succeeds { continuation.resume(Unit) }, // We do not need to throw if refresh fails { continuation.resume(Unit) } ) } }}これで、AWSMobileClient.getInstance() を AWSCredentialsProvider として使用する代わりに、AWSCredentialsProvider を受け入れるプラグインで AmplifyCredentialsProvider を使用できるようになりました。
AWS 設定情報の提供
Amplify v2 は amplifyconfiguration.json ファイルを使用し、AWS Android SDK は awsconfiguration.json ファイルを使用します。プロジェクトで Amplify v2 と AWS Android SDK の両方を使用している場合、リソースが同期していることを確認することが重要です。Amplify CLI はこれら両方のファイル タイプを生成および更新し続けていますが、手動のカスタマイズはどちらのファイルにも適用される必要があります。
AWS Android SDK プラグインが設定情報を必要とする場合、AWSConfiguration クラスを引き続き使用できます。
AWSConfiguration awsConfiguration = new AWSConfiguration(context);val awsConfiguration = AWSConfiguration(context)Amplify v2 を使用した AWS Android SDK プラグインの使用例
これはサポートされているプラグインの完全なリストではありません。AWSCredentialsProvider を受け入れ、AWS Mobile Client に依存しないプラグインは機能するはずです。
S3 Storage(com.amazonaws:aws-android-sdk-s3)
AWSConfiguration awsConfiguration = new AWSConfiguration(context);TransferUtility transferUtility = TransferUtility.builder() .context(context) .awsConfiguration(awsConfig) .s3Client( new AmazonS3Client( new AmplifyCredentialsProvider(), Region.getRegion(Regions.US_EAST_1) ) ) .build();val awsConfiguration = AWSConfiguration(context)val transferUtility = TransferUtility.builder() .context(context) .awsConfiguration(awsConfiguration) .s3Client( AmazonS3Client( AmplifyCredentialsProvider(), Region.getRegion(Regions.US_EAST_1) ) ) .build()IoT(com.amazonaws:aws-android-sdk-iot)
AWSIotClient client = new AWSIotClient(new AmplifyCredentialsProvider());val client = AWSIotClient(AmplifyCredentialsProvider())API Gateway によって生成された Android SDK(aws-android-sdk-apigateway-core)
ApiClientFactory clientFactory = new ApiClientFactory();clientFactory.credentialsProvider(new AmplifyCredentialsProvider());val clientFactory = ApiClientFactory()clientFactory.credentialsProvider(AmplifyCredentialsProvider())