好吧,那里的API很奇怪,文档也不是很清楚。这是我想出来的。第一次上传附件时,通过 UploadAttachment 方法完成,但在后续尝试时,您需要调用 Update。这是执行此操作的代码:
class AttachmentUploader(object):
"""Uploads a given attachment to a given filecabinet in Google Sites."""
def __init__(self, site, username, password):
self.client = gdata.sites.client.SitesClient(
source="uploaderScript", site=site)
self.client.ssl = True
try:
self.client.ClientLogin(username, password, "some.key")
except:
traceback.print_exc()
raise
def FindAttachment(self, title):
uri = "%s?kind=%s" % (self.client.MakeContentFeedUri(), "attachment")
feed = self.client.GetContentFeed(uri=uri)
for entry in feed.entry:
if entry.title.text == title:
return entry
return None
def FindCabinet(self, title):
uri = "%s?kind=%s" % (self.client.MakeContentFeedUri(), "filecabinet")
feed = self.client.GetContentFeed(uri=uri)
for entry in feed.entry:
if entry.title.text == title:
return entry
return None
def Upload(self, cabinet_title, title, file_path, description):
"""Upload the given file as attachment."""
ms = gdata.data.MediaSource(file_path=file_path, content_type="text/ascii")
existing_attachment = self.FindAttachment(title)
if existing_attachment is not None:
existing_attachment.summary.text = description
updated = self.client.Update(existing_attachment, media_source=ms)
print "Updated: ", updated.GetAlternateLink().href
else:
cabinet = self.FindCabinet(cabinet_title)
if cabinet is None:
print "OUCH: cabinet %s does not exist" % cabinet_title
return
attachment = self.client.UploadAttachment(
ms, cabinet, title=title, description=description)
print "Uploaded: ", attachment.GetAlternateLink().href