首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从Google云存储桶(用于GCE托管的Django应用程序)提供静态文件

从Google云存储桶(用于GCE托管的Django应用程序)提供静态文件
EN

Stack Overflow用户
提问于 2016-10-19 09:41:01
回答 2查看 8.2K关注 0票数 8

我试图为我的django应用程序提供来自云存储桶的静态文件,但不知道确切的过程。有谁能建议一个合适的方法吗?

我采取了以下步骤:

  1. 使用gsutil命令将所有静态文件上传到GoogleCloudStorage桶(www.example.com)上。
  2. 编辑的/etc/apache2/sites-available/default-ssl.conf文件。

文件内容:

代码语言:javascript
复制
<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文件:

代码语言:javascript
复制
# 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,)

对于这项任务需要做什么额外的修改,有什么建议吗?

谢谢,

EN

回答 2

Stack Overflow用户

发布于 2020-04-30 08:30:09

主要参考资料:

先决条件步骤

  1. 转到GCP:云存储(GCS)并单击CREATE (根据需要填充)
  2. 一旦创建,您可以公开它,如果您希望它作为您的网站的CDN (存储您的静态文件,如css,图像,视频等)。
代码语言:javascript
复制
- 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)

  1. 在settings.py中配置Django的静态文件设置
代码语言:javascript
复制
# 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/
  1. 如果您有访问其他静态文件的HTML文件或CSS文件,请确保它们引用具有此更新的STATIC_URL设置的其他静态文件。

在你的home.html里

代码语言:javascript
复制
{% load static %}
<link rel="stylesheet" type="text/css" href="{% static 'home/css/home.css' %}">

然后在你的home.css文件中

代码语言:javascript
复制
background-image: url("../assets/img/myHandsomeImage.jpg");

现在,home.css链接将转换为:

名称]/home/css/home.css

而myHandsomeImage.jpg则是:

[name]/home/assets/img/myHandomeImage.jpg

当然,如果您愿意,您可以只放置绝对路径(完整的URL),但是这种配置总是需要手动更新使用的URL,就像您切换到开发模式,并且只想在本地访问静态文件而不是GCS。

  1. 跑到下面去。这将将STATICFILES_DIRS中每个目录中的所有文件复制到STATIC_ROOT目录中。
代码语言:javascript
复制
python3 manage.py collectstatic

# or if your STATIC_ROOT folder requires permissions to write to it then:
# sudo python3 manage.py collectstatic
  1. 转到STATIC_ROOT文件夹并将其内容上载到GCS。或者通过GCS GUI控制台或者通过Google提供的工具"gsutil“和rsync一起手动上传它们。
  2. 现在,GCS已经包含了静态文件,您的Django项目配置为通过配置的STATIC_URL直接访问这些文件。

方法2(更长,但不需要手动复制)

  1. 可选步骤准备python虚拟环境
代码语言:javascript
复制
python3 -m venv path/to/the/target/location/for/the/virtual/environment
source path/to/the/target/location/for/the/virtual/environment/bin/activate
  1. 安装必要的软件包,以便能够直接访问和存储到GCS。
代码语言:javascript
复制
pip3 install django-storages # https://pypi.org/project/django-storages/
pip3 install google-cloud-storage # https://pypi.org/project/google-cloud-storage/
  1. 如果您在Google基础设施之外的计算机上,请执行强制步骤,转到GCP: IAM、服务帐户,然后单击CREATE服务帐户
代码语言:javascript
复制
- 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)

  1. 在settings.py中配置Django的静态文件设置
代码语言:javascript
复制
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
  1. 跑到下面去。这将直接将STATICFILES_DIRS中每个目录中的所有文件复制到GCS桶中。这可能需要一段时间。
代码语言:javascript
复制
python3 manage.py collectstatic
  1. 现在,GCS已经包含了静态文件,您的Django项目配置为通过配置的STATIC_URL直接访问这些文件。
票数 19
EN

Stack Overflow用户

发布于 2017-03-02 10:33:56

基本上你需要:

  1. 创建云存储桶并将其设置为公共可读的。
  2. 收集本地静态文件
  3. 将文件复制到云存储中
  4. 设置STATIC_URL

检查步骤1-4 发动机

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40127675

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档