我没有使用Appengine.
我在VM上运行了一个简单的vanilla Django应用程序.我想使用Google云端存储来提供我的静态文件,以及上传/提供我的媒体文件.
我有一个水桶.
如何将Django应用程序链接到我的存储桶?我试过了django-storages
.这可能有用,但是我需要做些什么来准备我的django应用程序使用我的存储桶?我的Django设置需要什么基线配置?
当前设置:
# Google Cloud Storage # http://django-storages.readthedocs.org/en/latest/backends/apache_libcloud.html LIBCLOUD_PROVIDERS = { 'google': { 'type' : 'libcloud.storage.types.Provider.GOOGLE_STORAGE', 'user' : , 'key' :, 'bucket': , } } DEFAULT_LIBCLOUD_PROVIDER = 'google' DEFAULT_FILE_STORAGE = 'storages.backends.apache_libcloud.LibCloudStorage' STATICFILES_STORAGE = 'storages.backends.apache_libcloud.LibCloudStorage'
小智.. 11
Django-storages有一个谷歌云存储的后端,但它没有记录,我意识到在回购中.得到它使用此设置:
DEFAULT_FILE_STORAGE = 'storages.backends.gs.GSBotoStorage' GS_ACCESS_KEY_ID = 'YourID' GS_SECRET_ACCESS_KEY = 'YourKEY' GS_BUCKET_NAME = 'YourBucket' STATICFILES_STORAGE = 'storages.backends.gs.GSBotoStorage'
要获取YourKEY和YourID,您应该Interoperability
在设置选项卡中创建密钥.
希望它有所帮助,你不必学习它的方式:)
啊,如果你还没有,依赖是:
pip install django-storages pip install boto
elnygren.. 11
事实上,Django存储是一种可行的替代方案.您必须小心使用它的Google Cloud后端,因为url()
它提供的方法会导致对Google进行不必要的HTTP调用.(例如,Django在渲染静态文件时调用.url()).
https://github.com/jschneier/django-storages/issues/491
settings.py
DEFAULT_FILE_STORAGE = 'config.storage_backends.GoogleCloudMediaStorage'
STATICFILES_STORAGE = 'config.storage_backends.GoogleCloudStaticStorage'
GS_PROJECT_ID = ''
GS_MEDIA_BUCKET_NAME = ''
GS_STATIC_BUCKET_NAME = ''
STATIC_URL = 'https://storage.googleapis.com/{}/'.format(GS_STATIC_BUCKET_NAME)
MEDIA_URL = 'https://storage.googleapis.com/{}/'.format(GS_MEDIA_BUCKET_NAME)
storage_backends.py
"""
GoogleCloudStorage extensions suitable for handing Django's
Static and Media files.
Requires following settings:
MEDIA_URL, GS_MEDIA_BUCKET_NAME
STATIC_URL, GS_STATIC_BUCKET_NAME
In addition to
https://django-storages.readthedocs.io/en/latest/backends/gcloud.html
"""
from django.conf import settings
from storages.backends.gcloud import GoogleCloudStorage
from storages.utils import setting
from urllib.parse import urljoin
class GoogleCloudMediaStorage(GoogleCloudStorage):
"""GoogleCloudStorage suitable for Django's Media files."""
def __init__(self, *args, **kwargs):
if not settings.MEDIA_URL:
raise Exception('MEDIA_URL has not been configured')
kwargs['bucket_name'] = setting('GS_MEDIA_BUCKET_NAME', strict=True)
super(GoogleCloudMediaStorage, self).__init__(*args, **kwargs)
def url(self, name):
""".url that doesn't call Google."""
return urljoin(settings.MEDIA_URL, name)
class GoogleCloudStaticStorage(GoogleCloudStorage):
"""GoogleCloudStorage suitable for Django's Static files"""
def __init__(self, *args, **kwargs):
if not settings.STATIC_URL:
raise Exception('STATIC_URL has not been configured')
kwargs['bucket_name'] = setting('GS_STATIC_BUCKET_NAME', strict=True)
super(GoogleCloudStaticStorage, self).__init__(*args, **kwargs)
def url(self, name):
""".url that doesn't call Google."""
return urljoin(settings.STATIC_URL, name)
注意:默认情况下,身份验证是通过GOOGLE_APPLICATION_CREDENTIALS环境变量处理的.
https://cloud.google.com/docs/authentication/production#setting_the_environment_variable
Django-storages有一个谷歌云存储的后端,但它没有记录,我意识到在回购中.得到它使用此设置:
DEFAULT_FILE_STORAGE = 'storages.backends.gs.GSBotoStorage' GS_ACCESS_KEY_ID = 'YourID' GS_SECRET_ACCESS_KEY = 'YourKEY' GS_BUCKET_NAME = 'YourBucket' STATICFILES_STORAGE = 'storages.backends.gs.GSBotoStorage'
要获取YourKEY和YourID,您应该Interoperability
在设置选项卡中创建密钥.
希望它有所帮助,你不必学习它的方式:)
啊,如果你还没有,依赖是:
pip install django-storages pip install boto
事实上,Django存储是一种可行的替代方案.您必须小心使用它的Google Cloud后端,因为url()
它提供的方法会导致对Google进行不必要的HTTP调用.(例如,Django在渲染静态文件时调用.url()).
https://github.com/jschneier/django-storages/issues/491
settings.py
DEFAULT_FILE_STORAGE = 'config.storage_backends.GoogleCloudMediaStorage'
STATICFILES_STORAGE = 'config.storage_backends.GoogleCloudStaticStorage'
GS_PROJECT_ID = ''
GS_MEDIA_BUCKET_NAME = ''
GS_STATIC_BUCKET_NAME = ''
STATIC_URL = 'https://storage.googleapis.com/{}/'.format(GS_STATIC_BUCKET_NAME)
MEDIA_URL = 'https://storage.googleapis.com/{}/'.format(GS_MEDIA_BUCKET_NAME)
storage_backends.py
"""
GoogleCloudStorage extensions suitable for handing Django's
Static and Media files.
Requires following settings:
MEDIA_URL, GS_MEDIA_BUCKET_NAME
STATIC_URL, GS_STATIC_BUCKET_NAME
In addition to
https://django-storages.readthedocs.io/en/latest/backends/gcloud.html
"""
from django.conf import settings
from storages.backends.gcloud import GoogleCloudStorage
from storages.utils import setting
from urllib.parse import urljoin
class GoogleCloudMediaStorage(GoogleCloudStorage):
"""GoogleCloudStorage suitable for Django's Media files."""
def __init__(self, *args, **kwargs):
if not settings.MEDIA_URL:
raise Exception('MEDIA_URL has not been configured')
kwargs['bucket_name'] = setting('GS_MEDIA_BUCKET_NAME', strict=True)
super(GoogleCloudMediaStorage, self).__init__(*args, **kwargs)
def url(self, name):
""".url that doesn't call Google."""
return urljoin(settings.MEDIA_URL, name)
class GoogleCloudStaticStorage(GoogleCloudStorage):
"""GoogleCloudStorage suitable for Django's Static files"""
def __init__(self, *args, **kwargs):
if not settings.STATIC_URL:
raise Exception('STATIC_URL has not been configured')
kwargs['bucket_name'] = setting('GS_STATIC_BUCKET_NAME', strict=True)
super(GoogleCloudStaticStorage, self).__init__(*args, **kwargs)
def url(self, name):
""".url that doesn't call Google."""
return urljoin(settings.STATIC_URL, name)
注意:默认情况下,身份验证是通过GOOGLE_APPLICATION_CREDENTIALS环境变量处理的.
https://cloud.google.com/docs/authentication/production#setting_the_environment_variable
因此,这基本上可以工作。(具有此库和设置)。
使它起作用的诀窍是知道从何处获取libcloud 的'user'
和'key'
参数。
在Google上Cloud Console > Storage
,单击Settings
。然后单击右侧的选项卡Interoperability
。在该面板上,是一个单独的按钮,上面写着Enable Interoperability
。点击它。
瞧!您现在有了用户名和密钥。
注意:不要不使用django-storages
一封来自PyPI。它尚未更新,并且不适用于最新版本的Django。
使用此版本:
pip install -e 'git+https://github.com/jschneier/django-storages.git#egg=django-storages'
编辑:如果您想使用反向代理,那么您可以考虑我的稍微修改的版本。 https://github.com/jschneier/django-storages/compare/master...halfnibble:master
说明: 在某些情况下,可能有必要使用反向代理加载文件。这可以用来减轻跨域请求错误。
这个小的PR允许开发人员在settings.py中设置可选的LIBCLOUD_PROXY_URL。
用法示例
# Apache VirtualHost conf ProxyPass /foo http://storage.googleapis.com ProxyPassReverse /foo http://storage.googleapis.com # settings.py LIBCLOUD_PROXY_URL = '/foo/'