S3와 django 연동

2023. 12. 7. 17:19project

참고 사이트

https://django-storages.readthedocs.io/en/latest/backends/amazon-S3.html

 

Amazon S3 — django-storages 1.14.2 documentation

Amazon S3 This backend implements the Django File Storage API for Amazon Web Services’s (AWS) Simple Storage Service (S3). Installation The backend is based on the boto3 library which must be installed; the minimum required version is 1.4.4 although we a

django-storages.readthedocs.io

 

 

필요패키지

 

# WAS에서 가상환경에 진입 후 아래 명령어 실행

pip install boto3 django-storages

 

 

AWS access key 생성

S3 접근을 위한 것이기 때문에 Other 선택 후 스크롤을 내려서 우하단 노란 박스 클릭

 

 

설명 입력 후 우하단 노란 박스 클릭

 

 

django와 S3 연동

 

# settings.py 수정

# APP 추가
INSTALLED_APPS = [
    'storages',
]


# S3 setting
AWS_REGION = '[리전명]'
AWS_STORAGE_BUCKET_NAME = '[버킷 이름]'
AWS_ACCESS_KEY_ID = '[access key ID]'
AWS_SECRET_ACCESS_KEY = '[secret access key]'

AWS_S3_CUSTOM_DOMAIN = '%s.s3.%s.amazonaws.com' % (AWS_STORAGE_BUCKET_NAME, AWS_REGION)


# Static Setting
STATIC_URL = "http://%s/static/" % AWS_S3_CUSTOM_DOMAIN
STATICFILES_STORAGE = 'storages.backends.s3boto3.S3Boto3Storage'


# Media Setting
MEDIA_URL = "http://%s/media/" % AWS_S3_CUSTOM_DOMAIN
DEFAULT_FILE_STORAGE = 'storages.backends.s3boto3.S3Boto3Storage'

 

# 정적파일을 수집하는 명령어 실행

python3 manage.py collectstatic

 

# S3에서 연동 확인

S3 해당 버킷에 admin 폴더가 생기면 연동 성공!

 

 

웹페이지 게시판에 너굴상점 이미지를 업로드하면 S3에 파일 저장

 

 

이미지 확인

'project' 카테고리의 다른 글

S3에 CloudFront 배포  (0) 2023.12.08
면접 참고 자료  (0) 2023.12.06
CSRF 검증  (0) 2023.12.06
Elasticache Redis  (1) 2023.12.06
오토 스케일링  (2) 2023.12.05