Ausgabe
Ich möchte registrierten Benutzern einer Django-App erlauben, Dateien in einen s3-Bucket hochzuladen und anzuzeigen.
Mit Hilfe der ersten Kommentatoren zu dieser Frage und dieser Antwort auf Stackoverflow konnte ich dies mithilfe generierter vorsignierter URLs zum Laufen bringen, ohne dass der öffentliche Zugriff oder Richtlinien für meinen S3-Bucket zugelassen werden mussten.
Kann mir jemand helfen, wie meine Richtlinien und Einstellungen lauten sollten, um benutzerdefinierte Domänen zuzulassen und keine vorsignierten URLs für statische Dateien zu verwenden?
Danke vielmals. — R
Einstellungen.py
AWS_ACCESS_KEY_ID = env('AWS_ACCESS_KEY_ID')
AWS_SECRET_ACCESS_KEY = env('AWS_SECRET_ACCESS_KEY')
AWS_STORAGE_BUCKET_NAME = env('AWS_STORAGE_BUCKET_NAME')
AWS_S3_OBJECT_PARAMETERS = {'CacheControl': 'max-age=86400',}
AWS_STATIC_LOCATION = 'static'
'STATICFILES_STORAGE = f'{ROOT_NAME}.storage_backends.StaticStorage'
AWS_PUBLIC_MEDIA_LOCATION = 'media/public'
DEFAULT_FILE_STORAGE = f'{ROOT_NAME}.storage_backends.PublicMediaStorage'
AWS_PRIVATE_MEDIA_LOCATION = 'media/private'
PRIVATE_FILE_STORAGE = f'{ROOT_NAME}.storage_backends.PrivateMediaStorage'
storage_backends.py
from django.conf import settings
from storages.backends.s3boto3 import S3Boto3Storage
class StaticStorage(S3Boto3Storage):
location = settings.AWS_STATIC_LOCATION
class PublicMediaStorage(S3Boto3Storage):
location = settings.AWS_PUBLIC_MEDIA_LOCATION
file_overwrite = False
class PrivateMediaStorage(S3Boto3Storage):
location = settings.AWS_PRIVATE_MEDIA_LOCATION
default_acl = 'private'
file_overwrite = False
custom_domain = False
Ansichten.py
class DocumentCreateView(CreateView):
model = Document
fields = ['upload', ]
success_url = reverse_lazy('home')
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
documents = Document.objects.all()
context['documents'] = documents
return context
Modelle.py
from django.db import models
from django.conf import settings
from django.contrib.auth.models import User
from mysite.storage_backends import PrivateMediaStorage
class Document(models.Model):
uploaded_at = models.DateTimeField(auto_now_add=True)
upload = models.FileField()
class PrivateDocument(models.Model):
uploaded_at = models.DateTimeField(auto_now_add=True)
upload = models.FileField(storage=PrivateMediaStorage())
user = models.ForeignKey(User, related_name='documents', on_delete=models.CASCADE,)
Lösung
Mit diesen Einstellungen und der Hilfe [dieses Tutorials] [1] habe ich alles wie gewünscht zum Laufen gebracht. Ich hoffe, das ist für jemanden nützlich.
AWS_STATIC_LOCATION = 'static'
STATICFILES_DIRS = [ os.path.join(BASE_DIR, '/static'), os.path.join(BASE_DIR, 'static'),]
STATICFILES_STORAGE = f'{ROOT_NAME}.storage_backends.StaticStorage'
AWS_PUBLIC_MEDIA_LOCATION = 'media/public'
DEFAULT_FILE_STORAGE = f'{ROOT_NAME}.storage_backends.PublicMediaStorage'
AWS_PRIVATE_MEDIA_LOCATION = 'media/private'
PRIVATE_FILE_STORAGE = f'{ROOT_NAME}.storage_backends.PrivateMediaStorage'
AWS_DEFAULT_ACL = None #'public-read'
#AWS_QUERYSTRING_AUTH = False
MEDIA_URL = "/media/"```
[1]: https://simpleisbetterthancomplex.com/tutorial/2017/08/01/how-to-setup-amazon-s3-in-a-django-project.html
Beantwortet von – Roger
Antwort geprüft von – Candace Johnson (FixError Volunteer)