我正在使用以下代码从我的 python 应用程序将图像上传到谷歌驱动器:
import logging
from django.core.management.base import BaseCommand
from apiclient.discovery import build
from apiclient.http import MediaFileUpload
import httplib2
from gdoauth2.models import DriveCredential
class Command(BaseCommand):
def handle(self, *args, **options):
credential = DriveCredential.objects.latest('id').credential
http = credential.authorize(httplib2.Http())
service = build('drive', 'v2', http=http)
mime_type = 'image/jpg'
filename = '/<path>/test.jpg'
logging.info('uploading %s' % filename)
media_body = MediaFileUpload(
filename, mimetype=mime_type, resumable=True)
upload = service.files().insert(
body=dict(title='test.jpg', mimeType=mime_type),
media_body=media_body, convert=True).execute()
上传后,我可以看到图像被插入到名为“test.jpg”的 doc 文件中,而不是在我的谷歌驱动器中将其视为精确的图像文件。如何将图像作为精确的图像文件上传到谷歌驱动器中。另外请帮我从网址上传图片。