配置 Django 和谷歌云存储?

新手上路,请多包涵

没有 使用 Appengine。

我有一个在 VM 上运行的普通 Django 应用程序。我想使用 Google Cloud Storage 来提供我的静态文件,以及上传/提供我的媒体文件。

我有一个水桶。

如何将我的 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'  : <I have no idea>,
        'key'   : <ditto above>,
        'bucket': <my bucket name>,
    }
}

DEFAULT_LIBCLOUD_PROVIDER = 'google'
DEFAULT_FILE_STORAGE = 'storages.backends.apache_libcloud.LibCloudStorage'
STATICFILES_STORAGE = 'storages.backends.apache_libcloud.LibCloudStorage'

原文由 Nostalg.io 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 456
2 个回答

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

原文由 Alan Wagner 发布,翻译遵循 CC BY-SA 3.0 许可协议

事实上,Django-storages 是一个可行的替代方案。你必须小心它的谷歌云后端,尽管它提供的 url() 方法会导致对谷歌的不必要的 HTTP 调用。 (例如,Django 在呈现静态文件时调用 .url())。

https://github.com/jschneier/django-storages/issues/491

设置.py

     DEFAULT_FILE_STORAGE = 'config.storage_backends.GoogleCloudMediaStorage'
    STATICFILES_STORAGE = 'config.storage_backends.GoogleCloudStaticStorage'
    GS_PROJECT_ID = '<google-cloud-project-id>'
    GS_MEDIA_BUCKET_NAME = '<name-of-static-bucket>'
    GS_STATIC_BUCKET_NAME = '<name-of-static-bucket>'
    STATIC_URL = 'https://storage.googleapis.com/{}/'.format(GS_STATIC_BUCKET_NAME)
    MEDIA_URL = 'https://storage.googleapis.com/{}/'.format(GS_MEDIA_BUCKET_NAME)

存储后端.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

原文由 elnygren 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题