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

发布于 2024-10-31 20:09:37 字数 3249 浏览 0 评论 0原文

我陷入困境..我使用tipfy 作为框架在scribd 存储和blobstore 中上传文件。 我的网络表单的操作不是由 blobstore.create_upload_url 创建的(我只是使用 url_for('myhandler'))。我这样做是因为如果我使用 blobstore 处理程序解析 POST 响应,并且我无法使用普通的 python-scribd api 将文件上传到 scribd 存储中。 现在我有工作的 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 中。但是,如果我上传大文件(即 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'))

如何修复它? 谢谢!

I have dilemma.. I'm uploading files both in scribd store and blobstore using tipfy as framework.
I have webform with action is not created by blobstore.create_upload_url (i'm just using url_for('myhandler')). I did it because if i'm using blobstore handler the POST response parsed and I cannot use normal python-scribd api to upload file into scribd store.
Now I have working 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')

As you can see it also saves file into blobstore.. But If i'm uploading big file (i.e. 5Mb) I'm receiving

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

How can I fix it?
Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我偏爱纯白色 2024-11-07 20:09:37

您需要对文件 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 上传机制。

You need to make multiple, smaller calls to the file API, for instance like this:

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

Note that the payload size limit on regular requests to App Engine apps is 10MB; if you want to upload larger files, you'll need to use the regular blobstore upload mechanism.

温柔戏命师 2024-11-07 20:09:37

最后我找到了解决方案。

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

因为string没有read()方法,我就按照他写的那样拼接文件string然后写。

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

finally i found solution.

Nick Johneson's answer occurred attribute error because uploaded_file is treated as string.
string didn't have read() method.

Cause string doesn't have method read(), i spliced file string and write it just like he wrote.

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)

you can check about splitCount(). here

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文