Ann's log

[AWS] Heroku๋กœ ๋ฐฐํฌํ•œ Node.js ์„œ๋ฒ„์—์„œ AWS S3 ์‚ฌ์šฉํ•˜๊ธฐ-1 ๋ณธ๋ฌธ

AWS

[AWS] Heroku๋กœ ๋ฐฐํฌํ•œ Node.js ์„œ๋ฒ„์—์„œ AWS S3 ์‚ฌ์šฉํ•˜๊ธฐ-1

-Ann- 2022. 5. 19. 22:16

 

Amazon S3 lcon

 

๐Ÿ™€  ๋ฌธ์ œ

์ด์ „์— ๋ฐฐํฌํ–ˆ๋˜ ํ”„๋กœ์ ํŠธ์—์„œ ์ƒˆ๋กœ์šด ์‚ฌ์ง„์„ ์—…๋กœ๋“œํ•˜๋ฉด ์‚ฌ์ง„ ํŒŒ์ผ์ด ๊นจ์ ธ์„œ ๋ณด์ธ๋‹ค๋Š” ๋ฌธ์ œ๋ฅผ ๋ฐœ๊ฒฌํ•˜์˜€๋‹ค.

ํ”„๋กœ์ ํŠธ์—์„œ Node.js ์„œ๋ฒ„๋ฅผ ๋ฐฐํฌํ• ๋•Œ Heroku๋ฅผ ์‚ฌ์šฉํ•˜์˜€๊ธฐ ๋•Œ๋ฌธ์— Heroku ๋ฌธ์„œ์—์„œ ๊ด€๋ จ ๋‚ด์šฉ์„ ์ฐพ์•„ ๋ณด์•˜๋‹ค.

๋ฌธ์„œ์— ๋”ฐ๋ฅด๋ฉด Heroku application์€ dyno๋ผ๊ณ  ํ•˜๋Š” ๊ฐ€๋ฒผ์šด Linux container๋กœ ๋™์ž‘ํ•˜๊ฒŒ ๋˜๋Š”๋ฐ, dyno๋Š” ์ˆ˜๋ช…์ด ์งง์€ ํŒŒ์ผ ์‹œ์Šคํ…œ์„ ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค๊ณ  ํ•œ๋‹ค.

๋”ฐ๋ผ์„œ dyno๊ฐ€ ๊ต์ฒด๋˜๊ฑฐ๋‚˜ ์žฌ์‹œ์ž‘๋˜๋ฉด ํŒŒ์ผ์€ ์‚ฌ๋ผ์ง€๊ฒŒ ๋œ๋‹ค. ๐Ÿ˜จ

 

๐Ÿ˜บ  ํ•ด๊ฒฐ ๋ฐฉ๋ฒ•

Heroku ๋ฌธ์„œ์—์„œ๋Š” ํ•ด๊ฒฐ ๋ฐฉ๋ฒ•์œผ๋กœ AWS S3๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด ๋‚˜์™€์žˆ๊ธธ๋ž˜ Amazon S3์— ๋Œ€ํ•ด ์•Œ์•„๋ณด๊ณ  ์‚ฌ์šฉํ•˜๊ณ ์ž ํ•œ๋‹ค.

 

 

์ด ๊ธ€์—์„œ ์ •๋ฆฌํ•˜๋Š” ๋‚ด์šฉ:

  • Amazon S3(Amazon Simple Storage Service)๋ž€?
  • S3 bucket ๋งŒ๋“ค๊ธฐ
  • Bucket ์ •์ฑ… ์„ค์ •ํ•˜๊ธฐ
  • S3 bucket CORS setting

 

 

Amazon S3(Amazon Simple Storage Service)๋ž€?

AWS ๋ฌธ์„œ์— ๋”ฐ๋ฅด๋ฉด Amazon S3๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ๋ฒ„ํ‚ท(bucket) ๋‚ด์˜ ๊ฐ์ฒด๋กœ ์ €์žฅํ•˜๋Š” ๊ฐ์ฒด ์Šคํ† ๋ฆฌ์ง€ ์„œ๋น„์Šค๋ผ๊ณ  ํ•œ๋‹ค.

์—ฌ๊ธฐ์„œ ๊ฐ์ฒด๋Š” ํ•ด๋‹น ํŒŒ์ผ์„ ์„ค๋ช…ํ•˜๋Š” ๋ชจ๋“  ๋ฉ”ํƒ€๋ฐ์ดํ„ฐ์ด๊ณ , ๋ฒ„ํ‚ท์€ ๊ฐ์ฒด์— ๋Œ€ํ•œ ์ปจํ…Œ์ด๋„ˆ์ด๋‹ค.

