首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将blobstore blob作为文件处理(python)

将blobstore blob作为文件处理(python)
EN

Stack Overflow用户
提问于 2013-01-09 10:32:49
回答 2查看 1.8K关注 0票数 2

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

我已经想出了如何做第一部分

代码语言:javascript
复制
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))

所有这些都为False提供了isinstance(ffile, file)

任何帮助都是非常感谢的。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-01-09 10:43:33

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

票数 2
EN

Stack Overflow用户

发布于 2013-01-09 14:23:15

要从from存储读取file_data:

代码语言:javascript
复制
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

但是,您也可以发送一个url与blob_key和服务的网址。对于图像,您不必自己提供图像,因为您可以通过动态缩放使用发布一个get_serving_url。以这种方式提供图像也非常便宜。

下面是这样一个url的例子:

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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14233238

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档