๊ด€๋ฆฌ ๋ฉ”๋‰ด

Hey Tech

ํŒŒ์ผ ์—…๋กœ๋“œ ์›น ํŽ˜์ด์ง€ ๊ตฌ์ถ•: (1) React ๊ธฐ๋ฐ˜ Frontend ๊ฐœ๋ฐœ ๋ณธ๋ฌธ

SW ๊ฐœ๋ฐœ/Amazon Web Service

ํŒŒ์ผ ์—…๋กœ๋“œ ์›น ํŽ˜์ด์ง€ ๊ตฌ์ถ•: (1) React ๊ธฐ๋ฐ˜ Frontend ๊ฐœ๋ฐœ

Tony Park (ํ† ๋‹ˆ) 2022. 6. 8. 11:46
728x90
๋ฐ˜์‘ํ˜•

๐Ÿ“Œ ์‹œ๋ฆฌ์ฆˆ ํฌ์ŠคํŒ…

1.  ํŒŒ์ผ ์—…๋กœ๋“œ ์›น ํŽ˜์ด์ง€ ๊ตฌ์ถ•: (1) React ๊ธฐ๋ฐ˜ Frontend ๊ฐœ๋ฐœ
2.  ํŒŒ์ผ ์—…๋กœ๋“œ ์›น ํŽ˜์ด์ง€ ๊ตฌ์ถ•: (2) AWS IAM ์—ญํ•  ์ƒ์„ฑ
3.  ํŒŒ์ผ ์—…๋กœ๋“œ ์›น ํŽ˜์ด์ง€ ๊ตฌ์ถ•: (3) AWS Lambda ์ƒ์„ฑ
4.  ํŒŒ์ผ ์—…๋กœ๋“œ ์›น ํŽ˜์ด์ง€ ๊ตฌ์ถ•: (4) AWS S3 ์ƒ์„ฑ
5.  ํŒŒ์ผ ์—…๋กœ๋“œ ์›น ํŽ˜์ด์ง€ ๊ตฌ์ถ•: (5) AWS API Gateway ์ƒ์„ฑ
6.  ํŒŒ์ผ ์—…๋กœ๋“œ ์›น ํŽ˜์ด์ง€ ๊ตฌ์ถ•: (6) AWS Lambda ํ•จ์ˆ˜ ์ž‘์„ฑ
7.  ํŒŒ์ผ ์—…๋กœ๋“œ ์›น ํŽ˜์ด์ง€ ๊ตฌ์ถ•: (7) Axios ๊ธฐ๋ฐ˜ API Call
8.  ํŒŒ์ผ ์—…๋กœ๋“œ ์›น ํŽ˜์ด์ง€ ๊ตฌ์ถ•: (8) ํŒŒ์ผ ์—…๋กœ๋“œ ํ…Œ์ŠคํŠธ
9.  ํŒŒ์ผ ์—…๋กœ๋“œ ์›น ํŽ˜์ด์ง€ ๊ตฌ์ถ•: (9) AWS S3 ๊ธฐ๋ฐ˜ ์›น ํ˜ธ์ŠคํŒ…

๐Ÿ‘จ‍๐Ÿ’ป ๋“ค์–ด๊ฐ€๋ฉฐ

๋ณธ ํฌ์ŠคํŒ…์—์„œ๋Š” React๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ํŒŒ์ผ์„ ์—…๋กœ๋“œํ•˜๋Š” ์›น ํŽ˜์ด์ง€๋ฅผ(frontend) ๊ตฌ์ถ•ํ•ฉ๋‹ˆ๋‹ค. ์‹ค์ œ ํŒŒ์ผ์„ ์—…๋กœ๋“œํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ์„œ๋ฒ„๊ฐ€ ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. ๋ณธ ํ”„๋กœ์ ํŠธ์—์„œ๋Š” ์„œ๋ฒ„ ๊ตฌ์ถ•์€ ํด๋ผ์šฐ๋“œ ์„œ๋น„์Šค์ธ Amzoan Web Service(AWS)๋ฅผ ํ™œ์šฉํ•˜์—ฌ ๊ตฌ์ถ•ํ•ฉ๋‹ˆ๋‹ค.

