【问题标题】:Handling a blobstore blob as a file (python)将 blobstore blob 作为文件处理 (python)
【发布时间】:2013-01-09 10:32:49
【问题描述】:

我想使用 GAE 创建一个进程,通过该进程,给定一个 url,一个文件被下载并作为 blob 存储在 blobstore 中。完成此操作后,我想将此 blob 作为 POST 数据传递到第二个 url。但是,要使第二部分工作,我需要能够将 blob 作为文件实例打开。

我已经知道如何做第一部分了

from __future__ import with_statement
from google.appengine.api import files

imagefile = urllib2.urlopen('fileurl')
# Create the file
file_name = files.blobstore.create(mime_type=imagefile.headers['Content-Type'])
# Open the file and write to it
with files.open(file_name, 'ab') as f:
    f.write(imagefile.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)

但我不知道如何做第二部分。到目前为止我已经尝试过

  1. ffile = files.open(files.blobstore.get_file_name(blob_key), 'r')

  2. from google.appengine.ext import blobstore

    ffile = blobstore.BlobReader(blob_key)
    
  3. from google.appengine.ext import blobstore

    ffile = blobstore.BlobInfo.open(blobstore.BlobInfo(blob_key))
    

所有这些都给Falsefor isinstance(ffile, file)

感谢任何帮助。

【问题讨论】:

标签: python google-app-engine blobstore


【解决方案1】:

ffile = blobstore.BlobReader(blob_key) 有效。但是,返回的对象只有一个 file-like 接口;它不扩展文件类。因此,isinstance 测试不起作用。试试if ffile and "read" in dir( ffile )

【讨论】:

  • 谢谢。这可能意味着阻止我传递文件的错误正在下游发生。
【解决方案2】:

从 blobstore 中读取 file_data :

blob_key = .....                                        # is what you have
file_name = blobstore.BlobInfo.get(blob_key).filename   # the name of the file (image) to send 
blob_reader = blobstore.BlobReader(blob_key)
file_data = blob_reader.read()                          # and the file data with the image

但您也可以发送带有 blob_key 的 url 并提供该 url。对于图像,您不必自己提供图像,因为您可以发布 get_serving_url,利用具有动态缩放功能的 Google 高性能图像服务 API。以这种方式提供图像也非常便宜。

以下是此类网址的示例:

https://lh6.ggpht.com/lOghqU2JrYk8M-Aoio8WjMM6mstgZcTP0VzJk79HteVLhnwZy0kqbgVGQZYP8YsoqVNzsu0EBysX16qMJe7H2BsOAr4j=s70

【讨论】:

  • 谢谢。由于我需要将文件作为 http POST 传递,我认为我不能使用 serving_url。
  • 我现在可以使用 blob_reader.read() 读取图像数据。通过将 blob_reader 或 file_data 传递给第二个网页,我仍然无法让整个程序工作,所以我认为错误在下游。
猜你喜欢
  • 1970-01-01
  • 2011-12-18
  • 2023-03-25
  • 1970-01-01
  • 2015-10-14
  • 2016-10-16
  • 2021-05-08
  • 2014-11-14
  • 2014-04-07
相关资源
最近更新 更多