沒有S3媒體文件,我想服務於亞馬遜S3我的Django項目(託管在Heroku)的靜態和媒體文件。靜態文件現在可以在本地和部署中正常使用,但媒體文件只能在本地使用。我可以確認文件被添加並且當本地主機提供正確地從我鬥送達,但既不節約,也不負荷生產工作。當我嘗試成爲一個媒體圖像(我手動添加到鬥),我得到一個403次拒絕訪問錯誤:在Heroku的部署Django項目
<Error>
<Code>AccessDenied</Code>
<Message>Access Denied</Message>
<RequestId>7430A21BE321C26B</RequestId>
<HostId>
h/c1VQF57wrLJ7JdHXuJ8LCrQdZL9PfQZN4G38Ihg8UYCxSt0znbxWfiTSDxz0dCooQoqgW9tpw=
</HostId>
</Error>
當我嘗試保存文件(通過鶺鴒圖片上傳),我得到相當通用的Internal Server Error - 500
。我不知道問題在哪裏。我可以得到靜態而不是媒體文件事實上是一個什麼樣的如此混亂的部分(和讓我覺得這不只是一個權限錯誤)。以下是我認爲可能與問題有關的事情。
編輯:我意識到我有我的生產設置DEBUG = True
調試不同的生產錯誤。當我設置爲False,我現在得到的403錯誤靜態文件和媒體文件。
我鬥政策:
{
"Version": "2008-10-17",
"Statement": [
{
"Sid": "PublicReadForGetBucketObjects",
"Effect": "Allow",
"Principal": {
"AWS": "*"
},
"Action": "s3:GetObject",
"Resource": "arn:aws:s3:::owen-tribune/*"
},
{
"Effect": "Allow",
"Principal": {
"AWS": "arn:aws:iam::494147019987:user/jtebert"
},
"Action": "s3:*",
"Resource": [
"arn:aws:s3:::owen-tribune",
"arn:aws:s3:::owen-tribune/*"
]
},
{
"Sid": "somethingElse",
"Action": "s3:ListBucket",
"Effect": "Allow",
"Resource": [
"arn:aws:s3:::owen-tribune",
"arn:aws:s3:::owen-tribune/*"
],
"Principal": {
"AWS": [
"arn:aws:iam::494147019987:user/jtebert"
]
}
}
]
}
相關部分的settings.py
:
AWS_ACCESS_KEY_ID = '###########'
AWS_SECRET_ACCESS_KEY = '################'
AWS_S3_HOST = 's3-us-east-2.amazonaws.com'
#AWS_SECRET_ACCESS_KEY = env['AWS_SECRET_ACCESS_KEY']
#AWS_ACCESS_KEY_ID = env['AWS_ACCESS_KEY_ID']
AWS_STORAGE_BUCKET_NAME = 'owen-tribune'
AWS_S3_CUSTOM_DOMAIN = '%s.s3.amazonaws.com' % AWS_STORAGE_BUCKET_NAME
STATICFILES_LOCATION = 'static'
STATICFILES_STORAGE = 'owen.custom_storages.StaticStorage'
STATIC_URL = "https://%s/%s/" % (AWS_S3_CUSTOM_DOMAIN, STATICFILES_LOCATION)
MEDIAFILES_LOCATION = 'media'
MEDIA_URL = "https://%s/%s/" % (AWS_S3_CUSTOM_DOMAIN, MEDIAFILES_LOCATION)
DEFAULT_FILE_STORAGE = 'custom_storages.MediaStorage'
自儲存在custom_storages.py
:
# custom_storages.py
from django.conf import settings
from storages.backends.s3boto import S3BotoStorage
class StaticStorage(S3BotoStorage):
location = settings.STATICFILES_LOCATION
class MediaStorage(S3BotoStorage):
location = settings.MEDIAFILES_LOCATION