๋”ฐ๋ผ์„œ Amazon S3์— ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅํ•˜๋ ค๋ฉด ๋จผ์ € ๋ฒ„ํ‚ท์„ ์ƒ์„ฑํ•ด์•ผ ํ•œ๋‹ค.

๊ณ„์ •์—์„œ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ๋Š” ๋ฒ„ํ‚ท์˜ ์ˆ˜๋Š” ์ตœ๋Œ€ 100๊ฐœ์ด์ง€๋งŒ, ๋ฒ„ํ‚ท์— ์ €์žฅํ•  ์ˆ˜ ์žˆ๋Š” ๊ฐ์ฒด ์ˆ˜์—๋Š” ์ œํ•œ์ด ์—†๋‹ค๊ณ  ํ•œ๋‹ค.

 

S3 bucket ๋งŒ๋“ค๊ธฐ

๋งŒ๋“œ๋Š” ๋ฐฉ๋ฒ•์€ ์ด ๋ธ”๋กœ๊ทธ๋ฅผ ์ฐธ๊ณ ํ•˜์˜€๋‹ค.

1. AWS ์ฝ˜์†”์— IAM ์‚ฌ์šฉ์ž๋กœ ๋กœ๊ทธ์ธ ํ•˜๊ณ , s3๋ฅผ ๊ฒ€์ƒ‰ํ•˜์—ฌ ๋‹ค์Œ์„ ํด๋ฆญํ•œ๋‹ค.

 

2. create bucket ๋ฒ„ํŠผ์„ ํด๋ฆญํ•œ๋‹ค.

 

3. bucket name๊ณผ ์ง€์—ญ ์ •๋ณด๋ฅผ ์„ค์ •ํ•œ๋‹ค.

(Heroku ๋ฌธ์„œ์— ๋”ฐ๋ฅด๋ฉด ์ƒ์„ฑํ•œ heroku application๊ณผ ๊ฐ™์€ ์ง€์—ญ์œผ๋กœ S3 bucket ์ง€์—ญ์„ ์„ค์ •ํ•ด์•ผ AWS ์š”๊ธˆ์ด ๋” ์ ๊ฒŒ ๋‚˜์˜จ๋‹ค๊ณ  ํ•œ๋‹ค.)

 

4. Block all public access ์ฒดํฌ๋ฐ•์Šค๋Š” ์ฒดํฌ๋ฅผ ํ•˜์ง€ ์•Š๋Š”๋‹ค.

์ด๋ ‡๊ฒŒ ํ•ด์•ผ ์™ธ๋ถ€์—์„œ๋„ ๋‚ด ํŒŒ์ผ์„ ์ฝ์„ ์ˆ˜ ์žˆ๊ณ , ์ƒˆ๋กœ์šด ํŒŒ์ผ๋„ ์—…๋กœ๋“œํ•  ์ˆ˜ ์žˆ๋‹ค๊ณ  ํ•œ๋‹ค.

๋ณด์•ˆ ์ทจ์•ฝ์„ฑ์€ ์ข€ ๋†’์•„์ง€๊ธฐ ๋•Œ๋ฌธ์— ์กฐ์‹ฌํ•ด์•ผ ํ•œ๋‹ค๊ณ  ํ•˜๋Š”๋ฐ, ์ด ๋ถ€๋ถ„์„ ๋ณด์™„ํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ์–ด๋–ป๊ฒŒ ํ•ด์•ผํ•˜๋Š”์ง€ ์กฐ๊ธˆ ๋” ๊ณต๋ถ€๋ฅผ ํ•ด๋ด์•ผ ํ•  ๊ฒƒ ๊ฐ™๋‹ค.

 

5. ๊ทธ ์•„๋ž˜์˜ ๊ฒฝ๊ณ  ์ฒดํฌ๋ฐ•์Šค์—๋Š” ์ฒดํฌ๋ฅผ ํ•ด์ค€๋‹ค.

 

6. ๋‚˜๋จธ์ง€ ์„ค์ •์€ ๋ชจ๋‘ ๊ธฐ๋ณธ๊ฐ’์œผ๋กœ ๋†”๋‘๊ณ , bucket ์ƒ์„ฑํ•˜๊ธฐ ๋ฒ„ํŠผ์„ ํด๋ฆญํ•œ๋‹ค.

 

Bucket ์ •์ฑ… ์„ค์ •ํ•˜๊ธฐ

