当前位置: 首页 > 知识库问答 >
问题:

在App Engine上从Django上传到Google云存储

党权
2023-03-14

我在Google App Engine上使用Django 1.5,并尝试将文件上传到Google Cloud Storage。我正在使用gcs库,并编写了一个自定义文件上传处理程序,我已在settings.py注册为唯一的文件上传器。我可以在开发环境中的blostore查看器中看到我的文件被正确上传,但一旦在views.py中调用form.save(),我就会抛出异常,说它是只读文件系统?我知道Google App Engine不允许访问文件系统,这就是我首先使用GCS的原因!

我是否需要做些什么来阻止Django尝试将文件保存到磁盘?

相关代码附在本要点中。

谢谢:)

堆栈跟踪:

Environment:


Request Method: POST
Request URL: http://localhost:8080/cms/media/add

Django Version: 1.5
Python Version: 2.7.5
Installed Applications:
('django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.staticfiles',
 'django.contrib.messages',
 'api',
 'cms',
 'frontend')
Installed Middleware:
('django.middleware.common.CommonMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware')


Traceback:
File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/django-1.5/django/core/handlers/base.py" in get_response
  115.                         response = callback(request, *callback_args, **callback_kwargs)
File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/django-1.5/django/contrib/auth/decorators.py" in _wrapped_view
  25.                 return view_func(request, *args, **kwargs)
File "/Users/james/Dropbox/University/Year 4/Advanced Development/assignment/cms/views.py" in media_add_or_edit
  44.             form.save()
File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/django-1.5/django/forms/models.py" in save
  370.                              fail_message, commit, construct=False)
File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/django-1.5/django/forms/models.py" in save_instance
  87.         instance.save()
File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/django-1.5/django/db/models/base.py" in save
  546.                        force_update=force_update, update_fields=update_fields)
File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/django-1.5/django/db/models/base.py" in save_base
  650.                 result = manager._insert([self], fields=fields, return_id=update_pk, using=using, raw=raw)
File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/django-1.5/django/db/models/manager.py" in _insert
  215.         return insert_query(self.model, objs, fields, **kwargs)
File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/django-1.5/django/db/models/query.py" in insert_query
  1673.     return query.get_compiler(using=using).execute_sql(return_id)
File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/django-1.5/django/db/models/sql/compiler.py" in execute_sql
  936.         for sql, params in self.as_sql():
File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/django-1.5/django/db/models/sql/compiler.py" in as_sql
  894.                 for obj in self.query.objs
File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/django-1.5/django/db/models/fields/files.py" in pre_save
  250.             file.save(file.name, file, save=False)
File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/django-1.5/django/db/models/fields/files.py" in save
  86.         self.name = self.storage.save(name, content)
File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/django-1.5/django/core/files/storage.py" in save
  48.         name = self._save(name, content)
File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/django-1.5/django/core/files/storage.py" in _save
  198.                     fd = os.open(full_path, flags, 0o666)
File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/tools/devappserver2/python/stubs.py" in fake_open
  71.     raise OSError(errno.EROFS, 'Read-only file system', filename)

Exception Type: OSError at /cms/media/add
Exception Value: [Errno 30] Read-only file system: u'/Users/james/Dropbox/University/Year 4/Advanced Development/assignment/IMG_0746.jpg'

共有1个答案

从建明
2023-03-14

我最终解决了这个问题,将文件字段从我的模型移动到我的模型表单中,如下所示:

# Used for uploading media that forms part of a story
class Media(models.Model):
    title = models.CharField(max_length=100)
    type = models.CharField(max_length=5, choices=MEDIA_TYPES)
    content = models.TextField()
    date_created = models.DateTimeField(auto_now_add=True)

# Used to convert the media model to a form in the cms
class MediaForm(forms.ModelForm):
    file = forms.FileField()

    class Meta:
        model = Media
        # Don't show the date created field because we want that to be set automatically
        exclude = ('date_created', 'content',)

我相信我以前试过,但它似乎已经解决了我的问题,希望这可能会帮助其他遇到同样问题的人。

 类似资料:
  • 我有一个firebase帐户,我想以编程方式上传一些照片到firebase存储。为此,我使用了google cloud java storage API。我配置权限和其他权限。我可以成功地创建桶,斑点。然而,当我试图上传一张图片时,要么它的大小为0,要么它无法下载(不生成URL)。下面是我使用的代码; 任何建议都很感激。问候。

  • 我试图链接PHP文件在谷歌云存储,但得到他们作为纯文本。我想连接到其他文件,但没有应用程序引擎,这是专门为应用程序引擎写的所有留档,甚至找不到桶存储是否有PHP支持,即使它应该。任何线索如何链接PHP文件正确的云存储?

  • 我正试着上传一张照片,在被croppie剪切到GCS之后。gcs write(imageToUpload)方法失败,因为imageToUpload当前不是文件,而是Blob。有没有人知道一个办法让这件事成功?可能是python中一种将BLOB转换为文件的方法吗?可以从Croppie返回的图像类型有:1)画布,2)HTML,3)BLOB和4)Base64这里是文档(Ctrl-F“result”)。任

  • Firebase Admin SDK文档清楚地说明,服务帐户对存储帐户具有完全的范围控制,但由于传输令牌源为nil,因此正确引导凭据似乎有些错误。

  • 我在本地开发服务器上运行代码,但使用在项目上配置的GCS默认桶名。 Python 2.7版