2012-08-10 184 views
1

我创建了一个表单和一个简单的服务器与谷歌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类型的值。可悲的是,两者都没有工作。

+0

你可以尝试使用可恢复的上传协议吗?通过为MediaInMemoryUpload初始化程序指定'resumable = True',您可以轻松地切换到可恢复的上载:'media_body = MediaInMemoryUpload(filestruct.value,filestruct.type,resumable = True)'。 – Alain 2012-08-10 16:32:05

+0

我曾试过'resumable = True',那没用。对不起,我忘了说我试过了。 – 2012-08-10 20:16:19

回答

4

在我看来,您使用的是旧版本的Python客户端库,并且指的是Drive API v1,而Drive API v2自6月底以来一直可用。

请尝试更新您的库并检查完整的Python示例https://developers.google.com/drive/examples/python

+0

谢谢。像魅力一样工作!事实证明,我使用的是过时版本的Python客户端库,我几周前从DrEdit示例中复制出来。 – 2012-08-10 20:53:57

相关问题