表題の通り、S3へメディアデータ(.png)を保存はできますが、アプリ上でデータを取得できず途方に暮れています。どうしたらデータを取得できるのか、お分かりの方御指南いただけるとありがたいです。よろしくお願いいたします。
###Procfile
release: python manage.py migrate
web: gunicorn config.wsgi --log-file -
###requirements.txt
boto3==1.17.80
botocore==1.20.80
dj-database-url==0.5.0
Django==2.2.23
django-environ==0.4.5
django-heroku==0.3.1
django-storages==1.11.1
gunicorn==20.1.0
jmespath==0.10.0
Pillow==8.2.0
psycopg2==2.8.6
python-dateutil==2.8.1
pytz==2021.1
s3transfer==0.4.2
six==1.16.0
sqlparse==0.4.1
urllib3==1.26.4
whitenoise==5.2.0
###runtime.txt
python-3.6.13
###S3設定等
- パブリックアクセス設定:すべてオフ
- バケットポリシー:記述なし
- ACL:デフォルト
- CORS:以下の通り
json
1[ 2 { 3 "AllowedHeaders": [ 4 "*" 5 ], 6 "AllowedMethods": [ 7 "GET", 8 "POST", 9 "PUT" 10 ], 11 "AllowedOrigins": [ 12 "mydomain/*" 13 ], 14 "ExposeHeaders": [], 15 "MaxAgeSeconds": 3000 16 } 17]
###settings/production.py
python
1AWS_ACCESS_KEY_ID = os.environ['AWS_ACCESS_KEY_ID'] 2AWS_SECRET_ACCESS_KEY = os.environ['AWS_SECRET_ACCESS_KEY'] 3AWS_STORAGE_BUCKET_NAME = os.environ['AWS_STORAGE_BUCKET_NAME'] 4AWS_S3_CUSTOM_DOMAIN = '%s.s3.amazonaws.com' % AWS_STORAGE_BUCKET_NAME 5AWS_S3_OBJECT_PARAMETERS = { 6'CacheControl': 'max-age=86400', 7} 8AWS_LOCATION = 'media' 9DEFAULT_FILE_STORAGE = 'config.settings.storage_backends.MediaStorage' 10MEDIA_URL = "https://%s/%s/" % (AWS_S3_CUSTOM_DOMAIN, AWS_LOCATION)
###settings/storage_backends.py
python
1from storages.backends.s3boto3 import S3Boto3Storage 2 3class MediaStorage(S3Boto3Storage): 4 location = 'media' 5 file_overwrite = False
###model.py
python
1from django.db import models 2 3class BoardModel(models.Model): 4 title = models.CharField(max_length=100) 5 content = models.TextField() 6 author = models.CharField(max_length=100) 7 images = models.ImageField(upload_to='') 8 good = models.IntegerField(null=True, blank=True, default=0) 9 read = models.IntegerField(null=True, blank=True, default=0) 10 readtext = models.CharField(max_length=200, null=True, blank=True, default='default') 11
###views.py
python
1from django.shortcuts import render 2from django.contrib.auth.models import User 3from django.contrib.auth import authenticate, login, logout 4from django.shortcuts import redirect 5from django.contrib.auth.decorators import login_required 6from django.views.generic import CreateView 7from django.urls import reverse_lazy 8 9from .models import BoardModel 10 11def detailfunc(request, pk): 12 object = BoardModel.objects.get(pk=pk) 13 return render(request, 'detail.html', {'object':object}) 14 15class BoardCreate(CreateView): 16 template_name = 'create.html' 17 model = BoardModel 18 fields = ('title', 'content', 'author', 'images') 19 success_url = reverse_lazy('list')
###html
#####detail.html
{% block content %}
あなたの回答
tips
プレビュー