わかりました、そこにある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