Amazon Simple Storage Service(S3)는 AWS에서 제공하는 객체 스토리지 서비스입니다. S3는 데이터를 안전하게 저장하고, 검색하고, 다른 AWS 서비스와 연동하여 활용할 수 있는 유연하고 신뢰할 수 있는 솔루션을 제공합니다.
{
"Version": "2012-10-17",
"Id": "정책 만들면 생성되는 id",
"Statement": [
{
"Sid": "정책 만들면 생성되는 sid",
"Effect": "Allow",
"Principal": "*",
"Action": "s3:GetObject",
"Resource": "arn:aws:s3:::bucket_name/static/*"
},
{
"Sid": "정책 만들면 생성되는 sid",
"Effect": "Allow",
"Principal": "*",
"Action": ["s3:GetObject", "s3:DeleteObject", "s3:PutObject" ]
"Resource": "arn:aws:s3:::bucket_name/media/*"
},
{
"Sid": "정책 만들면 생성되는 sid",
"Effect": "Allow",
"Principal": {
"AWS": "my-IAM"
},
"Action": [ "s3:GetObject","s3:DeleteObject", "s3:PutObject" ],
"Resource": "arn:aws:s3:::bucket_name/*"
}
]
}
ACL은 버킷이나 객체에 대해 요청자의 권한 허용 범위를 설정합니다.
INSTALLED_APPS = [
"storages"
]
AWS_ACCESS_KEY_ID='......'
AWS_SECRET_ACCESS_KEY='......'
from functools import lru_cache
from pathlib import Path
from decouple import config as decouple_config, Config, RepositoryEnv
# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent
PROJECT_DIR = BASE_DIR.parent
ENV_FILE_PATH = PROJECT_DIR / ".env-prod"
@lru_cache()
def get_config():
if ENV_FILE_PATH.exists():
return Config(RepositoryEnv(str(ENV_FILE_PATH)))
return decouple_config
config = get_config()
from core.env import config
AWS_ACCESS_KEY_ID=config("AWS_ACCESS_KEY_ID", default=None)
AWS_SECRET_ACCESS_KEY=config("AWS_SECRET_ACCESS_KEY", default=None)
AWS_S3_ADDRESSING_STYLE = "virtual"
AWS_STORAGE_BUCKET_NAME=config("AWS_STORAGE_BUCKET_NAME", default="estsoft-ormi-bookstore")
AWS_S3_REGION_NAME="ap-northeast-2"
AWS_DEFAULT_ACL="public-read"
AWS_S3_USE_SSL=True
DEFAULT_FILE_STORAGE = 'core.storages.backends.MediaStorage'
STATICFILES_STORAGE = 'core.storages.backends.StaticFileStorage'
https://github.com/jschneier/django-storages/issues/782
django v4.2 이하
DEFAULT_FILE_STORAGE = 'core.storages.backends.MediaStorage'
STATICFILES_STORAGE = 'core.storages.backends.StaticFileStorage'
from storages.backends.s3boto3 import S3Boto3Storage
class MediaStorage(S3Boto3Storage):
location = "media"
class StaticFileStorage(S3Boto3Storage):
location = "static"
django v4.2 이상
STORAGES = {
"staticfiles": {
"BACKEND": "storages.backends.s3boto3.StaticFileStorage",
},
"default": {
"BACKEND": "storages.backends.s3boto3.MediaStorage",
}
}
이라고 공식 문서에 되어있으나 동작하지 않았음.
4.2이상 버전에서도, 4.2 이하버전으로 작성하니 동작됨
https://django-storages.readthedocs.io/en/latest/backends/amazon-S3.html
from storages.backends.s3boto3 import S3Boto3Storage
class MediaStorage(S3Boto3Storage):
location = "media"
class StaticFileStorage(S3Boto3Storage):
location = "static"
python manage.py collectstatic
[
{
"AllowedHeaders": [
"*"
],
"AllowedMethods": [
"HEAD",
"PUT",
"GET",
"POST",
"DELETE"
],
"AllowedOrigins": [
"*"
],
"ExposeHeaders": [
"x-amz-request-id",
"x-amz-server-side-encryption",
"x-amz-version-id",
"Content-Length",
"Content-Type",
"Connection",
"Date",
"ETag",
"x-amz-delete-marker"
]
}
]
https://docs.aws.amazon.com/ko_kr/sdk-for-javascript/v2/developer-guide/cors.html
https://docs.aws.amazon.com/ko_kr/AmazonS3/latest/userguide/ManageCorsUsing.html
https://docs.aws.amazon.com/AmazonS3/latest/API/RESTCommonResponseHeaders.html