• <small id='nhWnd'></small><noframes id='nhWnd'>

    1. <legend id='nhWnd'><style id='nhWnd'><dir id='nhWnd'><q id='nhWnd'></q></dir></style></legend>
      <i id='nhWnd'><tr id='nhWnd'><dt id='nhWnd'><q id='nhWnd'><span id='nhWnd'><b id='nhWnd'><form id='nhWnd'><ins id='nhWnd'></ins><ul id='nhWnd'></ul><sub id='nhWnd'></sub></form><legend id='nhWnd'></legend><bdo id='nhWnd'><pre id='nhWnd'><center id='nhWnd'></center></pre></bdo></b><th id='nhWnd'></th></span></q></dt></tr></i><div id='nhWnd'><tfoot id='nhWnd'></tfoot><dl id='nhWnd'><fieldset id='nhWnd'></fieldset></dl></div>

        <tfoot id='nhWnd'></tfoot>

        • <bdo id='nhWnd'></bdo><ul id='nhWnd'></ul>

        Google App Engine:如何将大文件写入 Google Cloud Storage

        Google App Engine: How to write large files to Google Cloud Storage(Google App Engine:如何将大文件写入 Google Cloud Storage)

                <bdo id='KtvT0'></bdo><ul id='KtvT0'></ul>

                  <legend id='KtvT0'><style id='KtvT0'><dir id='KtvT0'><q id='KtvT0'></q></dir></style></legend><tfoot id='KtvT0'></tfoot>
                  <i id='KtvT0'><tr id='KtvT0'><dt id='KtvT0'><q id='KtvT0'><span id='KtvT0'><b id='KtvT0'><form id='KtvT0'><ins id='KtvT0'></ins><ul id='KtvT0'></ul><sub id='KtvT0'></sub></form><legend id='KtvT0'></legend><bdo id='KtvT0'><pre id='KtvT0'><center id='KtvT0'></center></pre></bdo></b><th id='KtvT0'></th></span></q></dt></tr></i><div id='KtvT0'><tfoot id='KtvT0'></tfoot><dl id='KtvT0'><fieldset id='KtvT0'></fieldset></dl></div>

                  <small id='KtvT0'></small><noframes id='KtvT0'>

                    <tbody id='KtvT0'></tbody>
                • 本文介绍了Google App Engine:如何将大文件写入 Google Cloud Storage的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  I am trying to save large files from Google App Engine's Blobstore to Google Cloud Storage to facilitate backup.

                  It works fine for small files (<10 mb) but for larger files it get gets unstable and GAE throws and FileNotOpenedError.

                  My code:

                  PATH = '/gs/backupbucket/'
                  for df in DocumentFile.all():           
                    fn = df.blob.filename
                    br = blobstore.BlobReader(df.blob)
                    write_path = files.gs.create(self.PATH+fn.encode('utf-8'), mime_type='application/zip',acl='project-private') 
                    with files.open(write_path, 'a') as fp:
                      while True:
                        buf = br.read(100000)
                        if buf=="": break
                        fp.write(buf)
                    files.finalize(write_path)
                  

                  (Runs in a taskeque to avoid exceeding execution time).

                  Throws a FileNotOpenedError:

                  Traceback (most recent call last):
                    File "/base/python27_runtime/python27_lib/versions/third_party/webapp2-2.3/webapp2.py", line 1511, in __call__
                      rv = self.handle_exception(request, response, e)
                    File "/base/python27_runtime/python27_lib/versions/third_party/webapp2-2.3/webapp2.py", line 1505, in __call__
                      rv = self.router.dispatch(request, response)
                    File "/base/python27_runtime/python27_lib/versions/third_party/webapp2-2.3/webapp2.py", line 1253, in default_dispatcher
                      return route.handler_adapter(request, response)
                    File "/base/python27_runtime/python27_lib/versions/third_party/webapp2-2.3/webapp2.py", line 1077, in __call__
                      return handler.dispatch()
                    File "/base/python27_runtime/python27_lib/versions/third_party/webapp2-2.3/webapp2.py", line 547, in dispatch
                      return self.handle_exception(e, self.app.debug)
                    File "/base/python27_runtime/python27_lib/versions/third_party/webapp2-2.3/webapp2.py", line 545, in dispatch
                      return method(*args, **kwargs)
                    File "/base/data/home/apps/s~simplerepository/1.354754771592783168/processFiles.py", line 249, in post
                      fp.write(buf)
                    File "/base/python27_runtime/python27_lib/versions/1/google/appengine/api/files/file.py", line 281, in __exit__
                      self.close()
                    File "/base/python27_runtime/python27_lib/versions/1/google/appengine/api/files/file.py", line 275, in close
                      self._make_rpc_call_with_retry('Close', request, response)
                    File "/base/python27_runtime/python27_lib/versions/1/google/appengine/api/files/file.py", line 388, in _make_rpc_call_with_retry
                      _make_call(method, request, response)
                    File "/base/python27_runtime/python27_lib/versions/1/google/appengine/api/files/file.py", line 236, in _make_call
                      _raise_app_error(e)
                    File "/base/python27_runtime/python27_lib/versions/1/google/appengine/api/files/file.py", line 179, in _raise_app_error
                      raise FileNotOpenedError()
                  

                  I have investigated further and according to a comment to GAE Issue 5371 the Files API closes the file every 30 seconds. I have not seen this documented anywhere else.

                  I have tried to work around this by closing and opening the file at intervals but now I get an WrongOpenModeError. The code below is edited from the first version of this post I have added a 0.5 second pause between the close and the open of the file. It now throws a WrongOpenModeError.

                  My code (updated):

                  PATH = '/gs/backupbucket/'
                  for df in DocumentFile.all():           
                    fn = df.blob.filename
                    br = blobstore.BlobReader(df.blob)
                    write_path = files.gs.create(self.PATH+fn.encode('utf-8'), mime_type='application/zip',acl='project-private') 
                    fp = files.open(write_path, 'a')
                    c = 0
                    while True:       
                      if (c == 5):
                        c = 0
                        fp.close()
                        files.finalize(write_path)
                        time.sleep(0.5)
                        fp = files.open(write_path, 'a')
                      c = c + 1
                      buf = br.read(100000)
                      if buf=="": break
                      fp.write(buf)
                    files.finalize(write_path)
                  

                  Stacktrace:

                  Traceback (most recent call last):
                    File "/base/python27_runtime/python27_lib/versions/third_party/webapp2-2.3/webapp2.py", line 1511, in __call__
                      rv = self.handle_exception(request, response, e)
                    File "/base/python27_runtime/python27_lib/versions/third_party/webapp2-2.3/webapp2.py", line 1505, in __call__
                      rv = self.router.dispatch(request, response)
                    File "/base/python27_runtime/python27_lib/versions/third_party/webapp2-2.3/webapp2.py", line 1253, in default_dispatcher
                      return route.handler_adapter(request, response)
                    File "/base/python27_runtime/python27_lib/versions/third_party/webapp2-2.3/webapp2.py", line 1077, in __call__
                      return handler.dispatch()
                    File "/base/python27_runtime/python27_lib/versions/third_party/webapp2-2.3/webapp2.py", line 547, in dispatch
                      return self.handle_exception(e, self.app.debug)
                    File "/base/python27_runtime/python27_lib/versions/third_party/webapp2-2.3/webapp2.py", line 545, in dispatch
                      return method(*args, **kwargs)
                    File "/base/data/home/apps/s~simplerepository/1.354894420907462278/processFiles.py", line 267, in get
                      fp.write(buf)
                    File "/base/python27_runtime/python27_lib/versions/1/google/appengine/api/files/file.py", line 310, in write
                      self._make_rpc_call_with_retry('Append', request, response)
                    File "/base/python27_runtime/python27_lib/versions/1/google/appengine/api/files/file.py", line 388, in _make_rpc_call_with_retry
                      _make_call(method, request, response)
                    File "/base/python27_runtime/python27_lib/versions/1/google/appengine/api/files/file.py", line 236, in _make_call
                      _raise_app_error(e)
                    File "/base/python27_runtime/python27_lib/versions/1/google/appengine/api/files/file.py", line 188, in _raise_app_error
                      raise WrongOpenModeError()
                  

                  I have tried to find information about the WrongOpenModeError but the only place it is mentioned is in the appengine.api.files.file.py itself.

                  Suggestions on how to get around this and be able to save also large files to Google Cloud storage would be greatly appreciated. Thanks!

                  解决方案

                  I was having the same issue, endup writing an iterator around fetch data and catch the exception, works but is a work-around.

                  Re-writing your code would be something like:

                  from google.appengine.ext import blobstore
                  from google.appengine.api import files
                  
                  def iter_blobstore(blob, fetch_size=524288):
                    start_index = 0
                    end_index = fetch_size
                  
                    while True:
                      read = blobstore.fetch_data(blob, start_index, end_index)
                  
                      if read == "":
                        break
                  
                      start_index += fetch_size
                      end_index += fetch_size
                  
                      yield read
                  
                  
                  PATH = '/gs/backupbucket/'
                  for df in DocumentFile.all():           
                    fn = df.blob.filename
                    br = blobstore.BlobReader(df.blob)
                    write_path = files.gs.create(self.PATH+fn.encode('utf-8'), mime_type='application/zip',acl='project-private') 
                    with files.open(write_path, 'a') as fp:
                      for buf in iter_blobstore(df.blob):
                        try:
                          fp.write(buf)
                        except files.FileNotOpenedError:
                          pass
                    files.finalize(write_path)
                  

                  这篇关于Google App Engine:如何将大文件写入 Google Cloud Storage的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  What happens when you compare 2 pandas Series(当你比较 2 个 pandas 系列时会发生什么)
                  Quickly find differences between two large text files(快速查找两个大文本文件之间的差异)
                  Python - Compare 2 files and output differences(Python - 比较 2 个文件和输出差异)
                  Why do comparisions between very large float values fail in python?(为什么在 python 中非常大的浮点值之间的比较会失败?)
                  Dictionary merge by updating but not overwriting if value exists(字典通过更新合并,但如果值存在则不覆盖)
                  Find entries of one text file in another file in python(在python中的另一个文件中查找一个文本文件的条目)
                  <tfoot id='OYisL'></tfoot><legend id='OYisL'><style id='OYisL'><dir id='OYisL'><q id='OYisL'></q></dir></style></legend>
                  <i id='OYisL'><tr id='OYisL'><dt id='OYisL'><q id='OYisL'><span id='OYisL'><b id='OYisL'><form id='OYisL'><ins id='OYisL'></ins><ul id='OYisL'></ul><sub id='OYisL'></sub></form><legend id='OYisL'></legend><bdo id='OYisL'><pre id='OYisL'><center id='OYisL'></center></pre></bdo></b><th id='OYisL'></th></span></q></dt></tr></i><div id='OYisL'><tfoot id='OYisL'></tfoot><dl id='OYisL'><fieldset id='OYisL'></fieldset></dl></div>

                    <bdo id='OYisL'></bdo><ul id='OYisL'></ul>

                        <small id='OYisL'></small><noframes id='OYisL'>

                            <tbody id='OYisL'></tbody>