前提・実現したいこと
ここに質問の内容を詳しく書いてください。
Djangoのアプリ上でAmazonS3に保存した画像がリンク切れになります。
発生している問題・エラーメッセージ
検証ツールのコンソール Failed to load resource: the server responded with a status of 403 (Forbidden)
該当のソースコード
python
1""" 2Django settings for myproject project. 3 4Generated by 'django-admin startproject' using Django 2.1.5. 5 6For more information on this file, see 7https://docs.djangoproject.com/en/2.1/topics/settings/ 8 9For the full list of settings and their values, see 10https://docs.djangoproject.com/en/2.1/ref/settings/ 11""" 12 13import os 14 15# Build paths inside the project like this: os.path.join(BASE_DIR, ...) 16BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) 17 18 19# Quick-start development settings - unsuitable for production 20# See https://docs.djangoproject.com/en/2.1/howto/deployment/checklist/ 21 22 23 24# SECURITY WARNING: don't run with debug turned on in production! 25DEBUG = False 26 27ALLOWED_HOSTS = ['*'] 28 29 30# Application definition 31 32INSTALLED_APPS = [ 33 'django.contrib.admin', 34 'django.contrib.auth', 35 'django.contrib.contenttypes', 36 'django.contrib.sessions', 37 'django.contrib.messages', 38 'django.contrib.staticfiles', 39 'myapp', 40 'django.contrib.sites', 41 'sitemanage', 42 'storages', 43] 44 45SITE_ID = 1 46 47MIDDLEWARE = [ 48 'django.middleware.security.SecurityMiddleware', 49 'whitenoise.middleware.WhiteNoiseMiddleware', 50 'django.contrib.sessions.middleware.SessionMiddleware', 51 'django.middleware.common.CommonMiddleware', 52 'django.middleware.csrf.CsrfViewMiddleware', 53 'django.contrib.auth.middleware.AuthenticationMiddleware', 54 'django.contrib.messages.middleware.MessageMiddleware', 55 'django.middleware.clickjacking.XFrameOptionsMiddleware', 56 'django.contrib.sites.middleware.CurrentSiteMiddleware', 57] 58 59ROOT_URLCONF = 'myproject.urls' 60 61TEMPLATES = [ 62 { 63 'BACKEND': 'django.template.backends.django.DjangoTemplates', 64 'DIRS': [], 65 'APP_DIRS': True, 66 'OPTIONS': { 67 'context_processors': [ 68 'django.template.context_processors.debug', 69 'django.template.context_processors.request', 70 'django.contrib.auth.context_processors.auth', 71 'django.contrib.messages.context_processors.messages', 72 'django.template.context_processors.media', 73 'myapp.context_processors.all_category', 74 ], 75 }, 76 }, 77] 78 79WSGI_APPLICATION = 'myproject.wsgi.application' 80 81 82# Database 83# https://docs.djangoproject.com/en/2.1/ref/settings/#databases 84 85DATABASES = { 86 'default': { 87 'ENGINE': 'django.db.backends.sqlite3', 88 'NAME': os.path.join(BASE_DIR, 'db.sqlite3'), 89 } 90} 91 92 93# Password validation 94# https://docs.djangoproject.com/en/2.1/ref/settings/#auth-password-validators 95 96AUTH_PASSWORD_VALIDATORS = [ 97 { 98 'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator', 99 }, 100 { 101 'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator', 102 }, 103 { 104 'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator', 105 }, 106 { 107 'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator', 108 }, 109] 110 111 112# Internationalization 113# https://docs.djangoproject.com/en/2.1/topics/i18n/ 114 115LANGUAGE_CODE = 'ja' 116 117TIME_ZONE = 'Asia/Tokyo' 118 119USE_I18N = True 120 121USE_L10N = True 122 123USE_TZ = True 124 125 126# Static files (CSS, JavaScript, Images) 127# https://docs.djangoproject.com/en/2.1/howto/static-files/ 128 129STATIC_URL = '/static/' 130 131# ログイン機能の追加 132LOGIN_URL = 'myapp:login' 133LOGIN_REDIRECT_URL = 'myapp:index' 134 135# S3のための記述 136AWS_STORAGE_BUCKET_NAME = 'バケット名' 137AWS_S3_CUSTOM_DOMAIN = '%s.s3.amazonaws.com' % AWS_STORAGE_BUCKET_NAME 138AWS_S3_OBJECT_PARAMETERS = { 139 'CacheControl': 'max-age=86400'#一日はそのキャッシュを使う 140} 141AWS_LOCATION = 'media' 142DEFAULT_FILE_STORAGE = 'storages.backends.s3boto3.S3Boto3Storage' 143MEDIA_ROOT = os.path.join(BASE_DIR,'media') 144# MEDIA_URL = '/media/' 145MEDIA_URL = "https://%s/%s/" % (AWS_S3_CUSTOM_DOMAIN,AWS_LOCATION) 146# herokuのための追記 147 148import dj_database_url 149DATABASES['default'] = dj_database_url.config() 150 151SECURE_PROXY_SSL_HEADER = ('HTTP_X_FOWARDED_PROTO', 'https') 152 153ALLOWED_HOSTS = ['*'] 154 155STATICFILES_DIRS = (os.path.join(BASE_DIR, 'static'),) 156 157STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles') 158 159DEBUG = False 160 161try: 162 from .local_settings import * 163except ImportError: 164 pass 165 166if not DEBUG: 167 SECRET_KEY = os.environ['SECRET_KEY'] 168 AWS_ACCESS_KEY_ID = os.environ['AWS_ACCESS_KEY_ID'] 169 AWS_SECRET_ACCESS_KEY = os.environ['AWS_SECRET_ACCESS_KEY'] 170
settings.py asgiref==3.2.10 certifi==2020.6.20 chardet==3.0.4 dj-database-url==0.5.0 dj-static==0.0.6 Django==3.1 gunicorn==20.0.4 idna==2.8 Pillow==5.4.1 psycopg2-binary==2.8.2 pytz==2020.1 requests==2.21.0 setuptools==49.2.0 sqlparse==0.3.1 static3==0.7.0 urllib3==1.24.1 whitenoise==5.2.0 django-storages==1.10.1 boto3==1.15.0 botocore==1.18.0 jmespath==0.10.0 python-dateutil==2.8.1 s3transfer==0.3.3 six==1.15.0
試したこと
AWS_ACCESS_KEY_ID とAWS_SECRET_ACCESS_KEYの確認→問題ありませんでした。環境変数設定もOKです。
S3バケットのパブリックブロックをオフになっているか確認→オフになっていました。
補足情報(FW/ツールのバージョンなど)
python 3.7.9
django 3.1
あなたの回答
tips
プレビュー