尝试使用GData API将图片从Google App Engine上传到Picasa时出现TypeError

Bir*_*irt 2 python google-app-engine picasa gdata-api typeerror

我正在尝试编写一个小工具,将图片从Google App Engine上传到Picasa.获取图像有效,但是当我尝试上传它时,我得到错误" TypeError:stat()参数1必须是(没有NULL字节的编码字符串),而不是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)
Run Code Online (Sandbox Code Playgroud)

完整的Stacktrace是:

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

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

文件"/home/birt/stuff/app_picasaupload/main.py",第124行,获取uploadpicture(评论,pic)

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

在InsertPhotoSimple content_type中输入文件"/home/birt/stuff/app_picasaupload/gdata/photos/service.py",第469行

在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(path)

文件"/home/birt/stuff/google/appengine/tools/dev_appserver.py",1109,在通话 如果不是FakeFile.IsFileAccessible(路径):

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

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

在isdir st = os.stat(path)中输入文件"/usr/lib/python2.5/posixpath.py",第195行

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

有任何想法吗 ?:-)

Bir*_*irt 5

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

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

加入

pic = StringIO.StringIO(pic)
Run Code Online (Sandbox Code Playgroud)

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