์™ธ๋ถ€์—์„œ bucket์— ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•˜๋ ค๋ฉด bucket ์ •์ฑ… ์„ค์ •๊นŒ์ง€ ํ•ด์ค˜์•ผ ํ•œ๋‹ค.

1. ์ƒ์„ฑํ•œ ๋ฒ„ํ‚ท ์ด๋ฆ„์„ ํด๋ฆญํ•œ๋‹ค.

 

2. Permissions ํƒญ์„ ํด๋ฆญํ•œ๋‹ค.

 

3. ํ•˜๋‹จ์œผ๋กœ ์Šคํฌ๋กค ํ•˜๋‹ค๊ฐ€ bucket ์ •์ฑ… ๋ถ€๋ถ„์—์„œ ์ˆ˜์ •ํ•˜๊ธฐ ๋ฒ„ํŠผ์„ ํด๋ฆญํ•œ๋‹ค.

 

4. Bucket ์ •์ฑ…์„ ์„ค์ •ํ•˜๊ธฐ ์œ„ํ•ด policy generator๋ฅผ ํด๋ฆญํ•œ๋‹ค.

Policy generator๋ฅผ ์ด์šฉํ•˜๋ฉด ๋‚ด๊ฐ€ ์›ํ•˜๋Š” ์กฐ๊ฑด์„ ์„ ํƒํ•˜๊ธฐ๋งŒ ํ•˜๋ฉด ์ž๋™์œผ๋กœ JSON ํ˜•์‹์˜ bucket ์ •์ฑ…์„ ๋งŒ๋“ค์–ด์ค€๋‹ค.

 

5. ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ •์ฑ…์„ ์„ค์ •ํ•˜๊ณ  Add Statement ๋ฒ„ํŠผ์„ ํด๋ฆญํ•œ๋‹ค.

Principal์—๋Š” ๋ฒ„ํ‚ท ์ •์ฑ…์ด ์ ์šฉ๋  ๋Œ€์ƒ์„ ์ „์ฒด๋กœ ์„ค์ •ํ•˜๊ธฐ ์œ„ํ•ด *๋ฅผ ์ž…๋ ฅํ•œ๋‹ค.

Actions์—์„œ๋Š” GetObject์™€ PutObject ๋‘๊ฐ€์ง€๋ฅผ ์„ ํƒํ•œ๋‹ค.

