2

我正在尝试编写一个小工具来将图片从 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 是:

回溯(最近一次通话最后):

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

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

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

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

File "/home/birt/stuff/app_picasaupload/gdata/photos/service.py", line 398, in InsertPhoto os.path.exists(filename_or_handle): # 这是一个文件名

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

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

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

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

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

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

有任何想法吗 ?:-)

4

1 回答 1

5

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

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

添加

pic = StringIO.StringIO(pic)

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

于 2009-11-12T20:07:55.550 回答