【问题标题】:Flutter Amplify S3 setup error, amplifyconfiguration.jsonFlutter Amplify S3 设置错误,amplifyconfiguration.json
【发布时间】:2021-11-10 21:40:43
【问题描述】:

我正在启动一个使用 Amplify S3 存储的项目。我学习了许多教程和示例项目。

我下载了这个示例项目: https://github.com/aws-amplify/amplify-flutter/tree/main/example

但是在调用 Amplify.configue(amplifyconfig) 时出现以下错误:

E/flutter (5798): [ERROR:flutter/lib/ui/ui_dart_state.cc(209)] Unhandled Exception: AnalyticsException(message: Unable to read appId or region from the amplify configuration json., recoverySuggestion: 确保amplifyconfiguration.json 是预期格式的有效 json 对象。请查看有关 amplifyconfiguration.json 预期格式的文档。底层异常:org.json.JSONException:pinpointAnalytics 没有值) E/flutter (5798):#0 AmplifyClass.configure (package:amplify_flutter/amplify.dart:171:9) 电子/颤振(5798): E/flutter (5798): #1 _MyAppState._initAmplifyFlutter (package:sample_app/main.dart:65:7) 电子/颤振(5798): E/颤动​​(5798):

这是部分代码:

 @override
  initState() {
    super.initState();
    _initAmplifyFlutter();
  }

  void _initAmplifyFlutter() async {
    AmplifyAuthCognito auth = AmplifyAuthCognito();
    AmplifyStorageS3 storage = AmplifyStorageS3();
    AmplifyAnalyticsPinpoint analytics = AmplifyAnalyticsPinpoint();

    Amplify.addPlugins([auth, storage, analytics]);

    // Initialize AmplifyFlutter
    try {
      await Amplify.configure(amplifyconfig);
    } on AmplifyAlreadyConfiguredException {
      print(
          "Amplify was already configured. Looks like app restarted on android.");
    }

    setState(() {
      _isAmplifyConfigured = true;
    });
  }

另外,我的 amplifyconfiguation.dart 看起来不错;

const amplifyconfig = ''' {
    "UserAgent": "aws-amplify-cli/2.0",
    "Version": "1.0",
    "auth": {
        "plugins": {
            "awsCognitoAuthPlugin": {
                "UserAgent": "aws-amplify-cli/0.1.0",
                "Version": "0.1.0",
                "IdentityManager": {
                    "Default": {}
                },
                "CredentialsProvider": {
                    "CognitoIdentity": {
                        "Default": {
                            "PoolId": "us-east-1:b9741a22-5ce7-44e6-807d-43ac31244d3b",
                            "Region": "us-east-1"
                        }
                    }
                },
                "CognitoUserPool": {
                    "Default": {
                        "PoolId": "us-east-1_xTO13zNaS",
                        "AppClientId": "3im526imsnhcl9rugc3t8c70tn",
                        "Region": "us-east-1"
                    }
                },
                "Auth": {
                    "Default": {
                        "authenticationFlowType": "USER_SRP_AUTH",
                        "loginMechanisms": [
                            "PREFERRED_USERNAME"
                        ],
                        "signupAttributes": [
                            "EMAIL"
                        ],
                        "passwordProtectionSettings": {
                            "passwordPolicyMinLength": 8,
                            "passwordPolicyCharacters": []
                        },
                        "mfaConfiguration": "OFF",
                        "mfaTypes": [
                            "SMS"
                        ],
                        "verificationMechanisms": [
                            "EMAIL"
                        ]
                    }
                },
                "S3TransferUtility": {
                    "Default": {
                        "Bucket": "demobucket225218-dev",
                        "Region": "us-east-1"
                    }
                }
            }
        }
    },
    "storage": {
        "plugins": {
            "awsS3StoragePlugin": {
                "bucket": "demobucket225218-dev",
                "region": "us-east-1",
                "defaultAccessLevel": "guest"
            }
        }
    }
}''';

我已经跑了:

  amplify add auth
   amplify add storage
   amplify push

这是我在 pubspec.yaml 中的依赖项

  file_picker: ^4.0.0
  amplify_flutter: 0.2.7
  amplify_analytics_pinpoint: 0.2.7
  amplify_auth_cognito: 0.2.7
  amplify_storage_s3: 0.2.7

仍然没有运气。谢谢

【问题讨论】:

    标签: flutter amazon-s3 aws-amplify


    【解决方案1】:

    您从未将分析添加到您的项目中。你做到了

     amplify add auth
       amplify add storage
       amplify push
    

    但你没有添加分析。

    另外,如果您检查您的 amplifyconfiguation.dart,有“auth”和“storage” - 但没有分析。

    只需将其注释掉并尝试:

    @override
      initState() {
        super.initState();
        _initAmplifyFlutter();
      }
    
      void _initAmplifyFlutter() async {
        AmplifyAuthCognito auth = AmplifyAuthCognito();
        AmplifyStorageS3 storage = AmplifyStorageS3();
        //AmplifyAnalyticsPinpoint analytics = AmplifyAnalyticsPinpoint();
    
        Amplify.addPlugins([auth, storage/*, analytics*/]);
    
        // Initialize AmplifyFlutter
        try {
          await Amplify.configure(amplifyconfig);
        } on AmplifyAlreadyConfiguredException {
          print(
              "Amplify was already configured. Looks like app restarted on android.");
        }
    
        setState(() {
          _isAmplifyConfigured = true;
        });
      }
    

    【讨论】:

    • 非常感谢
    猜你喜欢
    • 2020-05-08
    • 2022-01-26
    • 2020-01-13
    • 1970-01-01
    • 2020-12-16
    • 2020-12-15
    • 1970-01-01
    • 2020-08-30
    • 1970-01-01
    相关资源
    最近更新 更多