【问题标题】:TypeError when trying to upload Pictures from Google App Engine to Picasa with the GData API尝试使用 GData API 将图片从 Google App Engine 上传到 Picasa 时出现 TypeError
【发布时间】:2009-11-11 14:38:24
【问题描述】:

我正在尝试编写一个小工具来将图片从 Google App Engine 上传到 Picasa。获取图像有效,但是当我尝试上传它时,我收到错误“TypeError: stat() argument 1 must be (encoded string without NULL bytes), not str

代码基本上是这样的:

def getfile(url):
    result = urlfetch.fetch(url)
    if result.status_code == 200:
        return (result.content)
    logging.error ("[-] Error fetching URL: %s" % url)

def uploadpicture(comment,pic):
    album_url = '/data/feed/api/user/%s/album/%s' % (username, album)
    fname = "image.jpg"
    entry = gd_client.InsertPhotoSimple(album_url, fname, comment, pic, content_type='image/jpeg')

picurl = "http://brilliantleap.com/blog/frog.jpg"
pic = getfile(picurl)
comment = "Test"
uploadpicture(comment, pic)

完整的 Stacktrace 是:

Traceback(最近一次调用最后一次):

文件“/home/birt/stuff/google/appengine/ext/webapp/init.py”,第 507 行,在 call 中 handler.get(*groups)

文件“/home/birt/stuff/app_picasaupload/main.py”,第 124 行,在 get 上传图片(评论,图片)

文件“/home/birt/stuff/app_picasaupload/main.py”,第 104 行,在上传图片中 entry = gd_client.InsertPhotoSimple(album_url, fname, comment, pic, content_type='image/jpeg')

文件“/home/birt/stuff/app_picasaupload/gdata/photos/service.py”,第 469 行,在 InsertPhotoSimple 中 内容类型)

InsertPhoto 中的文件“/home/birt/stuff/app_picasaupload/gdata/photos/service.py”,第 398 行 os.path.exists(filename_or_handle): # 是文件名

文件“/usr/lib/python2.5/posixpath.py”,第 171 行,存在 st = os.stat(路径)

调用中的文件“/home/birt/stuff/google/appengine/tools/dev_appserver.py”,第 1109 行 如果不是 FakeFile.IsFileAccessible(path):

文件“/home/birt/stuff/google/appengine/tools/dev_appserver.py”,第 1018 行,在 IsFileAccessible 中 normcase=normcase)

文件“/home/birt/stuff/google/appengine/tools/dev_appserver.py”,第 1036 行,在 _IsFileAccessibleNoCache 中 if os.path.isdir(logical_filename):

文件“/usr/lib/python2.5/posixpath.py”,第 195 行,在 isdir 中 st = os.stat(路径)

TypeError: stat() 参数 1 必须是(没有 NULL 字节的编码字符串),而不是 str

有什么想法吗? :-)

【问题讨论】:

  • 如果没有实际代码和完整的堆栈跟踪,这是不可能的。
  • 您是否有可能使用 Python 3?这就是错误消息所暗示的。 appengine SDK 专门支持(当然也支持生产版本;-)Python 2.5,目前不支持更高版本。
  • 这不是你最初所说的例外。正如异常所暗示的那样,您似乎在尝试获取无效的 URL。
  • 对不起尼克,我首先发布了错误的 Stacktrace - 已修复 :)
  • Alex:我正在使用 python2.5 明确启动 SDK

标签: python google-app-engine gdata-api typeerror picasa


【解决方案1】:

这个问题的解决方案是使用 StringIO :-)

(http://docs.python.org/library/stringio.html)

添加

pic = StringIO.StringIO(pic)

将 urlfetch 中的 result.content 转换为 gdata 期望的类文件格式。

【讨论】:

  • 我在尝试使用 zipfile 库解压缩字符串时遇到了类似的问题,这成功了。
猜你喜欢
  • 2013-07-13
  • 2011-05-23
  • 2016-10-28
  • 1970-01-01
  • 2015-02-07
  • 2013-09-14
  • 1970-01-01
  • 1970-01-01
  • 2012-09-05
相关资源
最近更新 更多