๐Ÿ“š ๋ชฉ์ฐจ

1. ์‚ฌ์ „ ์ž‘์—…
2. ์‹คํ–‰ ๊ฒฐ๊ณผ
3. ์ „์ฒด ์ฝ”๋“œ
4. ์ฝ”๋“œ ์„ค๋ช…

1. ์‚ฌ์ „ ์ž‘์—…

node, npx, IDLE๋ฅผ ๋จผ์ € ์„ค์น˜ํ•ด ์ฃผ์„ธ์š”.

1.1. Node ์„ค์น˜

1) Window OS

์•„๋ž˜ ํฌ์ŠคํŒ…์„ ์ฐธ๊ณ ํ•ด ์ฃผ์„ธ์š”.
https://heytech.tistory.com/199

 

Node.js ์„ค์น˜ ๋ฐฉ๋ฒ• ๊ณต์œ !(+์„ค์น˜ ์—๋Ÿฌ ํ•ด๊ฒฐ ๋ฐฉ๋ฒ•)

๐Ÿ’กํ•™์Šต๋ชฉํ‘œ Node.js ์„ค์น˜ํ•˜๋Š” ๋ฐฉ๋ฒ•๊ณผ ํ™˜๊ฒฝ ๋ณ€์ˆ˜ ์„ค์ •ํ•˜๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด ํ•™์Šตํ•œ๋‹ค. Node.js ์„ค์น˜์—ฌ๋ถ€ ํ™•์ธ cmd ์ฐฝ์— ์•„๋ž˜ ๋ช…๋ น์–ด๋ฅผ ์ž…๋ ฅํ•ด ๋ด…๋‹ˆ๋‹ค. (cmd ์ฐฝ ํ‚ค๋Š” ๋ฐฉ๋ฒ•: ํ‚ค๋ณด๋“œ ๋‚ด ์œˆ๋„์šฐ ๋ฒ„ํŠผ + cmd ์ž…๋ ฅ) n

heytech.tistory.com

2) Mac OS

Mac ์‚ฌ์šฉ์ž๋ผ๋ฉด ์•„๋ž˜ ๊ณต์‹ ์›น ํŽ˜์ด์ง€์— ์ ‘์†ํ•˜์…”์„œ ๋‹ค์šด๋กœ๋“œํ•˜์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค(๊ทธ๋ฆผ 1).
https://nodejs.org/ko/download/

 

๋‹ค์šด๋กœ๋“œ | Node.js

Node.js® is a JavaScript runtime built on Chrome's V8 JavaScript engine.

nodejs.org

๊ทธ๋ฆผ 1. Node.js ์„ค์น˜ํ•˜๊ธฐ

1.2. npx

1) npx ๊ฐœ๋…

npx๋Š” ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ํŒจํ‚ค์ง€ ๊ด€๋ฆฌ ๋ชจ๋“ˆ์ธ npm(Node Package Module) 5.2.0์˜ ์ƒˆ๋กœ์šด ๋ฒ„์ „๋ถ€ํ„ฐ ์ƒˆ๋กœ ์ถ”๊ฐ€๋œ CLI(Command-line interface) ํˆด์ž…๋‹ˆ๋‹ค. npm๊ณผ ๋‹ค๋ฅธ ํŒจํ‚ค์ง€ ๊ด€๋ฆฌ ๋ชจ๋“ˆ์ด ์•„๋‹Œ npm์„ ๋”์šฑ ํŽธ๋ฆฌํ•˜๊ฒŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋„๋ก ๋•๊ธฐ ์œ„ํ•ด npm์—์„œ ์ง์ ‘ ์ œ๊ณตํ•˜๋Š” ํˆด์ž…๋‹ˆ๋‹ค.

2) npx ์„ค์น˜

ํ„ฐ๋ฏธ๋„์—์„œ ์•„๋ž˜ ๋ช…๋ น์–ด๋ฅผ ํ†ตํ•ด npx๋ฅผ ์„ค์น˜ํ•ฉ๋‹ˆ๋‹ค.

