[〜#〜]イントロ[〜#〜]
<?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>
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)
[〜#〜]エラー[〜#〜]
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>
これまでに試したこと
AWS4-HMAC-SHA256
のみをサポートします。コードがこの認証スキームをサポートせず、作成された "v2署名"のみの場合」という推奨事項としてこれを受け取りました。古いリージョンの1つ、たとえばヨーロッパではバケットはアイルランドのようです-ここで確認してください https://docs.aws.Amazon。 com/general/latest/gr/signature-version-2.html "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"
ほとんどの新しいリージョンはAWS4-HMAC-SHA256
のみをサポートします-コードがこの認証スキームをサポートせず、作成された "v2署名"のみの場合古い地域の1つ、例えばヨーロッパではアイルランドだけのようです-ここでチェックしてください: https://docs.aws.Amazon.com/general/ latest/gr/signature-version-2.html
ところで、S3アップロード/ダウンロードコードは、いくつかの標準AWS SDKを使用する必要があります-Python is boto の場合。これらの公式SDKは、適切な認証方法、新しいリージョン、独自のSDKをロールアウトしないでください。
それが役に立てば幸い:)