Nav*_*een 4 python django static django-staticfiles google-cloud-storage
我正在尝试从 Cloud Storage Bucket 为我的 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>
Run Code Online (Sandbox Code Playgroud)
设置.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,)
Run Code Online (Sandbox Code Playgroud)
关于此任务需要哪些所有额外更改的任何建议?
谢谢,
Nie*_*ano 13
主要参考:
先决步骤
转到 GCP: Cloud Storage (GCS) 并点击 CREATE BUCKET(根据需要填写)
创建后,如果您希望它像您网站的 CDN 一样(存储您的静态文件,例如 css、图像、视频等),您可以将其设为公开。
方法 1(更简单更快,但需要不断手动将文件复制到 GCS)
# 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/
Run Code Online (Sandbox Code Playgroud)
在你的 home.html
{% load static %}
<link rel="stylesheet" type="text/css" href="{% static 'home/css/home.css' %}">
Run Code Online (Sandbox Code Playgroud)
然后在你的 home.css 文件中
background-image: url("../assets/img/myHandsomeImage.jpg");
Run Code Online (Sandbox Code Playgroud)
home.css 链接现在将转换为:
https://storage.googleapis.com/[your_bucket_name]/home/css/home.css
而 myHandsomeImage.jpg 将是:
https://storage.googleapis.com/[your_bucket_name]/home/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
Run Code Online (Sandbox Code Playgroud)
转到 STATIC_ROOT 文件夹并将其内容上传到 GCS。通过 GCS GUI 控制台或通过 Google 提供的工具“gsutil”和 rsync 手动上传它们
现在,您的 GCS 存储桶已包含您的静态文件,您的 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
Run Code Online (Sandbox Code Playgroud)
pip3 install django-storages # https://pypi.org/project/django-storages/
pip3 install google-cloud-storage # https://pypi.org/project/google-cloud-storage/
Run Code Online (Sandbox Code Playgroud)
[如果您使用的是 Google 基础架构之外的计算机,则为强制性步骤] 转到 GCP:IAM、服务帐户,然后点击创建服务帐户
在 settings.py 中配置 Django 的静态文件设置
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
Run Code Online (Sandbox Code Playgroud)
python3 manage.py collectstatic
Run Code Online (Sandbox Code Playgroud)
基本上你需要:
检查步骤 1-4 https://cloud.google.com/python/django/container-engine#deploy_the_app_to_container_engine
| 归档时间: |
|
| 查看次数: |
5987 次 |
| 最近记录: |