AWS S3存储桶Django 3.0用户配置文件图像上传访问错误
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了AWS S3存储桶Django 3.0用户配置文件图像上传访问错误相关的知识,希望对你有一定的参考价值。
INTRO
- 我按照建议遵循this指南,这里是guide's GitHub repo。
- 我也创建了一个AmazonS3FullAccess
- 我正在使用指南的第三个示例“ 混合公共资产和私有资产”,其中包含静态,媒体公共,媒体,私有版本。
- 如果用户登录(本地开发环境),他将从网站上载文件,但是他不能仅从AWS S3管理网站从网站上访问它们。
- 当前,由于指南中的内容,我正在阻止所有公共访问(AWS S3管理面板设置)
- 我已将这些行从this other guide添加到我的CORS配置编辑器中>
<?xml version="1.0" encoding="UTF-8"?>
<CORSConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
<CORSRule>
<AllowedOrigin>*</AllowedOrigin>
<AllowedMethod>GET</AllowedMethod>
<AllowedMethod>POST</AllowedMethod>
<AllowedMethod>PUT</AllowedMethod>
<AllowedHeader>*</AllowedHeader>
</CORSRule>
</CORSConfiguration>
- 打开了对我来说更本地的中央EU服务器。没有用,我遇到了同样的错误。
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
settings.py
AWS_ACCESS_KEY_ID = 'DSHUGASGHLASF678FSHAFH' AWS_SECRET_ACCESS_KEY = 'uhsdgahsfgskajgjkafgjkdfjkgkjdfgfg' AWS_STORAGE_BUCKET_NAME = 'MYSTORAGE289377923' AWS_S3_CUSTOM_DOMAIN = '%s.s3.amazonaws.com' % AWS_STORAGE_BUCKET_NAME AWS_S3_OBJECT_PARAMETERS = 'CacheControl': 'max-age=86400', AWS_STATIC_LOCATION = 'static' STATICFILES_STORAGE = 'mysite.storage_backends.StaticStorage' STATIC_URL = "https://%s/%s/" % (AWS_S3_CUSTOM_DOMAIN, AWS_STATIC_LOCATION) AWS_PUBLIC_MEDIA_LOCATION = 'media/public' DEFAULT_FILE_STORAGE = 'mysite.storage_backends.PublicMediaStorage' AWS_PRIVATE_MEDIA_LOCATION = 'media/private' PRIVATE_FILE_STORAGE = 'mysite.storage_backends.PrivateMediaStorage' AWS_S3_HOST = "s3.eu-central-1.amazonaws.com" S3_USE_SIGV4 = True AWS_S3_REGION_NAME = "eu-central-1"
models.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')
views.py
from django.contrib.auth.decorators import login_required from django.views.generic.edit import CreateView from django.urls import reverse_lazy from django.utils.decorators import method_decorator from .models import Document, PrivateDocument 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 @method_decorator(login_required, name='dispatch') class PrivateDocumentCreateView(CreateView): model = PrivateDocument fields = ['upload', ] success_url = reverse_lazy('profile') def form_valid(self, form): self.object = form.save(commit=False) self.object.user = self.request.user self.object.save() return super().form_valid(form)
ERROR
This XML file does not appear to have any style information associated with it. The document tree is shown below. <Error> <Code>AccessDenied</Code> <Message>Access Denied</Message> <RequestId>56fg67dfg56df7g67df</RequestId> <HostId> hsiugYIGYfhuieHF7weg68g678dsgds78g67dsg86sdg68ds7g68ds7yfsd8f8hd7 </HostId> </Error>
到目前为止我尝试过的事情
- 在一段时间之间,它创建了AWS链接,并且还将文件添加到了本地“ media”文件夹。但是由于我已经删除了“媒体文件夹”,并且它仅创建URL链接,并且实际上将它们上传到S3存储桶
- 我在aws论坛上找到了相同的question,但尚未得到答案
- 访问权限Django + AWS S3 Bucket: Authenticated Access to S3 Bucket(我不知道此答案https://stackoverflow.com/a/21614550/10270590)
- “使用AWS4-HMAC-SHA256”
- 将S3主机的区域指定为正确的用法https://github.com/aws/aws-sdk-js/issues/829
- 查找您所在地区的网站-https://docs.aws.amazon.com/general/latest/gr/rande.html
- 我也收到建议,“大多数新区域仅支持
AWS4-HMAC-SHA256
-如果您的代码不支持此身份验证方案,并且仅创建了“ v2签名” 在一个旧区域中创建存储桶,例如在欧洲,似乎只有Ireland-在此处查看:https://docs.aws.amazon.com/general/latest/gr/signature-version-2.html“ - 我在使用测试机的欧盟-我在美国建立了S3存储桶 -如何配置Django应用或AWS S3存储桶,以便可以从任何地方访问它(尤其重要的是,部署该应用程序以使世界各地的人们都可以访问它)。来自同一video's评论部分的人评论了以下
Steve D Great video series, just to say I am using an S3 bucket in Europe and needed to add additional settings AWS_S3_HOST = "s3.eu-west-2.amazonaws.com" and AWS_S3_REGION_NAME="eu-west-2" to make it work
AWS_S3_HOST = "s3.eu-central-1.amazonaws.com"
S3_USE_SIGV4 = True
AWS_S3_REGION_NAME = "eu-central-1"
INTRO我已按照建议遵循此指南,这是该指南的GitHub存储库。我也使用它创建了一个AmazonS3FullAccess,它使用的是指南的第三个示例“混合公共资产和...
答案
[在AWS控制台中,单击“权限”选项卡,然后在“桶策略”按钮上。将出现一个编辑框。粘贴以下内容:
以上是关于AWS S3存储桶Django 3.0用户配置文件图像上传访问错误的主要内容,如果未能解决你的问题,请参考以下文章
SonataMediaBundle - S3 AWS:'配置的存储桶“我的存储桶”不存在