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

2 投票
1 回答
3089 浏览
提问于 2025-04-15 15:54

我正在尝试写一个小工具,用来把图片从 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)

完整的错误追踪信息是:

追踪记录(最近的调用在最前面):

文件 "/home/birt/stuff/google/appengine/ext/webapp/init.py",第 507 行,在 call

handler.get(*groups)

文件 "/home/birt/stuff/app_picasaupload/main.py",第 124 行,在 get 中

uploadpicture(comment, pic)

文件 "/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)

文件 "/home/birt/stuff/app_picasaupload/gdata/photos/service.py",第 398 行,在 InsertPhoto 中

os.path.exists(filename_or_handle): # 这是一个文件名

文件 "/usr/lib/python2.5/posixpath.py",第 171 行,在 exists 中

st = os.stat(path)

文件 "/home/birt/stuff/google/appengine/tools/dev_appserver.py",第 1109 行,在 call

if not 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(path)

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

有什么想法吗?:-)

1 个回答

5

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

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

添加

pic = StringIO.StringIO(pic)

这段代码把从 urlfetch 获取的结果内容转换成 gdata 所需要的文件格式。

撰写回答