我正在尝试通过v3 API将文件(pdf)上传到Google云端硬盘,我想用电子邮件中的附件创建文件。我发现的所有文档似乎都依赖于本地文件,因为我想上传一个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。
有人能帮助我吗?
答案 0 :(得分:0)
请尝试使用此UploaderForGoogleDrive示例,了解如何通过DrPaulBrewer将blob文件上传到Drive 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();
检查整个代码实现的链接。
答案 1 :(得分:0)
最后,关键是使用MediaInMemoryUpload
模块中的apiclient.http
,我通过检查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()