我试图从云存储桶中为我的django应用程序提供静态文件,但不知道确切的过程。有人能建议一个合适的方法吗?
我所做的步骤:
/etc/apache2/sites-available/default-ssl.conf
文件。文件内容:
<VirtualHost *:443>
ServerName example.com
ServerAdmin admin@example.com
# Alias /static /opt/projects/example-google/example_static
Alias /static https://storage.googleapis.com/www.example.com/static
<Directory /opt/projects/example-google/example_static>
Require all granted
</Directory>
<Directory /opt/projects/example-google/example/example>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
WSGIDaemonProcess example python-path=/opt/projects/example-google/example:/opt/projects/example-google/venv/lib/python2.7/site-packages
WSGIProcessGroup example
WSGIApplicationGroup %{GLOBAL}
WSGIScriptAlias / /opt/projects/example-google/example/example/wsgi.py
SSLEngine on
SSLCertificateFile /etc/apache2/ssl/example.com.crt
SSLCertificateKeyFile /etc/apache2/ssl/example.com.key
SSLCertificateChainFile /etc/apache2/ssl/intermediate.crt
</VirtualHost>
settings.py文件:
# Static files (CSS, JavaScript, Images)
STATIC_URL = '/static/'
# STATIC_URL = 'https://storage.googleapis.com/www.example.com/static/'
STATIC_ROOT = os.path.join(BASE_DIR, '../example_static')
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, '../example_media')
STATICFILES_DIRS = (os.path.join(BASE_DIR, 'static'), MEDIA_ROOT,)
关于这项任务需要哪些额外的更改,有什么建议吗?
主要参考资料:
先决条件步骤
# Tell Django about the different locations to where the static files used by the project can be found
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'templates'),
os.path.join(BASE_DIR, "yourapp1", "templates"),
os.path.join(BASE_DIR, "yourapp2", "static"),
os.path.join(BASE_DIR, "watever"),
"/home/me/Music/TaylorSwift/",
"/home/me/Videos/notNsfw/",
]
# If the command "collectstatic" is invoked, tell Django where to place all the collected static
# files from all the directories included in STATICFILES_DIRS. Be aware that configuring it with a
# path outside your /home/me means that you need to have permissions to write to that folder later
# on when you invoke "collectstatic", so you might need to login as root first or run it as sudo.
STATIC_ROOT = "/var/www/mywebsite/"
# Tell Django the base url to access the static files. Think of this as the "prefix" of the URL
# to where your static files are. Note that if you browse through your bucket and happen to see a
# URL such as "https://storage.cloud.google.com/<your_bucket_name>/someFileYouHaveUploaded", such
# URL requires that whoever accesses it should be currently logged-in with their Google accounts. If
# you want your static files to be publicly accessible by anyone whether they are logged-in or not,
# use the link "https://storage.googleapis.com/<your_bucket_name>/someFileYouHaveUploaded" instead.
STATIC_URL = "https://storage.googleapis.com/<your_bucket_name>/"
# References:
# https://docs.djangoproject.com/en/3.0/howto/static-files/
# https://docs.djangoproject.com/en/3.0/howto/static-files/deployment/
# https://docs.djangoproject.com/en/3.0/ref/settings/
{% load static %}
<link rel="stylesheet" type="text/css" href="{% static 'home/css/home.css' %}">
background-image: url("../assets/img/myHandsomeImage.jpg");
当然,如果您愿意,您可以只放置绝对路径(完整的URL),但是这样的配置总是需要您手动更新所使用的URL,就像您切换到开发模式并且只想在本地而不是从GCS访问静态文件一样。
python3 manage.py collectstatic
# or if your STATIC_ROOT folder requires permissions to write to it then:
# sudo python3 manage.py collectstatic
转到STATIC_ROOT文件夹并将其内容上传到GCS。要么通过GCS GUI控制台手动上传,要么通过Google提供的工具“gsutil”和rsync手动上传
现在,GCS bucket已经包含了静态文件,Django项目被配置为通过配置的static_URL直接访问这些文件。
方法2(更长,但之后不需要手动复制)
python3 -m venv path/to/the/target/location/for/the/virtual/environment
source path/to/the/target/location/for/the/virtual/environment/bin/activate
pip3 install django-storages # https://pypi.org/project/django-storages/
pip3 install google-cloud-storage # https://pypi.org/project/google-cloud-storage/
[如果您在Google基础结构之外的计算机上,则强制步骤]转到gcp:IAM,Service Accounts,然后单击CREATE Service ACCOUNT
STATICFILES_DIRS = ['same_values_as_in_method_1_above']
DEFAULT_FILE_STORAGE = 'storages.backends.gcloud.GoogleCloudStorage'
GS_BUCKET_NAME = 'your_bucket_name'
STATICFILES_STORAGE = 'storages.backends.gcloud.GoogleCloudStorage'
STATIC_URL = 'https://storage.googleapis.com/<your_bucket_name>/'
from google.oauth2 import service_account
GS_CREDENTIALS = service_account.Credentials.from_service_account_file(
'path/to/the/downloaded/json/key/credentials.json' # see step 3
)
# There are 2 ways to authenticate, you could either do 1 of the following
# 1. Define the variable GS_CREDENTIALS in the settings.py (as done above), or just
# 2. write the command "export GOOGLE_APPLICATION_CREDENTIALS='path/to/credentials.json'" in the shell where you would run the "collectstatic" command
python3 manage.py collectstatic
问题内容: 我正在尝试将我一直在工作的Django网站从开发服务器阶段移至真正的托管环境。目前,我只是在我的个人计算机上托管。我已经安装了Apache和mod-wsgi,但是在获取静态文件时遇到了问题。我很确定这与Apache有关。这是我的站点配置文件: 我正在尝试从我使用静态别名的目录中托管文件。当我尝试加载网站时,所有内容都出现了,但是没有CSS。另外,当我访问我的URL www.server
这是可行的,但这里的问题是,在流回此方法的客户端之前,它必须首先缓冲所有字节。这会导致很多延迟,尤其是当存储在GCS中的文件很大时。 是否有一种方法可以从GCS获取文件并将其直接流到OutputStream,这里的OutputStream是针对servlet的。
问题内容: 让我感谢Stack Overflow社区的人们帮助我解决了各种Django和Apache(带有mod_wsgi)错误。到目前为止,我已经问了5个相关的问题,现在我越来越接近在生产站点上发布内容了! 我读到有关,(很快就会过时),并在Apache配置中设置。我试图逐个测试每个解决方案,但是我什么也做不了。 这是我的管理网站现在的样子 我也有一个奇怪的情况,那就是 任何子 域都可以在我的服
我目前正在一个在Appengine标准环境下运行flask的项目中工作,我试图提供一个已经上传到我项目默认Appengine存储桶中的Google云存储的图像。 这是我目前拥有的路由代码: 实际上,我试图提供一个图像,我已经上传到我的app engine项目中的默认云存储桶中。 我还尝试使用以下代码为映像提供服务,但没有成功:
问题内容: 我正在使用,我的django应用的结构如下 的是 在我尝试使用它为: 但是它没有加载并给出错误 我跑去收集所有静态文件。 为什么要加载CSS文件?是否缺少任何配置? 请提出建议。 问题答案: 根据文档正确的方式来加载静态文件是 这会工作