Python Google Drive API-使用blob从MediaFileUpload上载

Python Google Drive API-使用blob从MediaFileUpload上载,python,google-drive-api,Python,Google Drive Api,我正试图通过v3 API将一个文件(pdf)上传到Google Drive,我想从电子邮件的附件中创建该文件。我找到的所有文档似乎都依赖于本地文件,因为我想上传一个blob 以下是迄今为止我的代码的要点: class MailHandler(InboundMailHandler): def receive(self, mail_message): logging.info("Received a message from: " + mail_message.sender) i

我正试图通过v3 API将一个文件(pdf)上传到Google Drive,我想从电子邮件的附件中创建该文件。我找到的所有文档似乎都依赖于本地文件,因为我想上传一个blob

以下是迄今为止我的代码的要点:

class MailHandler(InboundMailHandler):
  def receive(self, mail_message):
    logging.info("Received a message from: " + mail_message.sender)
    if hasattr(mail_message, 'attachments'):
      logging.info("Has attachments")
      for filename, filecontents in mail_message.attachments:
        file_blob = filecontents.payload.decode(filecontents.encoding)
        credentials = UserModel.query().get()
        media = MediaFileUpload(filename, mimetype = 'image/jpg')
        file = drive.files().create( body = {'name': 'testupload.jpg'}, media_body = media)
        file.execute(c.credentials.authorize(http))
这会引发一个错误,因为该文件不存在,但由于它是一个blob,因此不会引发该错误

有人能帮我吗?

请尝试以下示例,了解如何通过DrPaulBrewer将blob文件上载到驱动API:

片段:

**
     * Helper class for resumable uploads using XHR/CORS. Can upload any Blob-like item, whether
     * files or in-memory constructs.
     *
     * @example
     * var content = new Blob(["Hello world"], {"type": "text/plain"});
     * var uploader = new MediaUploader({
     *   file: content,
     *   token: accessToken,
     *   onComplete: function(data) { ... }
     *   onError: function(data) { ... }
     * });
     * uploader.upload();

检查整个代码实现的链接。

最后,关键是使用
apiclient.http
模块中的
MediaInMemoryUpload
,我通过检查
http
找到了该模块

下面是如何使用它的大致轮廓:

from apiclient.http import MediaInMemoryUpload
media = MediaInMemoryUpload(file_blob, mime_type, resumable=True)
file = drive.files().create(body={'name': filename},media_body=media)
file.execute()

除非我遗漏了什么,否则这是一个客户端javascript函数,我想问的是一个服务器端python问题?“不推荐:将MediaIoBaseUpload与流的io.TextIOBase或StringIO一起使用。”