如何在GAE上的Django中将django-google-cloud-storage用于GCS [英] How to use django-google-cloud-storage for GCS in Django on GAE

查看:140
本文介绍了如何在GAE上的Django中将django-google-cloud-storage用于GCS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将Django Custom Storage后端与Google Cloud Storage一起使用?

尝试使用此方法: ckopanos的django-google-cloud-storage

根据文档,我已在"settings.py"中添加了以下内容:

GOOGLE_CLOUD_STORAGE_BUCKET = [my bucket name] # the name of the bucket you have created from the google cloud storage console
GOOGLE_CLOUD_STORAGE_URL = [my bucket URL] #whatever the url for accessing your cloud storgage bucket
GOOGLE_CLOUD_STORAGE_DEFAULT_CACHE_CONTROL = [cache control] # default cache control headers for your files

DEFAULT_FILE_STORAGE = 'google.storage.googleCloud.GoogleCloudStorage'

我将'google'文件夹/软件包添加到了我的项目文件中,因此它对于'settings.py'应该是可以访问的

我现在如何在视图中使用此自定义存储系统?

我可以通过执行以下操作手动使用gcs(本示例在gcs上存储了xlsx):

import cloudstorage as gcs
from google.appengine.api import app_identity
import xlsxwriter
import StringIO

bucket_name = os.environ.get('BUCKET_NAME',
                           app_identity.get_default_gcs_bucket_name())
bucket = '/' + bucket_name

filename = bucket + '/'+ userid + '/' + [some code] + '.xlsx' 
gcs_file = gcs.open(filename,
            'w',
            content_type='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
            options={'x-goog-meta-user': userid})

output = StringIO.StringIO()
workbook = xlsxwriter.Workbook(output, {'in_memory': True})
worksheet = workbook.add_worksheet()

worksheet.write(0, 0, 'Hello World')

# Close the workbook before streaming the data.
workbook.close()

# Rewind the buffer.
output.seek(0)            

gcs_file.write(output.read())
gcs_file.close()

如何导入存储系统以在视图中使用?您会为Django和GCS推荐一个不同的存储系统吗?

解决方案

就像在Django中使用任何其他文件存储系统一样,因为Django中的自定义存储后端是通过定义必须为<django.core.files.storage.Storage 的子类的href ="https://docs.djangoproject.com/en/1.10/howto/custom-file-storage/" rel ="nofollow noreferrer">子类.

django-cloud-storage 是自定义存储后端,它允许您可以在Django中使用GCS,就像您在使用任何其他存储后端一样.

DEFAULT_FILE_STORAGE = 'google.storage.googleCloud.GoogleCloudStorage'

settings.py中的上述行使GCS成为默认的文件存储系统. 这意味着,如果在models中定义新的FileField,它将使用Google Cloud Storage.

您可以在视图中使用它,如下所示:

from django.core.files.storage import default_storage
file = default_storage.open(file_name, 'w')
file.write(output.read())
file.close()

文件将保存到您的默认存储桶中.

How do I use a Django Custom Storage backend with Google Cloud Storage?

Trying to use this: ckopanos's django-google-cloud-storage

As per the documentation I have added the following to my 'settings.py':

GOOGLE_CLOUD_STORAGE_BUCKET = [my bucket name] # the name of the bucket you have created from the google cloud storage console
GOOGLE_CLOUD_STORAGE_URL = [my bucket URL] #whatever the url for accessing your cloud storgage bucket
GOOGLE_CLOUD_STORAGE_DEFAULT_CACHE_CONTROL = [cache control] # default cache control headers for your files

DEFAULT_FILE_STORAGE = 'google.storage.googleCloud.GoogleCloudStorage'

And I added the 'google' folder/package to my project files so it should be accesible for 'settings.py'

How do I use this Custom Storage System in my views now?

I can manually use gcs by doing the following (this example stores an xlsx on gcs):

import cloudstorage as gcs
from google.appengine.api import app_identity
import xlsxwriter
import StringIO

bucket_name = os.environ.get('BUCKET_NAME',
                           app_identity.get_default_gcs_bucket_name())
bucket = '/' + bucket_name

filename = bucket + '/'+ userid + '/' + [some code] + '.xlsx' 
gcs_file = gcs.open(filename,
            'w',
            content_type='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
            options={'x-goog-meta-user': userid})

output = StringIO.StringIO()
workbook = xlsxwriter.Workbook(output, {'in_memory': True})
worksheet = workbook.add_worksheet()

worksheet.write(0, 0, 'Hello World')

# Close the workbook before streaming the data.
workbook.close()

# Rewind the buffer.
output.seek(0)            

gcs_file.write(output.read())
gcs_file.close()

How do I import the storage system for use in my views? Would you reccomend a different storage system for Django and GCS instead?

解决方案

Like the way you would use any other File Storage System in Django, because custom storage backends in Django are built by defining a custom storage class which must be a subclass of django.core.files.storage.Storage.

django-cloud-storage , is a custom storage backend which allows you to use GCS in Django, just like you would if you were using any other storage backend.

DEFAULT_FILE_STORAGE = 'google.storage.googleCloud.GoogleCloudStorage'

The above line in your settings.py makes GCS your default File storage system. That means if you define a new FileField in your models, it will use Google Cloud Storage.

You can use it in your views like this:

from django.core.files.storage import default_storage
file = default_storage.open(file_name, 'w')
file.write(output.read())
file.close()

The file will be saved to your default bucket.

这篇关于如何在GAE上的Django中将django-google-cloud-storage用于GCS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