Flutter에서 AWS Amplify 설정에 대한 단계별 가이드

AWS amplify는 AWS 제품군의 일부인 BAAS(Back-End As A Service)입니다.
Aws Amplify는 firebase와 같은 백엔드 서비스를 제공했습니다.

초보자가 AWS를 시작하는 것은 매우 혼란스럽기 때문에 가능한 한 간단하게 만들려고 합니다.

시작하기



amplfly CLI를 설치합니다.

npm install -g @aws-amplify/cli


완료되면 Amplify CLI를 설정합니다. 다음 명령을 실행하여 Amplify를 구성합니다.

amplify configure

amplify configure는 기존 계정이 있거나 sign 새 계정이 있는 경우 AWS 콘솔에 create 들어가야 합니다.
Amplify CLI에 로그인하면 AWS Identity and Access Management(IAM)에 사용자를 생성해야 합니다. 귀하의 계정에서

IAM 포털로 이동하여 '사용자 추가'를 선택합니다. 적절한 지역을 선택했는지 확인하십시오(오른쪽 상단).

일련의 단계를 통해 사용자를 생성할 수 있습니다.ACCESS KEY IDSECRET ACCESS KEY를 모두 저장해야 프로젝트를 설정할 때 유용합니다.

Enter the access key of the newly created user:
? accessKeyId:  # YOUR_ACCESS_KEY_ID
? secretAccessKey:  # YOUR_SECRET_ACCESS_KEY
This would update/create the AWS Profile in your local machine
? Profile Name:  # (default)


새 사용자를 성공적으로 설정했습니다.

프로젝트 설정



open pubspec.yaml을 설정한 후 Android 스튜디오에서 새 Flutter 프로젝트를 만들고 "SDK:flutter"줄 아래에 다음 3개의 종속성을 추가합니다.

  amplify_flutter: ^0.2.0
  amplify_auth_cognito: ^0.2.0
  amplify_analytics_pinpoint: ^0.2.0


프로젝트를 새 구성과 동기화하고 다음을 실행합니다.

flutter pub get


그런 다음 Amplify를 초기화했습니다.

amplify init


메시지가 표시되면 다음을 입력합니다.

? Enter a name for the environment
    `dev`
? Choose your default editor:
    `IntelliJ IDEA`
? Choose the type of app that you're building: 
    'flutter'
? Where do you want to store your configuration file? 
    ./lib/
? Do you want to use an AWS profile?
    `Yes`
? Please choose the profile you want to use
    `default`


이는 Amplify 지원 애플리케이션을 생성하기 위한 일회성 초기화 프로세스입니다. 이 명령은 모든 AWS 리소스를 할당하는 데 사용할 AWS 프로필을 선택하고 프로젝트에 해당하는 프레임워크 언어(Javascript, Flutter, Java, Swift)를 선택하는 데 도움이 됩니다. Amplify init는 다른 AWS 리소스가 추가될 때 중첩된 CloudFormation 템플릿을 포함할 S3 버킷에 상위 CloudFormation 템플릿을 생성합니다. 동일한 S3 버킷에는 Lambda zip 파일과 AppSync 스키마 및 해석기 파일도 포함되어 있습니다. amplify/directory는 필요한 모든 메타데이터 파일이 포함된 amplify init 명령이 성공적으로 완료되면 생성됩니다.

Amplify Flutter 라이브러리에서 메서드를 사용하기 전에 필요한 모든 플러그인을 추가하고 앱에서 구성을 한 번 호출하는 것이 중요합니다.

// Amplify Flutter Packages
import 'package:amplify_flutter/amplify.dart';
import 'package:amplify_analytics_pinpoint/amplify_analytics_pinpoint.dart';
import 'package:amplify_auth_cognito/amplify_auth_cognito.dart';

// Generated in previous step 
import 'amplifyconfiguration.dart'; 


애플리케이션의 루트 Stateful Widget에 다음 코드를 추가합니다.


class _MyHomePageState extends State<MyHomePage> {

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

  void _configureAmplify() async {

    // Add Pinpoint and Cognito Plugins, or any other plugins you want to use
    AmplifyAnalyticsPinpoint analyticsPlugin = AmplifyAnalyticsPinpoint();
    AmplifyAuthCognito authPlugin = AmplifyAuthCognito();
    await Amplify.addPlugins([authPlugin, analyticsPlugin]);

    // Once Plugins are added, configure Amplify
    // Note: Amplify can only be configured once.
    try {
      await Amplify.configure(amplifyconfig);
    } on AmplifyAlreadyConfiguredException {
      print("Tried to reconfigure Amplify; this can occur when your app restarts on Android.");
    }
  }

  // customize the rest of your Widget below as you wish...

Addplugin 메소드가 Amplify.configuration() 전에 호출되었음을 주목하십시오.

축하합니다. Aws Amplify로 첫 번째 Flutter 프로젝트 설정을 막 마쳤습니다.
앞으로 이 시리즈의 다음 부분에서는 AWS Cognito를 프로젝트에 추가하여 멋진 사용자 등록/로그인을 활성화할 것입니다.

좋은 웹페이지 즐겨찾기