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

2023-11-23

我陷入困境..我使用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'))

我该如何修复它? 谢谢!


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

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

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

随机推荐

  • 使用 ant 编辑/追加数据到文本文件

    我正在尝试在 ant 构建过程中向 jad 文件添加属性 ant 中有任务可以做到这一点吗 我需要做的就是在文本文件的末尾添加一行文本 但我找不到执行此操作的任务 我相信这会起作用 现在测试一下
  • 检测 jQuery.ajaxComplete() 中是否存在 HTTP 方法(POST、GET)

    In a jQuery ajaxComplete 如何检测 HTTP 方法 特别是 GET 或 POST 我尝试阅读 jQuery 文档并四处搜索 但似乎找不到传递给内部函数处理程序的 3 个对象的太多文档 jQuery element a
  • 如何诊断或检测 Java 静态初始化器中的死锁

    在 Java 中使用静态初始化器是否是一个好主意超出了这个问题的范围 我在 Scala 应用程序中遇到了死锁 我认为这是由编译类中的互锁静态初始化器引起的 我的问题是如何检测和诊断这些死锁 我发现当涉及静态初始化程序块时 用于死锁的普通 J
  • 如何将 jQuery 自动完成组合框与 AJAX JSON 数据一起使用?

    我需要使用组合框执行以下操作 Select box有一个用户可以搜索的默认城市列表 如果用户在input框 我需要进行 ajax 调用来获取数据并向用户显示选项 如果根据用户的请求获取数据 则应将这些城市附加到以下选项中Select box
  • 如何关闭生产中的 Node.js Express(ejs 模板引擎)错误?

    当我在开发服务器上出现错误时 Express 会发送回溯作为响应 然而 这对于生产来说并不好 我不想让任何人看到我的引用 我怎样才能关闭它 注意 我使用 EJS 作为模板引擎 这可能是原因 而不是表达 例如 当我在 ejs 模板中有一个未定
  • 使用 Zip() 合并不同长度的数组而不丢失值

    在下面的代码中 我合并两个类型的数组int and string 第一个的长度大于第二个 因此最后一个索引 即5 不会被合并 int numbers new 1 2 3 4 5 string words new string one two
  • 媒体查询可以根据 div 元素而不是屏幕调整大小吗?

    我想使用媒体查询根据 a 的大小调整元素的大小div它们所在的元素 我不能使用屏幕尺寸作为div只是像网页中的小部件一样使用 其大小可能会有所不同 是的 CSS 容器查询正是您所寻找的 这CSS 遏制模块是详细说明此功能的规范 您可以检查浏
  • 如何修复滚动时折叠工具栏中的视图?

    我想用两个实现折叠工具栏EditText在其中 用于用户输入的目的 我正在跟进this回答 答案给出了添加两个的完美解决方案EditText进入折叠工具栏 但行为并不如预期 我所取得的成就 预期行为 我的 XML 代码
  • 使用express.js和node上传文件,限制扩展

    我正在使用express js 和node 处理文件上传 并且基本功能正常工作 我需要的是实施一些安全措施 即限制上传某些格式 PNG JPEG 有没有一种简单的方法只允许某些格式 它会进入正文解析器吗 app use express bo
  • R 中按部分对象名称过滤或子集列表

    我有一个包含 417 个数据框的列表 每个数据帧在列表中都有一个单独的名称 以 Dec 1981 开头并以 Aug 2016 结尾 这些对象按时间顺序排列 我想仅按月份名称来子集或过滤此列表 例如 创建一个仅包含 Jan 对象 数据框 的新
  • 内联块没有边距?

    我有几个 DIV 显示为内联块 他们似乎从浏览器中自动应用了间距 它们的边距 填充设置为 0 有没有办法在不使用负边距的情况下纠正这个问题 山姆 你看到的那个空间实际上是空白 这就是为什么删除填充和边距没有任何作用 让我解释 当你有这个时
  • 导入错误:没有名为“MySQL”的模块

    我已成功下载 Connector Python for MySQL 我在 Python 的 shell 中使用以下代码来测试我的连接 import mysql connector 我收到以下错误消息 Traceback most recen
  • 如何在<区域>上添加边框?

    有没有办法在周围设置边框 area 元素 我需要这样做来测试图像映射 但这不起作用 area outline 1px solid red border 1px solid red 如果您愿意使用 Javascript 请添加mouseove
  • 如何将日期时间转换为时间

    我正在选角DateTime字段到时间通过使用CAST Syntax select CAST time as time as CSTTime 约会时间2015 03 19 00 00 00 000 当前输出 时间03 05 36 000000
  • 是否可以在没有任何用户交互的情况下创建选择对象?

    Can a 选择无需任何用户交互即可创建对象 window getSelection 确实返回一个Selection反对 但你不能modify 除非用户做出某种选择 是否可以创建一个从页面上第一个元素开始的选择 然后能够modify 不需要
  • JSF 所需的 URL 重写解决方案

    假设以下应用场景 App server ear1 web1 ctx1 lt http localhost ctx1 xxx lt http www example com xxx
  • Symfony2:如何根据权限隐藏 Twig 中的链接

    我的应用程序显示了项目列表 项目详细信息页面以及用于编辑这些项目的表单 这些是路线 项目列表 project 42 查看项目 项目详细信息页面 project 42 edit 编辑项目 只有其所有者才能编辑项目 我已经实现了一个投票者来阻止
  • 为什么Scala中Array.map的定义是“throw new Error()”

    的源代码map for Array is override def map B f A gt B Array B throw new Error 但以下有效 val name Array String new Array 1 name 0
  • 如何使用nestjs/mongoose在模式类中定义mongoose方法?

    我想在模式类中实现方法 如下所示 import SchemaFactory Schema Prop from nestjs mongoose import Document from mongoose import bcrypt from
  • 如何使用实验性 API 将大文件写入 Blobstore?

    我陷入困境 我使用tipfy 作为框架在scribd 存储和blobstore 中上传文件 我的网络表单的操作不是由 blobstore create upload url 创建的 我只是使用 url for myhandler 我这样做是