首页 > 解决方案 > Django - 媒体文件在生产中进行 404(Azure 存储)

问题描述

在将应用程序部署到 azure 应用服务器时,对于媒体文件,我指的是 azure blob 存储。我正在使用https://django-storages.readthedocs.io/en/latest/backends/azure.html文档。

我已经在我的settings.py文件中编写了这些代码行。

DEFAULT_FILE_STORAGE = 'index.custom_azure.AzureMediaStorage'
STATICFILES_STORAGE = 'index.custom_azure.AzureStaticStorage'
STATIC_URL = f'https://{AZURE_CUSTOM_DOMAIN}/{STATIC_LOCATION}/'
MEDIA_URL = f'https://{AZURE_CUSTOM_DOMAIN}/{MEDIAFILES_LOCATION}/'

在 custom_azure.py 文件中,我包含了我的类

从 storages.backends.azure_storage 导入 AzureStorage

class AzureMediaStorage(AzureStorage):
    account_name = 'ACCOUNT_NAME' # Must be replaced by your <storage_account_name>
    account_key = 'KEY' # Must be replaced by your <storage_account_key>
    azure_container = 'media'
    expiration_secs = None
    location = 'media'
    file_overwrite = False

class AzureStaticStorage(AzureStorage):
    account_name = 'MY_STORAGE' # Must be replaced by your storage_account_name
    account_key = 'My_KEY' # Must be replaced by your <storage_account_key>
    azure_container = 'static'
    expiration_secs = None

我的 urls.py 文件是这样的。

urlpatterns = [
    path('',include('index.urls')),
]
if settings.DEBUG:
    urlpatterns +=  static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

我正在尝试引用 <http//domain.com/media/abc.png> 但不幸的是,它正在进行 404。

任何帮助表示赞赏!

标签: pythondjangoazure

解决方案


推荐阅读