我想使用 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)
但我不知道如何做第二部分。到目前为止我已经尝试过
ffile = files.open(files.blobstore.get_file_name(blob_key), 'r')
从 google.appengine.ext 导入 blobstore
ffile = blobstore.BlobReader(blob_key)
从 google.appengine.ext 导入 blobstore
ffile = blobstore.BlobInfo.open(blobstore.BlobInfo(blob_key))
对于 isinstance(ffile, file)
,所有这些都给出 False
。
感谢任何帮助。
最佳答案
ffile = blobstore.BlobReader(blob_key)
有效。然而,返回的对象只有一个类似文件的接口(interface);它不扩展文件类。因此, isinstance 测试不起作用。尝试类似 if ffile and "read"in dir( ffile )
。
关于python - 将 blobstore blob 作为文件处理 (python),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14233238/