npm install npx -g

1.3. IDLE

ํ†ตํ•ฉ๊ฐœ๋ฐœํ™˜๊ฒฝ(IDLE)๋ฅผ ํ•˜๋‚˜ ์„ค์น˜ํ•ด ์ฃผ๋ฉด ํ”„๋กœ์ ํŠธ ์ž‘์—…์ด ํ›จ์”ฌ ํŽธ๋ฆฌํ•ฉ๋‹ˆ๋‹ค. ์ €๋Š” Microsoft์—์„œ ๋ฌด๋ฃŒ๋กœ ์ œ๊ณตํ•˜๋Š” VS Code๋ฅผ ์‚ฌ์šฉ ์ค‘์ž…๋‹ˆ๋‹ค. ํ•˜๋‹จ์˜ ๋งํฌ๋ฅผ ํ†ตํ•ด ๋‹ค์šด๋กœ๋“œ ๋ฐ›์œผ์‹œ๊ธธ ๋ฐ”๋ž๋‹ˆ๋‹ค.
https://code.visualstudio.com/

 

Visual Studio Code - Code Editing. Redefined

Visual Studio Code is a code editor redefined and optimized for building and debugging modern web and cloud applications.  Visual Studio Code is free and available on your favorite platform - Linux, macOS, and Windows.

code.visualstudio.com

2. ์‹คํ–‰ ๊ฒฐ๊ณผ

๋ณธ ํฌ์ŠคํŒ…์˜ ์ตœ์ข… ๊ฒฐ๊ณผ๋ฌผ์€ ๊ทธ๋ฆผ 2์™€ ๊ฐ™์œผ๋ฉฐ, ์‹คํ–‰ ์ˆœ์„œ๋Š” ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค.

1) [ํŒŒ์ผ ์„ ํƒ] ๋ฒ„ํŠผ์„ ํด๋ฆญํ•˜์—ฌ ์—…๋กœ๋“œํ•  ํŒŒ์ผ์„ ์„ ํƒ
2) ํŒŒ์ผ์— ๋Œ€ํ•œ ์ •๋ณด ์ถœ๋ ฅ(ํŒŒ์ผ ์ด๋ฆ„, ์œ ํ˜•, ์ตœ์ข… ์ˆ˜์ •์ผ)
3) [ํŒŒ์ผ ์—…๋กœ๋“œ] ๋ฒ„ํŠผ์„ ํด๋ฆญํ•˜์—ฌ ํŒŒ์ผ ์—…๋กœ๋“œ ์š”์ฒญ

๊ทธ๋ฆผ 2. ํŒŒ์ผ ์—…๋กœ๋“œ ์›น ํŽ˜์ด์ง€

3. ์ „์ฒด ์ฝ”๋“œ

์ฝ”๋“œ๋Š” Github์— ์—…๋กœ๋“œํ•ด ๋‘์—ˆ์Šต๋‹ˆ๋‹ค.
https://github.com/park-gb/file-uploader-frontend

 

GitHub - park-gb/file-uploader-frontend: ๋ฆฌ์•กํŠธ ๊ธฐ๋ฐ˜ ํŒŒ์ผ ์—…๋กœ๋“œ ์›น ํŽ˜์ด์ง€ ๊ตฌํ˜„

๋ฆฌ์•กํŠธ ๊ธฐ๋ฐ˜ ํŒŒ์ผ ์—…๋กœ๋“œ ์›น ํŽ˜์ด์ง€ ๊ตฌํ˜„. Contribute to park-gb/file-uploader-frontend development by creating an account on GitHub.

github.com

4. ์ฝ”๋“œ ์„ค๋ช…

Step by Step์œผ๋กœ ๊ตฌํ˜„ํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์†Œ๊ฐœํ•ฉ๋‹ˆ๋‹ค. ๋ณธ ํฌ์ŠคํŒ…์˜ ์ฝ”๋“œ๋Š” ์ด๊ณณ์„ ์ฐธ๊ณ ํ•˜์—ฌ ์ž‘์„ฑํ•˜์˜€์Šต๋‹ˆ๋‹ค.

