You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

升级至Amplify 2.x版本后上传图片报The provided token has expired错误该如何解决?

问题

升级到Amplify 2版本(当前使用2.8.4版本)后,使用Amplify库上传照片时遇到以下错误:

SinglePartUploadWorker failed with exception: aws.sdk.kotlin.services.s3.model.S3Exception: The provided token has expired.
                                                                                                    at aws.sdk.kotlin.services.s3.transform.PutObjectOperationDeserializerKt.throwPutObjectError(PutObjectOperationDeserializer.kt:66)
                                                                                                    at aws.sdk.kotlin.services.s3.transform.PutObjectOperationDeserializerKt.access$throwPutObjectError(PutObjectOperationDeserializer.kt:1)
                                                                                                    at aws.sdk.kotlin.services.s3.transform.PutObjectOperationDeserializerKt$throwPutObjectError$1.invokeSuspend(Unknown Source:13)
                                                                                                    at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
                                                                                                    at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:106)
                                                                                                    at kotlinx.coroutines.scheduling.CoroutineScheduler.runSafely(CoroutineScheduler.kt:570)
                                                                                                    at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.executeTask(CoroutineScheduler.kt:750)
                                                                                                    at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.runWorker(CoroutineScheduler.kt:677)
                                                                                                    at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.run(CoroutineScheduler.kt:664)

Gradle配置如下:

//     Amplify core dependency
implementation 'com.amplifyframework:core:2.8.4'
implementation 'com.amplifyframework:aws-storage-s3:2.8.4'
implementation 'com.amplifyframework:aws-auth-cognito:2.8.4'

MainApplication类中的初始化代码:

Amplify.addPlugin(new AWSCognitoAuthPlugin());
Amplify.addPlugin(new AWSS3StoragePlugin());
Amplify.configure(getApplicationContext());

配置文件放置路径:

res/raw/amplifyconfiguration.json
res/raw/awsconfiguration.json
解决方案
  • 校验用户会话状态:上传操作前调用Amplify.Auth.getCurrentUser()确认用户处于登录状态,若会话过期,引导用户重新登录获取新凭证。
  • 主动刷新Auth凭证:在上传前强制获取最新会话,确保凭证有效:
    Amplify.Auth.fetchAuthSession(
        result -> {
            AWSCognitoAuthSession cognitoSession = (AWSCognitoAuthSession) result;
            if (cognitoSession.isSignedIn()) {
                // 凭证有效,执行上传逻辑
            } else {
                // 触发用户重新登录流程
            }
        },
        error -> Log.e("Auth", "获取会话失败", error)
    );
    
  • 检查配置文件中的Token有效期:打开amplifyconfiguration.json,查看Cognito用户池的tokenValidityUnits配置,确认access token和refresh token的有效期设置合理,若过短可调整。
  • 升级Amplify版本:2.8.4版本可能存在凭证刷新相关的已知问题,尝试升级至最新稳定版,修改Gradle依赖中的版本号即可。
  • 避免启动初期执行上传:确保Auth插件完成初始化后再执行上传操作,避免App启动阶段因凭证未加载完全导致的过期错误。
  • 清除缓存并重新登录:清除App缓存数据,让用户重新登录,确保新的有效凭证被正确获取和使用。

内容的提问来源于stack exchange,提问作者user3661581

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.19 17:02:06