尝试使用GData API将图片从Google App Engine上传到Picasa时发生TypeError [英] TypeError when trying to upload Pictures from Google App Engine to Picasa with the GData API

查看:158
本文介绍了尝试使用GData API将图片从Google App Engine上传到Picasa时发生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([ - ]错误提取URL:%s% url)
$ b $ def uploadpicture(评论,图片):
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:// (b)b












$ /

$ b

完整的Stacktrace是:



追溯(最近一次调用最后一次):



文件/ home / birt / stuff / google / ap pengine / ext / webapp / init .py,第507行,在调用中
handler.get(* groups)



文件/home/birt/stuff/app_picasaupload/main.py,第124行,获得
uploadpicture(评论,图片)



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

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



文件/home/birt/stuff/app_picasaupload/gdata/photos/service.py,第398行,位于InsertPhoto
os.path.exists(filename_or_handle)中:#it's一个文件名



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



通话 $ b中的文件/home/birt/stuff/google/appengine/tools/dev_appserver.py,第1109行$ b如果不是FakeFile.I sFileAccessible(路径):

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

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

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

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

任何想法? : - )

解决方案

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



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



加入

  pic = StringIO.StringIO(pic) 

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


I'm trying to write a small tool to upload Pictures from Google App Engine to Picasa. Fetching the image works, but when i try to upload it i get the error "TypeError: stat() argument 1 must be (encoded string without NULL bytes), not str"

The Code basically looks like this:

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)

The full Stacktrace is:

Traceback (most recent call last):

File "/home/birt/stuff/google/appengine/ext/webapp/init.py", line 507, in call handler.get(*groups)

File "/home/birt/stuff/app_picasaupload/main.py", line 124, in get uploadpicture(comment, pic)

File "/home/birt/stuff/app_picasaupload/main.py", line 104, in uploadpicture entry = gd_client.InsertPhotoSimple(album_url, fname, comment, pic, content_type='image/jpeg')

File "/home/birt/stuff/app_picasaupload/gdata/photos/service.py", line 469, in InsertPhotoSimple content_type)

File "/home/birt/stuff/app_picasaupload/gdata/photos/service.py", line 398, in InsertPhoto os.path.exists(filename_or_handle): # it's a file name

File "/usr/lib/python2.5/posixpath.py", line 171, in exists st = os.stat(path)

File "/home/birt/stuff/google/appengine/tools/dev_appserver.py", line 1109, in call if not FakeFile.IsFileAccessible(path):

File "/home/birt/stuff/google/appengine/tools/dev_appserver.py", line 1018, in IsFileAccessible normcase=normcase)

File "/home/birt/stuff/google/appengine/tools/dev_appserver.py", line 1036, in _IsFileAccessibleNoCache if os.path.isdir(logical_filename):

File "/usr/lib/python2.5/posixpath.py", line 195, in isdir st = os.stat(path)

TypeError: stat() argument 1 must be (encoded string without NULL bytes), not str

Any Ideas ? :-)

解决方案

The Solution to this problem was using StringIO :-)

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

adding

pic = StringIO.StringIO(pic)

converts the result.content from urlfetch into a file-like format gdata expects.

这篇关于尝试使用GData API将图片从Google App Engine上传到Picasa时发生TypeError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