【发布时间】:2012-08-07 19:56:11
【问题描述】:
我使用 google appengine 创建了一个表单和一个简单的服务器,用于将任意文件类型上传到我的 google 驱动器。该表单无法用于某些文件类型,而是给出此错误:
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 文档和图像。它也不适用于文本文件并给出此错误:
HttpError: <HttpError 400 when requesting https://www.googleapis.com/upload/drive/v1/files?alt=json returned "Multipart content has too many non-media parts">
我尝试取消设置资源字典中的“mimeType”值,让谷歌驱动器自动设置它。我还尝试在 MediaInMemoryUpload 构造函数中取消设置 mime 类型值。可悲的是,两者都没有奏效。
【问题讨论】:
-
您可以尝试使用可恢复上传协议吗?您可以通过将
resumable=True指定为MediaInMemoryUpload初始化程序:media_body=MediaInMemoryUpload(filestruct.value, filestruct.type, resumable=True),轻松切换到可恢复上传。 -
我试过
resumable = True,但没用。抱歉,我忘了说明我试过了。
标签: python google-app-engine google-drive-api