개발/AOS

안드로이드 AWS Amplify + S3에 파일 업로드 Android uploads files with AWS Amplify + S3 (1/3) 환경 세팅

728x90

회사에서 안드로이드 클라이언트에서 S3에 파일을 업로드 기능을 구현했는데

 

Amplify 공식 문서가 매우 빈약하여 포스팅을 남겨본다.

 

크게 구현 과정은 아래와 같다.

 

1. 안드로이드 프로젝트에 Amplify SDK 세팅

2. S3 버킷 생성.

3. S3 버킷의 권한 설정.

4. S3 버킷과 안드로이드 프로젝트 연동.

 

(공식문서에서 위 과정을 매끄럽게 설명하지 않고 있다;;)

 

아래와 같은 개발 환경이 필요하다.

 

 

터미널에서 npm을 통해 amplify cli을 설치한다. (cli는 한 프로젝트에 국한되는것이 아니기 때문에 전역으로 설치한다.)

npm install -g @aws-amplify/cli

 

설치 후 amplify를 세팅하기 위해 프로젝트의 루트 경로 에서 아래의 명령어를 입력한다.

amplify configure

 

 

amplify를 사용하려면 IAM(Identitiy and Access Management) 등록이 필요하다

IAM을 모른다면 여기로.

 

 

 

 

Specify the AWS Region
? region:  # Your preferred region
Specify the username of the new IAM user:
? user name:  # User name for Amplify IAM user
Complete the user creation using the AWS console

 

IAM 유저를 생성하고 나서

아래와 같이 IAM 유저의 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)

Successfully set up the new user.

 

 

 

그 후 안드로이드 프로젝트를 생성한다.

 

 

그리고 나서 app레벨 그레들에 아래의 옵션을 추가하고 Sync Now를 클릭한다.

android {
    compileOptions {
        // Support for Java 8 features
        coreLibraryDesugaringEnabled true
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
}

dependencies {
    // Amplify core dependency
    implementation 'com.amplifyframework:core:1.16.11'

    // Support for Java 8 features
    coreLibraryDesugaring 'com.android.tools:desugar_jdk_libs:1.1.1'
}

 

그리고 프로젝트 루트 디렉토리에서 터미널을 통해 아래와 같이 amplify를 init한다.

amplify init

? Enter a name for the environment
    `dev`
? Choose your default editor:
    `IntelliJ IDEA`
? Do you want to use an AWS profile?
    `Yes`
? Please choose the profile you want to use
    `default`

 

그리고 아래의 Application을 상속받는 클래스를 생성한다. (클래스 이름은 자유.)

 

class MyAmplifyApp: Application() {

    override fun onCreate() {
        super.onCreate()

        try {
            Amplify.configure(applicationContext)
            Log.i("MyAmplifyApp", "Initialized Amplify")
        } catch (error: AmplifyException) {
            Log.e("MyAmplifyApp", "Could not initialize Amplify", error)
        }
    }



}

 

그리고 AndroidManifest.xml에서 application에 android:name=".{위에서 생성한 클래스 이름}" 옵션을 추가한다.

<application
        android:name=".MyAmplifyApp"
        
        ...
        />

 

로그캣에 아래와 같이 찍히면 SDK 세팅 성공.

com.example.MyAmplifyApp I/MyAmplifyApp: Initialized Amplify