4.1. IDLE ์˜คํ”ˆ

์•ž์„œ ์„ค์น˜ํ•œ IDLE(e.g., VS Code)์™€ ํ„ฐ๋ฏธ๋„์„ ์—ด์–ด์ค๋‹ˆ๋‹ค.

4.2. create react app

ํ„ฐ๋ฏธ๋„์—์„œ ํ”„๋กœ์ ํŠธ๋ฅผ ์ƒ์„ฑํ•  ํด๋”๋กœ ๋””๋ ‰ํ† ๋ฆฌ๋ฅผ ๋ณ€๊ฒฝํ•ด ์ฃผ์‹œ๊ณ  react app์„ ์„ค์น˜ํ•ฉ๋‹ˆ๋‹ค. ํ„ฐ๋ฏธ๋„ ๋‚ด ๋‹จ์ถ•ํ‚ค๋Š” ์ด๊ณณ์„ ์ฐธ๊ณ ํ•ด ์ฃผ์„ธ์š”.

npx create-react-app ํ”„๋กœ์ ํŠธ์ด๋ฆ„

์„ค์ •ํ•œ ํ”„๋กœ์ ํŠธ ์ด๋ฆ„์— ํ•ด๋‹นํ•˜๋Š” ํด๋”๊ฐ€ ์ƒ์„ฑ๋˜์—ˆ์„ ๊ฒ๋‹ˆ๋‹ค. ํ•ด๋‹น ํด๋” ์•ˆ์œผ๋กœ ๋””๋ ‰ํ† ๋ฆฌ๋ฅผ ๋ณ€๊ฒฝํ•ฉ๋‹ˆ๋‹ค.

cd ํ”„๋กœ์ ํŠธ์ด๋ฆ„

๋ฆฌ์•กํŠธ ์•ฑ์ด ์ž˜ ์„ค์น˜๋˜์—ˆ๋Š”์ง€ ์„œ๋ฒ„๋ฅผ ์‹คํ–‰์‹œ์ผœ ๋ด…๋‹ˆ๋‹ค.

npm start

์›น ๋ธŒ๋ผ์šฐ์ €๊ฐ€ ์—ด๋ฆฌ๊ณ  ์•„๋ž˜์˜ ๊ทธ๋ฆผ 3๊ณผ ๊ฐ™์€ ํ™”๋ฉด์ด ๋‚˜์™”๋‹ค๋ฉด ์ •์ƒ์ ์œผ๋กœ ์„œ๋ฒ„๊ฐ€ ์‹คํ–‰๋œ ๊ฒƒ์ž…๋‹ˆ๋‹ค.

๊ทธ๋ฆผ 3. ๋ฆฌ์•กํŠธ ์„œ๋ฒ„ ์ •์ƒ ์‹คํ–‰ ์ค‘

4.3. ๋ถˆํ•„์š”ํ•œ ํŒŒ์ผ ์ œ๊ฑฐ

ํ”„๋กœ์ ํŠธ์—์„œ ๋ถˆํ•„์š”ํ•œ ํŒŒ์ผ์€ ์ œ๊ฑฐํ•˜๊ณ  ์‹œ์ž‘ํ•ฉ๋‹ˆ๋‹ค.

1) packagelock.json
2) src/logo.svg
3) src/reportWebVitals.js
4) src/setupTests.js
5) src/App.test.js

4.4. App.js

import './App.css';
import React, {Component} from 'react';

class App extends Component{
  state = {
    selectedFile: null,
    fileUploadedSuccessfully: false
  }
  onFileChange = event => {
    this.setState({selectedFile : event.target.files[0]});
  }

  onFileUpload = () => {
    const formData = new FormData();
    formData.append(
      "demo file",
      this.state.selectedFile,
      this.state.selectedFile.name
    )
    this.setState({selectedFile: null});
    this.setState({fileUploadedSuccessfully: true});
  }

