TypeError when trying to upload Pictures from Google App Engine to Picasa with the GData API

孤人 提交于 2019-12-22 11:22:12

问题


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 ? :-)


回答1:


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.



来源:https://stackoverflow.com/questions/1715574/typeerror-when-trying-to-upload-pictures-from-google-app-engine-to-picasa-with-t

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!