PyDrive 下载镜像 python
PyDrive download image python
我有包含附件的邮件消息。
我需要将附件从邮件上传到 google 驱动器。
对于邮件,我使用 imaplib and for google drive I'm using pyDrive
我正在使用以下代码获取附件:
if mail.get_content_maintype() == 'multipart':
for part in mail.walk():
if part.get_content_maintype() == 'multipart':
continue
if part.get('Content-Disposition') is None:
attachment = part.get_payload(decode=True)
我有 payload
我的邮件附件。现在我无法理解如何使用 pyDrive 将 payload
上传到 google 驱动器。我已经试过了,但没用
attachment = part.get_payload(decode=True)
gd_file = self.gd_box.g_drive.CreateFile({'title': 'Hello.jpg',
"parents": [{"kind": "drive#fileLink", "id": folder['id']}]})
gd_file.GetContentFile(attachment)
gd_file.Upload()
UPD:
此代码有效,但我认为它的解决方案不好(我们将图像保存在本地,然后将此图像上传到 google 驱动器)
attachment = part.get_payload(decode=True)
att_path = os.path.join("", part.get_filename())
if not os.path.isfile(att_path):
fp = open(att_path, 'wb')
fp.write(attachment)
fp.close()
gd_file = self.gd_box.g_drive.CreateFile({'title': part.get_filename(),
"parents": [{"kind": "drive#fileLink", "id": folder['id']}]})
gd_file.SetContentFile(part.get_filename())
gd_file.Upload()
GetContentFile()
is used to save a GoogleDriveFile
to a local file. You want the opposite so try using SetContentString()
,然后调用 Upload()
:
gd_file.SetContentString(attachment)
gd_file.Upload()
更新
如果您正在处理二进制数据,例如包含在图像文件中的数据,SetContentString()
将不起作用。作为解决方法,您可以将数据写入临时文件,上传到您的驱动器,然后删除临时文件:
import os
from tempfile import NamedTemporaryFile
tmp_name = None
with NamedTemporaryFile(delete=False) as tf:
tf.write(part.get_payload(decode=True))
tmp_name = tf.name # save the file name
if tmp_name is not None:
gd_file.SetContentFile(tf_name)
gd_file.Upload()
os.remove(tmp_name)
我有包含附件的邮件消息。
我需要将附件从邮件上传到 google 驱动器。
对于邮件,我使用 imaplib and for google drive I'm using pyDrive
我正在使用以下代码获取附件:
if mail.get_content_maintype() == 'multipart':
for part in mail.walk():
if part.get_content_maintype() == 'multipart':
continue
if part.get('Content-Disposition') is None:
attachment = part.get_payload(decode=True)
我有 payload
我的邮件附件。现在我无法理解如何使用 pyDrive 将 payload
上传到 google 驱动器。我已经试过了,但没用
attachment = part.get_payload(decode=True)
gd_file = self.gd_box.g_drive.CreateFile({'title': 'Hello.jpg',
"parents": [{"kind": "drive#fileLink", "id": folder['id']}]})
gd_file.GetContentFile(attachment)
gd_file.Upload()
UPD:
此代码有效,但我认为它的解决方案不好(我们将图像保存在本地,然后将此图像上传到 google 驱动器)
attachment = part.get_payload(decode=True)
att_path = os.path.join("", part.get_filename())
if not os.path.isfile(att_path):
fp = open(att_path, 'wb')
fp.write(attachment)
fp.close()
gd_file = self.gd_box.g_drive.CreateFile({'title': part.get_filename(),
"parents": [{"kind": "drive#fileLink", "id": folder['id']}]})
gd_file.SetContentFile(part.get_filename())
gd_file.Upload()
GetContentFile()
is used to save a GoogleDriveFile
to a local file. You want the opposite so try using SetContentString()
,然后调用 Upload()
:
gd_file.SetContentString(attachment)
gd_file.Upload()
更新
如果您正在处理二进制数据,例如包含在图像文件中的数据,SetContentString()
将不起作用。作为解决方法,您可以将数据写入临时文件,上传到您的驱动器,然后删除临时文件:
import os
from tempfile import NamedTemporaryFile
tmp_name = None
with NamedTemporaryFile(delete=False) as tf:
tf.write(part.get_payload(decode=True))
tmp_name = tf.name # save the file name
if tmp_name is not None:
gd_file.SetContentFile(tf_name)
gd_file.Upload()
os.remove(tmp_name)