๐Ÿ‘พ

002 AWS :: S3

ย 
S3 ์„œ๋น„์Šค๋ฅผ ์—ฐ๋™ ์‹œ์ผœ ๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.
๋งŒ์•ฝ ์„œ๋น„์Šค์—์„œ ์ƒˆ๋กœ์šด ๊ธ€์„ ์ž‘์„ฑํ•˜๊ฒŒ ๋˜๋ฉด Django๊ฐ€ ์„ค์น˜๋œ ์œ„์น˜์— ๋ฏธ๋””์–ด ํด๋”๋ฅผ ๋งŒ๋“ค์–ด์„œ ์ด๋ฏธ์ง€๋ฅผ ์—…๋กœ๋“œํ•˜๋Š” ๊ณผ์ •์„ ๊ฑฐ์น˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ S3 ์„œ๋น„์Šค๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค๋ฉด AWS์—์„œ ์ œ๊ณตํ•˜๋Š” ํด๋ผ์šฐ๋“œ ๊ณต๊ฐ„์— ์ด๋ฏธ์ง€๋ฅผ ์—…๋กœ๋“œํ•˜๊ณ  ๋‹ค์šด๋กœ๋“œ ๋ฐ›์„ ์ˆ˜ ์žˆ์–ด ๋” ํšจ์œจ์ ์ธ ์„œ๋น„์Šค๋ฅผ ์ œ์ž‘ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
ย 
Django ์„ค์น˜๊นŒ์ง€๋Š” ์•ž์˜ RDS์™€ ํ”„๋กœ์ ํŠธ๋ช…์„ ์ œ์™ธํ•˜๊ณ  ๋ชจ๋‘ ๋™์ผํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์ƒ๋žตํ•˜๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ์„œ๋ฒ„๋ฅผ ์—ด๊ณ  AWS ์›น ํŽ˜์ด์ง€์— ๋“ค์–ด๊ฐ€์„œ ๋กœ๊ทธ์ธํ•˜์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค.
notion imagenotion image
ย 
์„œ๋น„์Šค ๋ชฉ๋ก์„ ๋‚ด๋ฆฌ์‹œ๋‹ค ๋ณด๋ฉด S3๋ฅผ ๋ฐœ๊ฒฌํ•˜์‹ค ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
notion imagenotion image
ย 
notion imagenotion image
ย 
์ƒˆ๋กœ์šด ๋ฒ„ํ‚ท์„ ๋งŒ๋“ค๊ณ  ๋ชจ๋“  ํผ๋ธ”๋ฆญ ์—‘์„ธ์Šค ์ฐจ๋‹จ์„ ํ•ด์ œํ•ด์ฃผ๊ณ  ๋ฒ„ํ‚ท์„ ๋งŒ๋“ค์–ด์ค๋‹ˆ๋‹ค.
notion imagenotion image
notion imagenotion image
notion imagenotion image
ย 
๋ฒ„ํ‚ท์ด ๋งŒ๋“ค์–ด์กŒ๋‹ค๋ฉด IAM ์„ค์ •์„ ํ•ด์ฃผ์…”์•ผ ํ•ฉ๋‹ˆ๋‹ค.
notion imagenotion image
ย 
IAM์—์„œ ์‚ฌ์šฉ์ž๋ฅผ ์ถ”๊ฐ€ํ•ด์ค๋‹ˆ๋‹ค.
notion imagenotion image
ย 
ํ”„๋กœ๊ทธ๋ž˜๋ฐ ๋ฐฉ์‹ ์—‘์„ธ์Šค๋ฅผ ์ฒดํฌํ•ด์ค๋‹ˆ๋‹ค.
notion imagenotion image
notion imagenotion image
ย 
์‚ฌ์šฉ์ž๋ช…๊ณผ ๋™์ผํ•˜๊ฒŒ ๊ทธ๋ฃน์„ ๋งŒ๋“ค๊ณ  ํ•„ํ„ฐ์—์„œ S3๋ฅผ ๊ฒ€์ƒ‰ํ•ด์„œ ๋‘๋ฒˆ์งธ์— ์ฒดํฌํ•ด์ฃผ์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค.
notion imagenotion image
ย 
๋‚˜๋จธ์ง€๋Š” ๋„˜์–ด๊ฐ€์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค. ์‚ฌ์šฉ์ž ์ถ”๊ฐ€๊ฐ€ ์™„๋ฃŒ๋˜์—ˆ๋‹ค๋ฉด csv๋ฅผ ๋‹ค์šด๋กœ๋“œ ๋ฐ›์„ ์ˆ˜ ์žˆ๋Š” ๋ฒ„ํŠผ์ด ๋‚˜ํƒ€๋‚˜๋ฉด ๋‹ค์šด๋กœ๋“œ ๋ฐ›์Šต๋‹ˆ๋‹ค.
notion imagenotion image
ย 
๋‚˜์ค‘์— Django์— ๋‚ด์šฉ์„ ์ž‘์„ฑํ•  ๋•Œ ์‚ฌ์šฉํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์ž˜ ์ €์žฅํ•ด ๋‘๋„๋ก ํ•ฉ๋‹ˆ๋‹ค.
์„œ๋ฒ„๋ฅผ ์ž ์‹œ ๋ฉˆ์ถ”๊ณ  ํ„ฐ๋ฏธ๋„์—์„œ ํŒจํ‚ค์ง€๋ฅผ ์„ค์น˜ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.
pip install boto3 pip install django-storages
ย 
์„ค์น˜๋ฅผ ์™„๋ฃŒํ–ˆ๋‹ค๋ฉด settings.py ํŒŒ์ผ์„ ์ˆ˜์ •ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.
ํŒŒ์ผ๋ช… : settings.py
... STATIC_URL='/static/' AWS_ACCESS_KEY_ID='IAM ์—‘์„ธ์Šคํ‚ค ID' AWS_SECRET_ACCESS_KEY=' ๋น„๋ฐ€ ์—‘์„ธ์Šค ํ‚ค ' AWS_REGION='ap-northeast-2c' AWS_STORAGE_BUCKET_NAME='๋ฒ„ํ‚ท๋„ค์ž„' AWS_S3_CUSTOM_DOMAIN='%s.s3.%s.amazonaws.com' % (AWS_STORAGE_BUCKET_NAME, AWS_REGION) AWS_S3_OBJECT_PARAMETERS ={ 'CacheControl': 'max-age=86400', } AWS_DEFAULT_ACL='public-read' AWS_LOCATION='static' STATIC_URL='https://%s/%s/' % (AWS_S3_CUSTOM_DOMAIN, AWS_LOCATION) STATICFILES_STORAGE= 'storages.backends.s3boto3.S3Boto3Storage' DEFAULT_FILE_STORAGE= 'config.asst_storage.MediaStorage'
ย 
์•„๊นŒ ๋‹ค์šด๋กœ๋“œ ๋ฐ›์€ csv ํŒŒ์ผ์„ ์—ด์–ด์„œ ํ•ด๋‹น ๋ถ€๋ถ„์„ ์ฑ„์›Œ์ฃผ์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค.
๊ทธ๋ฆฌ๊ณ  asset_storage.py ํŒŒ์ผ์„ ๋งŒ๋“ค์–ด์ค๋‹ˆ๋‹ค.
ํŒŒ์ผ๋ช… : asset_storage.py
from storages.backends.s3boto3 import S3Boto3Storage class MediaStorage(S3Boto3Storage): location='media' file_overwrite = False
ย 
๋””๋ ‰ํ„ฐ๋ฆฌ์— static/css/main.css ํŒŒ์ผ์„ ๋งŒ๋“ญ๋‹ˆ๋‹ค.
ย 
ํ„ฐ๋ฏธ๋„์— ์•„๋ž˜ ๋ช…๋ น์–ด๋ฅผ ์ž‘์„ฑํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.
python manage.py collectstatic yes
ย 
ํŒŒ์ผ์ด ์—…๋กœ๋“œ ๋˜์–ด ์žˆ๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
notion imagenotion image
ย 
notion imagenotion image
ย