问题 如何使用实验API将大文件写入Blobstore?


我陷入困境......我使用tipfy作为框架在scribd商店和blobstore上传文件。 我的webform with action不是由blobstore.create_upload_url创建的(我只是使用url_for('myhandler'))。我这样做是因为如果我使用blobstore处理程序解析了POST响应,我就不能使用普通的python-scribd api将文件上传到scribd store。 现在我有工作的scribd saver:

class UploadScribdHandler(RequestHandler, BlobstoreUploadMixin):
    def post(self):
        uploaded_file = self.request.files.get('upload_file')
        fname = uploaded_file.filename.strip()
        try:
            self.post_to_scribd(uploaded_file, fname)
        except Exception, e:
            # ... get the exception message and do something with it
            msg = e.message
            # ...
        # reset the stream to zero (beginning) so the file can be read again
        uploaded_file.seek(0)
        #removed try-except to see debug info in browser window
        # Create the file

        file_name = files.blobstore.create(_blobinfo_uploaded_filename=fname)
        # Open the file and write to it
        with files.open(file_name, 'a') as f:
            f.write(uploaded_file.read())
        # Finalize the file. Do this before attempting to read it.      
        files.finalize(file_name)
        # Get the file's blob key
        blob_key = files.blobstore.get_blob_key(file_name)

        return Response('done')

    def post_to_scribd(self, uploaded_file, fname):
        errmsg =''
        uploaded_file = self.request.files.get('upload_file')
        fname = uploaded_file.filename.strip()
        fext = fname[fname.rfind('.')+1:].lower()
        if (fext not in ALLOWED_EXTENSION):
            raise Exception('This file type does not allowed to be uploaded\n')
        if SCRIBD_ENABLED:
            doc_title = self.request.form.get('title')
            doc_description = self.request.form.get('description')
            doc_tags = self.request.form.get('tags')
            try:
                document = scribd.api_user.upload(uploaded_file, fname, access='private')
                #while document.get_conversion_status() != 'DONE':
                #   time.sleep(2)
                if not doc_title:
                    document.title = fname[:fname.rfind('.')]
                else:
                    document.title = doc_title
                if not doc_description:
                    document.description = 'This document was uploaded at ' + str(datetime.datetime.now()) +'\n'
                else:
                    document.description = doc_description
                document.tags = doc_tags
                document.save()
            except scribd.ResponseError, err:
                raise Exception('Scribd failed: error code:%d, error message: %s\n' % (err.errno, err.strerror))
            except scribd.NotReadyError, err:
                raise Exception('Scribd failed: error code:%d, error message: %s\n' % (err.errno, err.strerror))
            except:
                raise Exception('something wrong exception')

正如你所看到它也将文件保存到blobstore ..但如果我上传大文件(即5Mb)我收到

RequestTooLargeError: The request to API call file.Append() was too large.
Request: docs.upload(access='private', doc_type='pdf', file=('PK\x03\x04\n\x00\x00\x00\x00\x00"\x01\x10=\x00\x00(...)', 'test.pdf'))

我该如何解决? 谢谢!


12455
2018-04-12 16:56


起源

你的问题及其答案对我帮助很大,欢呼! - selurvedu


答案:


您需要对文件API进行多次较小的调用,例如:

with files.open(file_name, 'a') as f:
    data = uploaded_file.read(65536)
    while data:
      f.write(data)
      data = uploaded_file.read(65536)

请注意,对App Engine应用程序的常规请求的有效负载大小限制为10MB;如果要上传较大的文件,则需要使用常规的blobstore上传机制。


7
2018-04-13 01:45



使用您的示例代码,您能想到它为什么会产生AttributeError - 'InMemoryUploadedFile'对象没有属性'eof'? (在你的例子的第二行) - ductionist
@bfox大概是因为它没有那个属性。我会用另一种方法更新我的答案。 - Nick Johnson
@minus你有没有想出一个解决方法呢?尝试将3-4 MB文件上传到blobstore时,我遇到了同样的问题。 - Matt Rajca
@Matt我发布的解决方案有什么问题?为什么不直接使用blobstore上传来上传? - Nick Johnson


最后我找到了解决方案

Nick Johneson的回答发生了属性错误,因为uploaded_file被视为字符串。 string没有read()方法。

原因字符串没有方法read(),我拼接文件字符串,就像他写的那样写。

class UploadRankingHandler(webapp.RequestHandler):
  def post(self):
    fish_image_file = self.request.get('file')

    file_name = files.blobstore.create(mime_type='image/png', _blobinfo_uploaded_filename="testfilename.png")

    file_str_list = splitCount(fish_image_file,65520)

    with files.open(file_name, 'a') as f:
      for line in file_str_list:
        f.write(line)

你可以检查一下splitCount()。这里

http://www.bdhwan.com/entry/gaewritebigfile


6
2017-08-18 11:31