简体   繁体   English

Django s3: 403 访问被拒绝,并生成 URL

[英]Django s3: 403 Access Denied, and generating URLs

I wish to allow registered users of a Django app to upload and view files to an s3 bucket.我希望允许 Django 应用程序的注册用户上传和查看文件到 s3 存储桶。

With the help of the first commenters on this question and this answer on stackoverflow I was able to get this working using generated presigned URLs with no need for allowing Public Access or any Policies on my S3 bucket.在这个问题的第一批评论者和stackoverflow 上的这个答案的帮助下,我能够使用生成的预签名 URL 来完成这项工作,而无需允许公共访问或我的 S3 存储桶上的任何策略。

Can anyone help with what my policy & settings should be to allow custom domains and not use pre-signed URLS for static files?任何人都可以帮助我的政策和设置应该是允许自定义域而不是对 static 文件使用预签名 URLS 吗?

Many thanks.非常感谢。 -- R -- R

Settings.py设置.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存储后端.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

views.py视图.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

models.py模型.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,)

I got everything working as needed with these settings and the help of this tutorial .通过这些设置和本教程的帮助,我使一切都按需要工作。 I hope this is useful to someone.我希望这对某人有用。

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/"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM