如何将Google云端存储与django-storage一起使用

时间:2018-05-18 05:10:19

标签: django google-cloud-storage

使用App Engine。 我看了这个页面。 Configure Django and Google Cloud Storage?我安装了django-storages和boto。我像这样设置我的设置文件。

DEFAULT_FILE_STORAGE = 'storages.backends.gs.GSBotoStorage'
GS_ACCESS_KEY_ID = '***@***.iam.gserviceaccount.com'
GS_SECRET_ACCESS_KEY = '***'
GS_BUCKET_NAME = '***'
STATICFILES_STORAGE = 'storages.backends.gs.GSBotoStorage'

然后我运行此代码

from django.core.files.storage import default_storage
default_storage.exists('storage_test')

然后我收到了这个错误。

---------------------------------------------------------------------------
GSResponseError                           Traceback (most recent call last)
<ipython-input-2-8a49776d7c1d> in <module>()
      1 from django.core.files.storage import default_storage
----> 2 default_storage.exists('storage_test')

/Users/trmt_8/.pyenv/versions/3.5.0/lib/python3.5/site-packages/storages/backends/s3boto.py in exists(self, name)
    433                 return False
    434 
--> 435         return self._get_key(name) is not None
    436 
    437     def listdir(self, name):

/Users/trmt_8/.pyenv/versions/3.5.0/lib/python3.5/site-packages/storages/backends/s3boto.py in _get_key(self, name)
    419         if self.entries:
    420             return self.entries.get(name)
--> 421         return self.bucket.get_key(self._encode_name(name))
    422 
    423     def delete(self, name):

/Users/trmt_8/.pyenv/versions/3.5.0/lib/python3.5/site-packages/boto/gs/bucket.py in get_key(self, key_name, headers, version_id, response_headers, generation)
    109         try:
    110             key, resp = self._get_key_internal(key_name, headers,
--> 111                                                query_args_l=query_args_l)
    112         except GSResponseError as e:
    113             if e.status == 403 and 'Forbidden' in e.reason:

/Users/trmt_8/.pyenv/versions/3.5.0/lib/python3.5/site-packages/boto/s3/bucket.py in _get_key_internal(self, key_name, headers, query_args_l)
    230             else:
    231                 raise self.connection.provider.storage_response_error(
--> 232                     response.status, response.reason, '')
    233 
    234     def list(self, prefix='', delimiter='', marker='', headers=None,

GSResponseError: GSResponseError: 403 Access denied to 'gs://***/storage_test'.

我该如何解决这个问题。请帮助我!

这是django-storages

的文件

1 个答案:

答案 0 :(得分:1)

您可能想要设置名为GOOGLE_APPLICATION_CREDENTIALS的环境变量。您应该可以从gcloud控制台下载它(.json文件)。

有关详细信息,请参阅documention