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

11

我有一个困境.. 我正在使用tipfy框架同时将文件上传到scribd存储和blobstore中。我有一个Web表单,其操作没有由blobstore.create_upload_url创建(我只是使用url_for('myhandler'))。我这样做是因为如果我使用blobstore处理程序,POST响应会被解析,并且我无法使用普通的python-scribd api将文件上传到scribd存储中。

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中。但是,如果我上传大文件(即5 MB),则会收到

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'))

我该如何修复它? 谢谢!


你的问题及其答案对我帮助很大,感谢! - selurvedu
2个回答

7
你需要进行多个较小的文件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上传机制。

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

6

最终我找到了解决方法。

Nick Johneson的答案出现属性错误,因为上传文件被视为字符串。 字符串没有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


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接