java - AWSMobile 与 Amplify
问题描述
我最近从一个项目中继承了一些旧代码。它正在被重新用作 ATAK(Android 战术突击工具包)的插件,它需要使用 AWS Amplify 从 AWS S3 上传和下载文件。代码的原始创建者使用 AWS amplify 上传文件。
现在,当我尝试在 CLI 中设置 AWS amplify(遵循 AWS 移动开发指南)时,我注意到运行 amplify init、amplify add auth、amplify add storage 和 amplify add API 一切都很顺利。没关系。
接下来,我将依赖项添加到我的 build.gradle (:app) 文件中的 Gradle 文件中。这也工作正常并同步。
现在,代码实际上从未通过如下所示的 AWS.configure 部分:
Amplify.addPlugin(new AWSCognitoAuthPlugin());
Amplify.addPlugin(new AWSCognitoAuthPlugin());
Amplify.addPlugin(new AWSS3StoragePlugin());
com.atakmap.coremap.log.Log.i(TAG, "Amplify Plugin configure!");
com.atakmap.coremap.log.Log.i(TAG, "Amplify: (plugin == null) == " + Boolean.toString(plugin == null));
Amplify.configure(plugin);
com.atakmap.coremap.log.Log.i(TAG, "Amplify Plugin configure Afterwards!!");
android.util.Log.i("MyAmplifyApp", "Initialized Amplify");
} catch (AmplifyException error) {
android.util.Log.e("MyAmplifyApp", "Could not initialize Amplify", error);
}
2021-04-13 15:09:58.824 27642-27642/com.atakmap.app.civ E/MyAmplifyApp: Could not initialize Amplify
AmplifyException {message=Failed to instantiate AWSMobileClient, cause=java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Context android.content.Context.getApplicationContext()' on a null object reference, recoverySuggestion=See attached exception for more details}
at com.amplifyframework.auth.cognito.AWSCognitoAuthPlugin.configure(AWSCognitoAuthPlugin.java:87)
at com.amplifyframework.core.category.Category.configure(Category.java:13)
at com.amplifyframework.core.Amplify.configure(Amplify.java:17)
at com.amplifyframework.core.Amplify.configure(Amplify.java:1)
at com.atakmap.android.helloworld.recyclerview.RecyclerViewDropDown.<init>(RecyclerViewDropDown.java:200)
at com.atakmap.android.helloworld.HelloWorldDropDownReceiver.<init>(HelloWorldDropDownReceiver.java:653)
at com.atakmap.android.helloworld.HelloWorldMapComponent.onCreate(HelloWorldMapComponent.java:254)
at com.atakmap.android.helloworld.plugin.HelloWorldLifecycle.onCreate(HelloWorldLifecycle.java:78)
at com.atak.plugins.impl.LifecycleMapComponent$2.run(SourceFile:142)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Context android.content.Context.getApplicationContext()' on a null object reference
at com.amazonaws.mobile.auth.core.IdentityManager.<init>(IdentityManager.java:207)
at com.amazonaws.mobile.client.AWSMobileClient$2.run(AWSMobileClient.java:482)
at com.amazonaws.mobile.client.internal.InternalCallback$1.run(InternalCallback.java:101)
at java.lang.Thread.run(Thread.java:764)
我有几个问题。
AWS Mobile 似乎已被弃用。
如果导致问题的代码涉及 AWS Mobile,是否有办法绕过此错误而无需使用 AWSMobile,如果是,我应该在哪里查看?
解决方案
AWS Mobile 似乎已被弃用。
您正在使用 AWS Amplify,截至 2021 年,它是当前一代产品。
Amplify.addPlugin(new AWSCognitoAuthPlugin()); Amplify.addPlugin(new AWSCognitoAuthPlugin());
看起来您要添加此插件两次;您应该只添加一次唯一的插件。
在 CLI 中设置 AWS 放大
请注意,Amplify Storage 类别需要Amplify Auth 类别。你也跑amplify add auth
了吗?CLI 将在app/src/main/res/raw/amplifyconfiguration.json
. 检查此文件以确保它包含有关身份验证和存储的信息。
最后 -
Amplify.configure(plugin);
上面的配置方法是为了取一个 Android Context
。我不清楚传入的是什么,但它有一个奇怪的名字(“ plugin
”。)
推荐阅读
- aws-lambda - 自定义资源在部署时无法正常运行
- mysql - MySQL:如何使用 CASE 或 IF 匹配较短的列的长度?
- javascript - 加载页面时如何随机执行 JavaScript 函数?
- javascript - 需要帮助,条件 if 和变量问题,返回空
- c# - 使用 $(this).val() 返回 Invalid Postback 错误
- r - 尽管为 Shiny 中的交互式地图提供了 Long/Lat 数据点,但未找到它们
- node.js - 从 react 发送请求时出现 Node.js 错误(net::ERR_TOO_MANY_REDIRECTS)
- java - 了解关于 Nexus 等 Artifactory 的 Maven 存储库
- excel - 将数据添加到 Excel 表
- android - 没有这样的文件或目录导出 'src/material/page.dart' MaterialPageRoute 构建问题