我創建了一個表單和一個簡單的服務器與谷歌appengine與上傳任意文件類型到我的谷歌驅動器。該表單無法爲某些文件類型工作,只是給出了這個錯誤,而不是:谷歌雲端硬盤API:無法上傳某些文件類型
HttpError: <HttpError 400 when requesting https://www.googleapis.com/upload/drive/v1/files?alt=json returned "Unsupported content with type: application/pdf">
是否支持PDF文件?
的AppEngine上代碼,不會上傳去有點像這樣:
def upload_to_drive(self, filestruct):
resource = {
'title': filestruct.filename,
'mimeType': filestruct.type,
}
resource = self.service.files().insert(
body=resource,
media_body=MediaInMemoryUpload(filestruct.value,
filestruct.type),
).execute()
def post(self):
creds = StorageByKeyName(Credentials, my_user_id, 'credentials').get()
self.service = CreateService('drive', 'v1', creds)
post_dict = self.request.POST
for key in post_dict.keys():
if isinstance(post_dict[key], FieldStorage):#might need to import from cgi
#upload to drive and return link
self.upload_to_drive(post_dict[key]) #TODO: there should be error handling here
我已經成功地用它的MS Office文檔和圖像。它不爲TEXTFILES工作過,並給出此錯誤:
HttpError: <HttpError 400 when requesting https://www.googleapis.com/upload/drive/v1/files?alt=json returned "Multipart content has too many non-media parts">
我試過在解封的資源字典的'mime類型的價值,讓谷歌驅動器自動設置。我也嘗試在MediaInMemoryUpload構造函數中取消設置mime類型的值。可悲的是,兩者都沒有工作。
你可以嘗試使用可恢復的上傳協議嗎?通過爲MediaInMemoryUpload初始化程序指定'resumable = True',您可以輕鬆地切換到可恢復的上載:'media_body = MediaInMemoryUpload(filestruct.value,filestruct.type,resumable = True)'。 – Alain 2012-08-10 16:32:05
我曾試過'resumable = True',那沒用。對不起,我忘了說我試過了。 – 2012-08-10 20:16:19