[typescript] AWS SDK를 사용하여 타입스크립트로 Amplify를 통한 앱 개발하기

AWS Amplify는 클라우드 앱을 구축하고 배포하기 위한 도구 모음입니다. TypeScript와 AWS SDK를 활용하여 Amplify를 사용하면 강력하고 안정적인 앱을 개발할 수 있습니다.

AWS SDK 및 Amplify 설정

우선, AWS SDK 및 Amplify를 설치하고 구성해야 합니다. 다음 명령을 사용하여 필요한 패키지를 설치할 수 있습니다.

npm install aws-sdk @aws-amplify/core @aws-amplify/auth @aws-amplify/storage --save

Amplify 설정

AWS 객체에 연결하려면 Amplify를 구성해야 합니다. aws-exports.js 파일을 사용하여 설정을 수행합니다. 아래와 같은 형식으로 파일을 생성하고 AWS 자격 증명과 관련된 정보를 입력해야 합니다.

const awsmobile = {
  "aws_project_region": "your-aws-project-region",
  "aws_cognito_identity_pool_id": "your-cognito-identity-pool-id",
  "aws_cognito_region": "your-cognito-region",
  "aws_user_pools_id": "your-user-pools-id",
  "aws_user_pools_web_client_id": "your-user-pools-web-client-id",
  "aws_appsync_graphqlEndpoint": "your-appsync-endpoint",
  "aws_appsync_region": "your-appsync-region",
  "aws_appsync_authenticationType": "API_KEY"
};

export default awsmobile;

AWS 서비스 사용 예제

S3 스토리지에 파일 업로드

Amplify를 사용하여 TypeScript로 S3 스토리지에 파일을 업로드하는 예제는 다음과 같습니다.

import Amplify, { Storage } from 'aws-amplify';
import awsconfig from './aws-exports';

Amplify.configure(awsconfig);

Storage.put('example.txt', 'Hello')
  .then(result => console.log('File uploaded', result))
  .catch(err => console.log('Error uploading file', err));

인증 및 사용자 관리

아래의 예제는 사용자가 로그인하고 로그아웃하는 방법을 나타냅니다.

import Amplify, { Auth } from 'aws-amplify';
import awsconfig from './aws-exports';

Amplify.configure(awsconfig);

Auth.signIn(username, password)
  .then(user => console.log('User signed in', user))
  .catch(err => console.log('Error signing in', err));

Auth.signOut()
  .then(data => console.log('User signed out', data))
  .catch(err => console.log('Error signing out', err));

마치며

이제 AWS SDK 및 Amplify를 사용하여 TypeScript로 안정적이고 확장 가능한 앱을 구축할 수 있게 되었습니다. 이러한 방법을 사용하여 AWS 클라우드 서비스를 활용하여 다양한 유형의 앱을 구축할 수 있습니다.

참고 자료