使用可恢复的API将文件上传到Google云端硬盘 [英] Uploading file to Google Drive using resumable API

查看:87
本文介绍了使用可恢复的API将文件上传到Google云端硬盘的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个很大的文件,我正在尝试使用API​​上传到Google驱动器.我正在尝试使用示例图像来进行学习.毫不犹豫地将图像上传为多文件上传或单个文件上传,但是当我尝试使用可恢复的上传端点进行操作时,代码给我一个错误,如下:

I have a quite large file that I am trying to upload to google drive using the API. I am trying to do it with a sample image for learning purposes. Uploading the image as a multiplart upload or a single file upload works without hesitation, but the moment I try to do it using the resumable upload endpoint, the code gives me a error as:

{
 "error": {
  "errors": [
   {
    "domain": "global",
    "reason": "badContent",
    "message": "Unsupported content with type: image/jpeg"
   }
  ],
  "code": 400,
  "message": "Unsupported content with type: image/jpeg"
 }
}

我正在使用的代码如下:

The code that I am using is as follows:

import requests
import os
filesize = os.path.getsize('./photo.jpeg')
print("File size is: ", filesize)

headers = {"Authorization" : "Bearer "+"<MY API KEY HERE>",
        "Content-Length": str(filesize),
        "Content-Type": "image/jpeg"}
params = {
        "name": "sample.png",
        "parents": ['1CxrbEfy5y3ZyBVF6k2IFIuOk_Z0wjZAo']
        }
files  = {
        'data': ('metadata', json.dumps(params), 'image/jpeg'),
        'file': open('./photo.jpeg', 'rb')
        }

r = requests.post(
        "https://www.googleapis.com/upload/drive/v3/files?uploadType=resumable",
        headers = headers,
        files = files
        )
print(r.text)

请帮助.

推荐答案

  • 您要使用断点续传上传 photo.jpeg 的图像文件.
  • 您想通过python使用 requests 来实现这一目标.
  • 您的访问令牌可以将文件上传到Google云端硬盘.
    • You want to upload an image file of photo.jpeg using the resumable upload.
    • You want to achieve this using requests with python.
    • Your access token can upload a file to Google Drive.
    • 如果我的理解是正确的,那么这个答案如何?请认为这只是几个可能的答案之一.

      If my understanding is correct, how about this answer? Please think of this as just one of several possible answers.

      • 在您的脚本中,

      • In your script,

        不使用
      • params .
      • 文件直接发送到 https://www.googleapis.com/upload/drive/v3/files?uploadType=resumable .
      • "Content-Type":"image/jpeg" 用于请求标头.这样,就会发生错误.
      • 您尝试上传'./photo.jpeg'.但是您尝试将文件名设置为 sample.png .
      • params is not used.
      • The file is directly send to https://www.googleapis.com/upload/drive/v3/files?uploadType=resumable.
      • "Content-Type": "image/jpeg" is used in the request headers. By this, the error occurs.
      • You try to upload './photo.jpeg'. But you try to set the filename as sample.png.

      为了通过可恢复的上传方式上传文件,首先,需要检索 location ,这是用于上传文件的URL.这包括在响应头中.参考

      In order to upload a file with the resumable upload, at first, it is required to retrieve location which is the URL for uploading the file. This is included in the response headers. Ref

      在以下示例脚本中,使用可恢复的上传上传了图像( image/jpeg ).在这种情况下,作为一个简单的测试,将文件上传一个块.在使用此功能之前,请设置 access_token filename

      In the following sample script, an image (image/jpeg) is uploaded with the resumable upload. In this case, as a simple test, the file is uploaded by one chunk. Before you use this, please set the variables of access_token, filename

      import json
      import os
      import requests
      
      access_token = '###'  # Please set your access token.
      filename = './photo.jpeg'  # Please set the filename with path.
      
      filesize = os.path.getsize(filename)
      print("File size is: ", filesize)
      
      # 1. Retrieve session for resumable upload.
      headers = {"Authorization": "Bearer "+access_token, "Content-Type": "application/json"}
      params = {
          "name": "sample.jpeg",
          "parents": ['1CxrbEfy5y3ZyBVF6k2IFIuOk_Z0wjZAo'],
          "mimeType": "image/jpeg"
      }
      r = requests.post(
          "https://www.googleapis.com/upload/drive/v3/files?uploadType=resumable",
          headers=headers,
          data=json.dumps(params)
      )
      location = r.headers['Location']
      
      # 2. Upload the file.
      headers = {"Content-Range": "bytes 0-" + str(filesize - 1) + "/" + str(filesize)}
      r = requests.put(location, headers=headers, data=open(filename, 'rb'))
      print(r.text)
      

      • 请再次确认 1CxrbEfy5y3ZyBVF6k2IFIuOk_Z0wjZAo 的父ID是否正确.
        • Please also confirm whether the parent ID of 1CxrbEfy5y3ZyBVF6k2IFIuOk_Z0wjZAo is correct, again.
        • 如果我误解了你的问题,而这不是你想要的方向,我深表歉意.

          If I misunderstood your question and this was not the direction you want, I apologize.

          这篇关于使用可恢复的API将文件上传到Google云端硬盘的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