  fileData = () => {
    if (this.state.selectedFile){
      return(
      <div>
        <h2>ํŒŒ์ผ ์„ธ๋ถ€์ •๋ณด</h2>
        <p>ํŒŒ์ผ๋ช…: {this.state.selectedFile.name}</p>
        <p>ํŒŒ์ผ์œ ํ˜•: {this.state.selectedFile.type}</p>
        <p>Last Modified: {" "}
          {this.state.selectedFile.lastModifiedDate.toDateString()}
        </p>
      </div>
      )
    } else if (this.state.fileUploadedSuccessfully){
      return(
      <div>
        <br />
        <h4>ํŒŒ์ผ ์ •์ƒ ์—…๋กœ๋“œ!</h4> 
      </div>
      )
    } else{
      return(
      <div>
        <br/>
        <h4> ํŒŒ์ผ์„ ์„ ํƒํ•˜๊ณ  ์—…๋กœ๋“œ ๋ฒ„ํŠผ์„ ํด๋ฆญํ•ด ์ฃผ์„ธ์š”.</h4>
      </div>
      )
    }
  }

  render(){
    return (
      <div className='container'>
        <h2>ํŒŒ์ผ ์—…๋กœ๋“œ ์›น ํŽ˜์ด์ง€</h2>
        <a href = "https://heytech.tistory.com" target="blank">Hey Tech ํ‹ฐ์Šคํ† ๋ฆฌ ๋ธ”๋กœ๊ทธ</a>
        <div>
          <br></br>
          <input type = "file" onChange = {this.onFileChange} />
          <button onClick={this.onFileUpload}>
            ํŒŒ์ผ ์—…๋กœ๋“œ
          </button>
        </div>
        {this.fileData()}
      </div>
    )
  }
}

export default App;

1) state

2๊ฐ€์ง€ state ๊ฐ’์˜ ๋ณ€ํ™”์— ๋”ฐ๋ผ ์—ฌ๋Ÿฌ ๋กœ์ง์„ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค.

  • selectedFile: ํŒŒ์ผ ์„ ํƒ ์—ฌ๋ถ€ ํ™•์ธ์šฉ
  • fileUploadedSuccessfully: ํŒŒ์ผ ์—…๋กœ๋“œ ์—ฌ๋ถ€ ํ™•์ธ์šฉ

2) onFileChange ํ•จ์ˆ˜

๋ Œ๋”๋ง์—์„œ input ํƒœ๊ทธ์—์„œ file ์œ ํ˜•์œผ๋กœ ํŒŒ์ผ์„ ์—…๋กœ๋“œ ํ•˜๊ธฐ ๋•Œ๋ฌธ์— event.target.files๋กœ ํŒŒ์ผ์ด ์ „๋‹ฌ๋ฉ๋‹ˆ๋‹ค. ์ด ๊ฐ’์œผ๋กœ selectedFile์˜ state ๊ฐ’์„ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค.

3) onFileUpload ํ•จ์ˆ˜

FormData ๊ฐ์ฒด๋ฅผ ์ƒˆ๋กญ๊ฒŒ ๋งŒ๋“ค๊ณ  ์„ ํƒํ–ˆ๋˜ ํŒŒ์ผ๊ณผ ํŒŒ์ผ๋ช…์„ ์ž…๋ ฅํ•ฉ๋‹ˆ๋‹ค.

4) fileData ํ•จ์ˆ˜

(1) ํŒŒ์ผ ๋ฏธ์„ ํƒ ์‹œ, ํŒŒ์ผ ์—…๋กœ๋“œ ๋ฐฉ๋ฒ• ์ถœ๋ ฅ
(2) ํŒŒ์ผ์ด ์„ ํƒ๋œ ๊ฒฝ์šฐ, ํŒŒ์ผ์— ๋Œ€ํ•œ ์„ธ๋ถ€์ •๋ณด ์ถœ๋ ฅ
(3) ํŒŒ์ผ ์—…๋กœ๋“œ ๋ฒ„ํŠผ ํด๋ฆญ ์‹œ, ํŒŒ์ผ ์—…๋กœ๋“œ ์„ฑ๊ณต ๊ฒฐ๊ณผ ์ถœ๋ ฅ

