我试图为我的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,)对于这项任务需要做什么额外的修改,有什么建议吗?
谢谢,
发布于 2020-04-30 08:30:09
主要参考资料:
先决条件步骤
- Go to your newly created bucket
- Go to Permissions and then Click Add members
- Add a new member "allUsers" with role "Cloud Storage - Storage Object Viewer"
- Reference: [https://cloud.google.com/storage/docs/quickstart-console](https://cloud.google.com/storage/docs/quickstart-console)
方法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/在你的home.html里
{% load static %}
<link rel="stylesheet" type="text/css" href="{% static 'home/css/home.css' %}">然后在你的home.css文件中
background-image: url("../assets/img/myHandsomeImage.jpg");现在,home.css链接将转换为:
而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方法2(更长,但不需要手动复制)
python3 -m venv path/to/the/target/location/for/the/virtual/environment
source path/to/the/target/location/for/the/virtual/environment/bin/activatepip3 install django-storages # https://pypi.org/project/django-storages/
pip3 install google-cloud-storage # https://pypi.org/project/google-cloud-storage/- Name: SomeName
- ID / email: somename
- Role: Project - Owner
- CREATE KEY, select JSON
- Store the downloaded JSON file. This generated json key would be used later for authentication purposes once we start accessing and storing to the GCS
- Reference: [https://cloud.google.com/docs/authentication/getting-started](https://cloud.google.com/docs/authentication/getting-started)
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" commandpython3 manage.py collectstatic发布于 2017-03-02 10:33:56
https://stackoverflow.com/questions/40127675
复制相似问题