Name:
interface
Value:
Amplify has re-imagined the way frontend developers build fullstack applications. Develop and deploy without the hassle.
Maintenance ModeYou are viewing Amplify Gen 1 documentation. Amplify Gen 1 has entered maintenance mode and will reach end of life on May 1, 2027. New project should use Amplify Gen 2. For existing Gen 1 projects, a migration guide and tooling are available to help you upgrade. Switch to the latest Gen 2 docs →

Amplify v2 互換性

AWS Mobile Client(com.amazonaws:aws-android-sdk-mobile-client)と Amplify Android v2 は互いに互換性がありません。Amplify v2 は AWS Mobile Client から認証情報を別の形式に移行するため、AWS Mobile Client がその認証情報を読み取ることができなくなります。この移行が実施された後に AWS Mobile Client が起動された場合、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.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.lang.RuntimeException
import kotlin.coroutines.resume
import kotlin.coroutines.resumeWithException
import kotlin.coroutines.suspendCoroutine
import 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())