4.5. App.css

css๋ฅผ ํ†ตํ•ด App.js์—์„œ ๋ Œ๋”๋ง๋˜๋Š” ์ „ ์˜์—ญ์„ ๊ฐ€์šด๋ฐ ์ •๋ ฌํ•ด ์ค๋‹ˆ๋‹ค.

.container{
  text-align: center;
}

๋‹ค์Œ ํฌ์ŠคํŒ…๋ถ€ํ„ฐ๋Š” AWS์˜ ์—ฌ๋Ÿฌ ์„œ๋น„์Šค๋ฅผ ์„ธํŒ…ํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์ž์„ธํžˆ ๋‹ค๋ฃน๋‹ˆ๋‹ค.

๐Ÿ“Œ ์‹œ๋ฆฌ์ฆˆ ํฌ์ŠคํŒ…

1.  ํŒŒ์ผ ์—…๋กœ๋“œ ์›น ํŽ˜์ด์ง€ ๊ตฌ์ถ•: (1) React ๊ธฐ๋ฐ˜ Frontend ๊ฐœ๋ฐœ
2.  ํŒŒ์ผ ์—…๋กœ๋“œ ์›น ํŽ˜์ด์ง€ ๊ตฌ์ถ•: (2) AWS IAM ์—ญํ•  ์ƒ์„ฑ
3.  ํŒŒ์ผ ์—…๋กœ๋“œ ์›น ํŽ˜์ด์ง€ ๊ตฌ์ถ•: (3) AWS Lambda ์ƒ์„ฑ
4.  ํŒŒ์ผ ์—…๋กœ๋“œ ์›น ํŽ˜์ด์ง€ ๊ตฌ์ถ•: (4) AWS S3 ์ƒ์„ฑ
5.  ํŒŒ์ผ ์—…๋กœ๋“œ ์›น ํŽ˜์ด์ง€ ๊ตฌ์ถ•: (5) AWS API Gateway ์ƒ์„ฑ
6.  ํŒŒ์ผ ์—…๋กœ๋“œ ์›น ํŽ˜์ด์ง€ ๊ตฌ์ถ•: (6) AWS Lambda ํ•จ์ˆ˜ ์ž‘์„ฑ
7.  ํŒŒ์ผ ์—…๋กœ๋“œ ์›น ํŽ˜์ด์ง€ ๊ตฌ์ถ•: (7) Axios ๊ธฐ๋ฐ˜ API Call
8.  ํŒŒ์ผ ์—…๋กœ๋“œ ์›น ํŽ˜์ด์ง€ ๊ตฌ์ถ•: (8) ํŒŒ์ผ ์—…๋กœ๋“œ ํ…Œ์ŠคํŠธ
9.  ํŒŒ์ผ ์—…๋กœ๋“œ ์›น ํŽ˜์ด์ง€ ๊ตฌ์ถ•: (9) AWS S3 ๊ธฐ๋ฐ˜ ์›น ํ˜ธ์ŠคํŒ…

ํฌ์ŠคํŒ… ๋‚ด์šฉ์— ์˜ค๋ฅ˜๊ฐ€ ์žˆ๊ฑฐ๋‚˜ ๋ณด์™„ํ•  ์‚ฌํ•ญ์ด ์žˆ๋‹ค๋ฉด ์•„๋ž˜์— ๋Œ“๊ธ€ ๋‚จ๊ฒจ์ฃผ์„ธ์š” :)
๊ทธ๋Ÿผ ์˜ค๋Š˜๋„ ์ฆ๊ฒ๊ณ  ๊ฑด๊ฐ•ํ•œ ํ•˜๋ฃจ ๋ณด๋‚ด์‹œ๊ธธ ๋ฐ”๋ž๋‹ˆ๋‹ค๐Ÿ˜Š
๊ณ ๋ง™์Šต๋‹ˆ๋‹ค :D

728x90
๋ฐ˜์‘ํ˜•