本文介绍了'python manage.py collectstatic' 仍在收集静态到 C 驱动器而不是 s3 存储桶,我错过了什么吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将 s3 存储桶与 django 一起使用(我之前已经这样做过两次),但这一次,在安装 boto3 和 django-storages 并为 settings.py 中的必要变量分配正确的值之后,python 管理.py collectstatic 仍在将静态文件收集到我计算机上的本地目录而不是 s3 存储桶.下面是我的settings.py...

I am trying to use s3 bucket with django (I have done this like twice before) but this time, after installing boto3 and django-storages and assigning correct values to necessary variables in settings.py, python manage.py collectstatic is still collecting static files to a local directory on my computer instead of s3 bucket. Below is my settings.py...

settings.py

INSTALLED_APPS = [
    ...
    "storages",
]

AWS_ACCESS_KEY_ID = "*****"
AWS_SECRET_ACCESS_KEY = "******"
AWS_STORAGE_BUCKET_NAME = "****"
AWS_S3_CUSTOM_DOMAIN = "%s.s3.amazonaws.com" % AWS_STORAGE_BUCKET_NAME
AWS_S3_OBJECT_PARAMETERS = {"CacheControl": "max-age=86400"}
AWS_DEFAULT_ACL = None
AWS_LOCATION = 'static'

STATICFILES_DIRS = [
    BASE_DIR / "build/static", #this is the correct path by the way!
]

STATIC_URL = "https://%s/%s/" % (AWS_S3_CUSTOM_DOMAIN, AWS_LOCATION)
STATICFILES_STORAGE = 'storages.backends.s3boto3.S3Boto3Storage'

终端

(env) C:\Users\LENOVO\Desktop\project> python manage.py collectstatic

You have requested to collect static files at the destination
location as specified in your settings:

    C:\Users\LENOVO\Desktop\project\staticfiles

This will overwrite existing files!
Are you sure you want to do this?

根据所有教程和我的期望,collectstatic 应该将我的静态文件收集到我的 s3 存储桶中..我错过了什么吗??

According to all tutorials and my expectation, collectstatic is supposed to be collecting my static files into my s3 bucket.. Am I missing something??

感谢您的时间!

推荐答案

我倾向于定义自己的 S3Boto3Storage 子类并使用它

I tend to have define my own subclass of S3Boto3Storage and use that

# ./storage_backends.py - lives in the same dir as the config file.

from storages.backends.s3boto3 import S3Boto3Storage

from django.conf import settings


class StaticRootS3BotoStorage(S3Boto3Storage):
    location = settings.STATIC_DIRECTORY


然后

# config.py

STATIC_DIRECTORY = BASE_DIR + "/build/static" # You will need to change this to your path
STATICFILES_STORAGE = 'config.settings.storage_backends.StaticRootS3BotoStorage' # you also need to change this to the path to the file we created above.

STATIC_URL = 'https://%s/%s/' % (AWS_S3_CUSTOM_DOMAIN, STATIC_DIRECTORY)

现在您可以覆盖调试方法并查看问题可能出在哪里.

Now you can override methods for debugging and see where the problem might be.

这篇关于'python manage.py collectstatic' 仍在收集静态到 C 驱动器而不是 s3 存储桶,我错过了什么吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 14:50