Django 忽略上传到 S3 的静态配置

问题描述 投票:0回答:1

我之前对媒体文件也遇到过同样的问题,我做了所有配置,但 django 一直在本地保存媒体文件而不是在 S3 中,我认为这是通过实现继承自 S3Boto3Storage 的自定义类来解决的。现在我想将静态文件保存在同一个 S3 中,但我遇到了同样的问题,django 完全忽略我的配置并继续将文件本地保存在 staticfiles 目录中。

这些是我安装的应用程序

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'storages',
    'app',
    'crispy_forms',
    'crispy_bootstrap5',
]

当前正在运行的媒体文件设置:

AWS_ACCESS_KEY_ID = config('AWS_ACCESS_KEY_ID')
AWS_SECRET_ACCESS_KEY = config('AWS_SECRET_ACCESS_KEY')
AWS_STORAGE_BUCKET_NAME = config('AWS_STORAGE_BUCKET_NAME')
AWS_S3_ENDPOINT_URL = config('AWS_S3_ENDPOINT_URL')
AWS_S3_OBJECT_PARAMETERS = {
    'CacheControl': 'max-age=86400',
}
AWS_MEDIA_LOCATION = 'media'
AWS_S3_REGION_NAME = config('AWS_S3_REGION_NAME')
AWS_DEFAULT_ACL = 'public-read'
AWS_QUERYSTRING_AUTH = False
AWS_S3_CONNECTION_TIMEOUT = 60
MEDIA_URL = f"https://{AWS_STORAGE_BUCKET_NAME}.{AWS_S3_REGION_NAME}.digitaloceanspaces.com/{AWS_MEDIA_LOCATION}/"
PUBLIC_MEDIA_LOCATION = 'media'
DEFAULT_FILE_STORAGE = 'cms.storage_backends.PublicMediaStorage'
DEFAULT_PROFILE_PHOTO=config('DEFAULT_PROFILE_PHOTO')

静态文件配置,其思想是从环境变量中读取 CMS_STATIC_LOCAL_STORAGE,但出于测试目的,该值会不断设置:

CMS_STATIC_LOCAL_STORAGE = False
print('Static Local Storage: ', CMS_STATIC_LOCAL_STORAGE)
AWS_STATIC_LOCATION = 'static'
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
if CMS_STATIC_LOCAL_STORAGE:
    STATIC_URL = '/static/'
    STATICFILES_DIRS = [
        os.path.join(BASE_DIR, 'static'),
    ]
else:
    STATIC_URL = f"https://{AWS_S3_REGION_NAME}.digitaloceanspaces.com/{AWS_STORAGE_BUCKET_NAME}/{AWS_STATIC_LOCATION}/"
    STATICFILES_STORAGE = 'cms.storage_backends.StaticStorage'

请注意,我也已经尝试过这样设置

STATIC_URL = f"https://{AWS_STORAGE_BUCKET_NAME}.{AWS_S3_REGION_NAME}.digitaloceanspaces.com/{AWS_STATIC_LOCATION}/"

storage_backends.py 文件:

from storages.backends.s3boto3 import S3Boto3Storage

class PublicMediaStorage(S3Boto3Storage):
    location = 'media'
    default_acl = 'public-read'
    file_overwrite = False

    @property
    def querystring_auth(self):
        return False

class StaticStorage(S3Boto3Storage):
    location = 'static'
    default_acl = 'public-read'

我发誓这个问题让我发疯,我已经尝试解决它好几个小时了。正如我告诉你的,同样的事情也发生在我的媒体文件上,我想我用自定义类解决了它,但对于静态文件,我在任何地方都找不到解决方案。

(env)  msi@fedora  ~/Escritorio/django_cms   develop ±  ./manage.py collectstatic --no-input
Running server with DEBUG=False and ALLOWED_HOSTS=['localhost']
Static Local Storage:  False

起初我尝试根据 CMS_STATIC_LOCAL_STORAGE 变量动态进行设置,但我什至尝试直接这样做并得到相同的结果。 我还尝试将 STATIC_ROOT 声明放入 CMS_STATIC_LOCAL_STORAGE 的 if true 内,但我在控制台上收到以下错误:

(env)  msi@fedora  ~/Escritorio/django_cms   develop ±  ./manage.py collectstatic --no-input
Running server with DEBUG=False and ALLOWED_HOSTS=['localhost']
Static Local Storage:  False
Traceback (most recent call last):
  File "/home/msi/Escritorio/django_cms/./manage.py", line 22, in <module>
    main()
...
...
  File "/home/msi/Escritorio/django_cms/env/lib/python3.11/site-packages/django/contrib/staticfiles/storage.py", line 39, in path
    raise ImproperlyConfigured(
django.core.exceptions.ImproperlyConfigured: You're using the staticfiles app without having set the STATIC_ROOT setting to a filesystem path.

更新 测试我意识到前端已经重定向到 Digital Ocean 的 S3,但是在执行收集静态时它继续保存在我的本地,因此前端无法正确渲染。

DOM

django django-staticfiles django-settings django-storage digital-ocean-spaces
1个回答
0
投票

我可能也有同样的问题。您最近升级到 Django 5.1 了吗?

S3 一如既往地配置,但

default_storage.url()
现在在 Django 5.1 中返回本地 URL。

$ python manage.py shell
Python 3.10.0
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> from django.conf import settings
>>> settings.DEFAULT_FILE_STORAGE
'storages.backends.s3boto3.S3Boto3Storage'
>>> from django.core.files.storage import default_storage
>>> default_storage.url('subfolder/dummyfile.txt')
>>> 'subfolder/dummyfile.txt' (instead of https://{}.s3.amazonaws.com/subfolder/dummyfile.txt?signature...)

尝试从 Django 5.1 降级到 5.0,似乎对我有用。

© www.soinside.com 2019 - 2024. All rights reserved.