用于google云存储的django文件存储后端
django-gcs的Python项目详细描述
Based on Google Cloud Storage JSON API.
Written using Google Cloud Python Client library.
快速启动
$ pip install django-gcs
更新settings.py
DJANGO_GCS={'bucket':'bucket-name','http':get_http_credentials}
设置
DJANGO_GCS={'bucket':None,'project':'','credentials':None,'http':None,'cache_control':['no-cache']}
- bucket谷歌云存储的存储桶名称。
- projectgoogle项目名(不需要)。
- credentialsoauth2凭据
- httphttplib2.http实例或可调用的,返回httplib2.http实例
- cache_control是字符串列表。默认情况下,['no-cache']
注意:应该为身份验证提供credentials或http之一。
使用量
全局设置存储:
DEFAULT_FILE_STORAGE='django_gcs.GoogleCloudStorage'
或每种型号:
fromdjango_gcsimportGoogleCloudStorageclassFileModel(models.Model):file=models.ImageField(storage=GoogleCloudStorage(bucket='some-bucket'))
示例如何生成要发出请求的http对象。
importpickleimporthttplib2defget_http_credentials():withopen('google/oauth2/credentials/file','r')asf:credentials=pickle.load(f)http_credentials=credentials.authorize(httplib2.Http())returnhttp_credentials