ARN์—๋Š” arn:aws:s3:::๋‚ด ๋ฒ„ํ‚ท ์ด๋ฆ„/*์˜ ํ˜•์‹์œผ๋กœ ์ ์–ด์ค€๋‹ค.

 

 

6. ๊ทธ๋Ÿฌ๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์ด statement๊ฐ€ ์ถ”๊ฐ€๋œ ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ๊ณ , Generate Policy ๋ฒ„ํŠผ์„ ํด๋ฆญํ•˜์—ฌ ์ •์ฑ…์„ ์ƒ์„ฑํ•˜๋ฉด ๋œ๋‹ค.

 

7. ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ƒ์„ฑ๋œ JSON ํ˜•์‹์˜ ๋ฒ„ํ‚ท ์ •์ฑ…์„ ๋ณต์‚ฌํ•˜๊ณ , ๋ฒ„ํ‚ท ์ •์ฑ… ์ˆ˜์ •ํ•˜๊ธฐ ํŽ˜์ด์ง€๋กœ ๋Œ์•„๊ฐ€์„œ ์›๋ž˜ ์žˆ๋˜ ๋‚ด์šฉ์€ ์ง€์šฐ๊ณ  ๋ณต์‚ฌํ•œ ๋‚ด์šฉ์„ ๋ถ™์—ฌ๋„ฃ๊ธฐ ํ•œ๋‹ค.

๊ทธ๋ฆฌ๊ณ  ๋ณ€๊ฒฝ์‚ฌํ•ญ ์ €์žฅํ•˜๊ธฐ ๋ฒ„ํŠผ์„ ํด๋ฆญํ•˜๋ฉด ๋œ๋‹ค.

 

 

์ด๋ ‡๊ฒŒ ํ•˜๋ฉด bucket ์ƒ์„ฑ๊ณผ ์„ค์ •์€ ๋๋‚ฌ์ง€๋งŒ, Heroku application์—์„œ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” Heroku ๋ฌธ์„œ์— ๋‚˜์™€์žˆ๋Š” CORS setting ๊ณผ์ •๋„ ์™„๋ฃŒํ•ด์•ผ ํ•œ๋‹ค.

S3 bucket CORS setting

1. Permissions ํƒญ ํด๋ฆญํ›„, ํ•˜๋‹จ์œผ๋กœ ์Šคํฌ๋กค์„ ํ•˜๋ฉด CORS ๋ถ€๋ถ„์ด ๋‚˜์˜ค๋Š”๋ฐ ์—ฌ๊ธฐ์„œ Edit ๋ฒ„ํŠผ์„ ํด๋ฆญํ•œ๋‹ค.

 

2. ๋‹ค์Œ ๋‚ด์šฉ์„ ์ž…๋ ฅํ•˜๊ณ  ๋ณ€๊ฒฝ์‚ฌํ•ญ ์ €์žฅํ•˜๊ธฐ ๋ฒ„ํŠผ์„ ํด๋ฆญํ•œ๋‹ค.

[
    {
        "AllowedHeaders": [
            "*"
        ],
        "AllowedMethods": [
            "GET",
            "HEAD",
            "POST",
            "PUT"
         ],
         "AllowedOrigins":[
             "*"
         ],
         "ExposeHeaders": []
    }
]

 

๊ทธ๋Ÿฌ๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์ด CORS setting์ด ์™„๋ฃŒ๋˜์—ˆ๋‹ค.

 

This tells S3 to allow any domain access to the bucket and that requests can contain any headers, which is generally fine for testing. When deploying, you should change the ‘AllowedOrigin’ to only accept requests from your domain.

 

์ฃผ์˜ํ•  ์ ์€ ์ด๋ ‡๊ฒŒ ์„ค์ •ํ•ด๋‘๋ฉด ๋ชจ๋“  ๋„๋ฉ”์ธ์˜ ์ ‘๊ทผ์„ ํ—ˆ์šฉํ•˜๋Š” ๊ฒƒ์ด๊ธฐ ๋•Œ๋ฌธ์— ํ…Œ์ŠคํŠธ ํ• ๋•Œ๋Š” ๊ดœ์ฐฎ์ง€๋งŒ, ๋ฐฐํฌํ• ๋•Œ๋Š” "AllowedOrigin" ๋ถ€๋ถ„์„ ์ˆ˜์ •ํ•ด์•ผ ํ•œ๋‹ค!

 

 

๋ชจ๋“  ๊ณผ์ •์„ ์™„๋ฃŒํ•˜๊ณ  ์‚ฌ์ง„ ํŒŒ์ผ ์—…๋กœ๋“œ ํ›„, ๊ฐ์ฒด URL๋กœ ๋“ค์–ด๊ฐ€๋ณด๋ฉด ์—๋Ÿฌ์—†์ด ๋‚ด๊ฐ€ ์˜ฌ๋ฆฐ ์‚ฌ์ง„์„ ๋ณผ ์ˆ˜ ์žˆ๊ฒŒ ๋œ๋‹ค.

์ด์ œ ์ด๊ฒƒ์„ ์–ด๋–ป๊ฒŒ Node.js ์„œ๋ฒ„์—์„œ ์‚ฌ์šฉํ•˜๋Š”์ง€ ๋‹ค์Œ ๊ธ€์—์„œ ์ •๋ฆฌํ•ด ๋ณด๊ฒ ๋‹ค.

https://enne.tistory.com/13

 

[AWS] Heroku๋กœ ๋ฐฐํฌํ•œ Node.js ์„œ๋ฒ„์—์„œ AWS S3 ์‚ฌ์šฉํ•˜๊ธฐ-2

์ด ๊ธ€์—์„œ ์ •๋ฆฌํ•˜๋Š” ๋‚ด์šฉ: AWS-SDK ์‚ฌ์šฉํ•˜๊ธฐ AWS Access Key ๋ฐœ๊ธ‰ํ•˜๊ธฐ Multer-S3 ์‚ฌ์šฉํ•˜๊ธฐ AWS-SDK ์‚ฌ์šฉํ•˜๊ธฐ SDK(Software Development Kit)๋ž€ ํŠน์ •ํ•œ ์†Œํ”„ํŠธ์›จ์–ด๋‚˜ ํ”Œ๋žซํผ์„ ์ด์šฉํ•ด์„œ ์†Œํ”„ํŠธ์›จ์–ด๋ฅผ ๊ฐœ๋ฐœํ•  ๋•Œ..

enne.tistory.com

 


์ฐธ๊ณ :

https://velog.io/@gbskang/Heroku%EC%97%90%EC%84%9C-AWS-S3-%EC%82%AC%EC%9A%A9%ED%95%98%EA%B8%B0

https://42place.innovationacademy.kr/archives/9784

 

Comments