【问题标题】:Get 400 error when try to upload a image file to a collection尝试将图像文件上传到集合时出现 400 错误
【发布时间】:2012-07-10 13:17:21
【问题描述】:

我想编写一个脚本来将我的照片上传到 Google 云端硬盘。经过几个小时深入研究 Google Document List API。我选择 gdata-python-client 2.0.17(latest) 来构建我的脚本。一切正常,除了我无法将文件上传到集合中。这是一个例外。

回溯(最近一次通话最后): 文件“./upload.py”,第 27 行,在 上传(sys.argv[1]) 文件“./upload.py”,第 22 行,上传中 client.create_resource(p, collection=f, media=ms) 文件“/usr/local/lib/python2.7/dist-packages/gdata/docs/client.py”,第 300 行,在 create_resource return uploader.upload_file(create_uri, entry, **kwargs) 文件“/usr/local/lib/python2.7/dist-packages/gdata/client.py”,第 1090 行,在 upload_file start_byte, self.file_handle.read(self.chunk_size)) 文件“/usr/local/lib/python2.7/dist-packages/gdata/client.py”,第 1048 行,在 upload_chunk 引发错误 gdata.client.RequestError:服务器响应:400,GDataInvalidEntryException很抱歉,发生服务器错误。请重试。

在侵入 gdata 的源代码后,我打印了一些信息进行调试。

> 范围:字节 0-524287/729223 放入:https://docs.google.com/feeds/upload/create-session/default/private/full/folder%3A0B96cfHivZx6ddGFwYXVCbzc4U3M/contents?upload_id=AEnB2UqnYRFTOyCCIGIESUIctWg6hvQIHY4JRMnL-CUQhHii3RGMFWZ12a7lXWd1hgOChd1Vqlr8dVnwh-B > 范围:字节 524288-729222/729223 放入:https://docs.google.com/feeds/upload/create-session/default/private/full/folder%3A0B96cfHivZx6ddGFwYXVCbzc4U3M/contents?upload_id=AEnB2UqnYRFTOyCCIGIESUIctWg6hvQIHY4JRMnL-CUQhHii3RGMFWZ12a7lXWd1hgOChd1Vqlr8dVnwh-B

PUT文件最后一部分时引发的异常。

【问题讨论】:

    标签: python google-drive-api


    【解决方案1】:

    我建议您尝试新的 Google Drive API v2,它通过更好地支持媒体上传使其变得更加容易:https://developers.google.com/drive/v2/reference/files/insert

    收到authorized service instance 后,您可以像这样简单地插入一个新文件:

    from apiclient import errors
    from apiclient.http import MediaFileUpload
    # ...
    
    def insert_file(service, title, description, parent_id, mime_type, filename):
      """Insert new file.
    
      Args:
        service: Drive API service instance.
        title: Title of the file to insert, including the extension.
        description: Description of the file to insert.
        parent_id: Parent folder's ID.
        mime_type: MIME type of the file to insert.
        filename: Filename of the file to insert.
      Returns:
        Inserted file metadata if successful, None otherwise.
      """
      media_body = MediaFileUpload(filename, mimetype=mime_type, resumable=True)
      body = {
        'title': title,
        'description': description,
        'mimeType': mime_type
      }
      # Set the parent folder.
      if parent_id:
        body['parents'] = [{'id': parent_id}]
    
      try:
        file = service.files().insert(
            body=body,
            media_body=media_body).execute()
    
        return file
      except errors.HttpError, error:
        print 'An error occured: %s' % error
        return None
    

    【讨论】:

    • 我已迁移到 Google Drive SDK v2。有用!另一个问题,有没有办法让匿名用户下载带有秘密链接的照片?也许我需要检查权限?
    • 对于匿名访问,您可以简单地向公共文件的编辑链接发送未经授权的GET 请求,该链接的格式应为https://www.googleapis.com/drive/v2/files/<FILE_ID>。由于未提供授权标头,因此您还需要提供 API Key 作为 ?key=<API_KEY> 查询参数。
    • 不幸的是它不起作用。回复是:{ "error": { "errors": [ { "domain": "global", "reason": "notFound", "message": "File not found: 0B2KSeJRpuK1PVm8xYTVaMHdVWGc" } ], "code": 404, "message": "File not found: 0B2KSeJRpuK1PVm8xYTVaMHdVWGc" } } 此外,我需要图片的直接链接或点击下载链接。这可能吗?
    • 刚刚发现照片查看器中的Download动作访问了https://doc-08-ao-docs.googleusercontent.com/docs/securesc/ha0ro937gcuc7l7deffksulhg5h7mbp1/0spj15t271ang5b0d6tc65p031hveq1d/1342058400000/14605084409704584131/*/0B2KSeJRpuK1PVm8xYTVaMHdVWGc?e=download这样的链接,有没有API可以生成这样的URL?
    • 不幸的是,除了再次检索文件的元数据(以获取更新的downloadUrl)之外,没有其他方法可以生成此链接。对于404 错误,请确保该文件是公开共享的,否则您将无法匿名访问该文件。
    猜你喜欢
    • 2021-02-11
    • 2021-05-13
    • 2017-02-11
    • 2015-10-08
    • 1970-01-01
    • 1970-01-01
    • 2018-01-07
    • 2021-03-23
    • 1970-01-01
    相关资源
    最近更新 更多