【问题标题】:How do I integrate filepicker.io with google app engine (blobstore)?如何将 filepicker.io 与谷歌应用引擎(blobstore)集成?
【发布时间】:2023-03-15 20:25:01
【问题描述】:

所以我正在尝试将 filepicker.io 与 GAE 应用程序一起使用。文件选择器小部件返回用户上传的文件的 URL。

然后我如何使用这个 url 将文件上传到 GAE 的 blobstore?

blobstore 只支持“通过表单上传文件”,所以真正的问题是如何伪造包含文件 URL 的表单 POST。

【问题讨论】:

    标签: google-app-engine google-cloud-datastore blobstore webapp2 filepicker.io


    【解决方案1】:

    可以伪造表单帖子,但使用Files API 更简单

    编辑:

    要将 File API 与 urlfetch 一起使用,您可以编写如下内容:

    from __future__ import with_statement
    from google.appengine.api import files
    from google.appengine.api import urlfetch
    
    url = "http://www.facebook.com/somephoto.png"
    result = urlfetch.fetch(url)
    if result.status_code not 200:
      return "some error"
    
    # Create the file
    file_name = files.blobstore.create(mime_type='application/octet-stream')
    
    # Open the file and write to it
    with files.open(file_name, 'a') as f:
      f.write(result.content)
    
    # 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)
    

    我还没有测试过 - 所以如果这不起作用,请告诉我。

    另外,我相信您可以将 mime_type 保留为“application/octet-stream”,App Engine 会尝试猜测正确的类型。如果这不起作用,请尝试将其更改为“image/png”。或者对于 pdf,mime 类型将是 'application/pdf'

    【讨论】:

    • 哦,我在看那个,但是你将如何将 pdf 的内容写入 blob 对象?好像只支持文字。
    • 啊酷,它有效!但是文件正在下载(一旦你在 url 上查看它)没有扩展名,所以你不可能知道它是一个 .pdf。有没有附加文件扩展名的方法?
    • @jellyksong 在从 Blobstore 提供服务时是否要添加 .pdf?你能为此创建一个新问题吗?请包含您用于服务器文件的代码。
    【解决方案2】:

    投票最多的答案中的文件 API 已弃用,因此我在 github 上写了一个要点来解决这个问题。它获取图像的 url 并使用请求和海报将其发布到您自己的服务器。

    https://gist.github.com/jmasonherr/6301914

    【讨论】:

      【解决方案3】:

      我们实际上花了很多时间试图找出是否可以破解浏览器来伪造您描述的确切类型的东西,但无济于事。我的建议是编写一些快速的后端代码,比如 Kyle 建议从 url 中获取文件对象,但如果你真的坚持不懈,你实际上可以使用 ajax 伪造你自己的多部分表单请求。

      请参阅 XMLHttpRequest POST multipart/form-dataIs it possible to fake a multipart/form-data post with a jquery ajax call? 了解有关如何执行此操作的一些想法

      【讨论】:

        猜你喜欢
        • 2011-04-18
        • 2011-04-07
        • 2015-02-18
        • 2014-04-04
        • 2012-01-19
        • 1970-01-01
        • 2014-10-29
        • 1970-01-01
        • 2017-01-05
        相关资源
        最近更新 更多