【问题标题】:Google Drive API: Can't upload certain filetypesGoogle Drive API:无法上传某些文件类型
【发布时间】: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


【解决方案1】:

在我看来,您使用的是旧版本的 Python 客户端库并指的是 Drive API v1,而 Drive API v2 已于 6 月底推出。

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

【讨论】:

  • 谢谢。像魅力一样工作!事实证明,我使用的是几周前从 DrEdit 示例中复制的过时版本的 Python 客户端库。
  • 看起来该链接转到 404。您有类似的链接吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-03-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多