我使用 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 类型值。可悲的是,两者都没有奏效。