从Google云端硬盘下载图片 [英] Downloading Images from Google Drive

查看:201
本文介绍了从Google云端硬盘下载图片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用python api从Google Drive下载一些图片文件。
我首先使用的代码获取文件资源对象,找到downloadUrl并使用它下载文件。问题是,gif和png文件似乎没有下载url。

  service = self.user.profile.DriveService ()
conditions = []
conditions.append(title ='image.png')
conditions.append(trashed = false)
response = service.files ().list(q =and.join(conditions))。execute()
f = response ['items'] [0]
url = f ['downloadUrl']
#现在从url
...
下载文件

这适用于下载文本和
当我尝试下载图像文件时,它们具有downloadUrl属性。
这里是一个示例图像文件资源

  u'alternateLink':u'https://docs.google.com/document/d/1S9amlqcwLl9FOYmHFhv_RK_NwPI47IWj8iGcIK9p8hk/edit',
u'appDataContents':False,
u'createdDate' :u'2013-01-20T08:39:55.314Z',
u'editable':True,
u'embedLink':u'https://docs.google.com/document/d / 1S9amlqcwLl9FOYmHFhv_RK_NwP I47IWj8iGcIK9p8hk / preview',
u'etag':u'Q0cVodxX8sh4vfxZTlOyWcmmc0k / MTM1ODY3MTE5NzY5Nw',
u'exportLinks':{u'application / pdf':u'https://docs.google。 COM /供稿/下载/文件/导出/导出? id = 1S9amlqcwLl9FOYmHFhv_RK_NwPI47IWj8iGcIK9p8hk& exportFormat = pdf',
u'application / rtf':u'https://docs.google.com/feeds/download/documents/export/Export?id = 1S9amlqcwLl9FOYmHFhv_RK_NwPI47IWj8iGcIK9p8hk& exportFormat = rtf' ,
u'application / vnd.oasis.opendocument.text':u'https://docs.google.com/feeds/download/documents/export/Export?id = 1S9amlqcwLl9FOYmHFhv_RK_NwPI47IWj8iGcIK9p8hk& exportFormat = odt',
u'application / vnd.openxmlformats-officedocument.wordprocessingml.document':u'https://docs.google.com/feeds/download/documents/export/Export?id = 1S9amlqcwLl9FOYmHFhv_RK_NwPI47IWj8iGcIK9p8hk& exportFormat = docx',
u'text / html':u'https://docs.google.com/feeds/download/documents/export/Export?id = 1S9amlqcwLl9FOYmHFhv_RK_NwPI47IWj8iGcIK9p8hk& exportFormat = html',
u'text / plain ':u'https://docs.google.com/feeds/download/documents/export/Export?id = 1S9a mlqcwLl9FOYmHFhv_RK_NwPI47IWj8iGcIK9p8hk& exportFormat = txt'},
u'iconLink':u'https://ssl.gstatic.com/docs/doclist/images/icon_11_document_list.png',
u'id':u '1S9amlqcwLl9FOYmHFhv_RK_NwPI47IWj8iGcIK9p8hk',
u'kind':u'drive#file',
u'labels':{...},
u'lastModifyingUserName':u'Max Ferguson',
u'lastViewedByMeDate':u'2013-01-20T08:39:57.697Z',
u'mimeType':u'application / vnd.google-apps.document',
u 'modifiedByMeDate':u'2013-01-20T08:39:57.697Z',
u'modifiedDate':u'2013-01-20T08:39:57.697Z',
u'ownerNames': [u'Max Ferguson'],
u'parents':[{u'id':u'0BxiZtRrps_bKaXM1OXJtUXBkOTg',
u'isRoot':False,
u'kind':u 'drive#parentReference',
u'parentLink':u'https://www.googleapis.com/drive/v2/files/0BxiZtRrps_bKaXM1OXJtUXBkOTg',
u'selfLink':u'https:/ /www.googleapis.c om / drive / v2 / files / 1S9amlqcwLl9FOYmHFhv_RK_NwPI47IWj8iGcIK9p8hk / parents / 0BxiZtRrps_bKaXM1OXJtUXBkOTg'}],

我无法理解为什么没有downloadUrl。这可能是因为mimetype是application / vnd.google-apps.document,但该文件绝对是一个图像。缩略图链接等指向图像。任何帮助,将不胜感激。 使用Google本机格式的文档(application / vnd.google-apps.XXX)没有 > downloadUrl 属性,您可以使用 exportLinks 集合以下列其中一种格式下载它们:



https://developers.google.com/drive/manage-downloads #downloading_google_documents


I am trying to download some image file from Google Drive using the python api. The code I am using first get the file resource object, the finds the downloadUrl and uses this to download the file. The problem is that gif and png files do not seem to have a download url.

service = self.user.profile.DriveService()
conditions = []
conditions.append("title='image.png')
conditions.append("trashed = false")
response = service.files().list(q=" and ".join(conditions)).execute()
f = response['items'][0]
url = f['downloadUrl']
# Now download the file from url
...

This works fine for downloading text and html files. When I try to download image files, they have downloadUrl property. Here is an example image file resource

u'alternateLink':  u'https://docs.google.com/document/d/1S9amlqcwLl9FOYmHFhv_RK_NwPI47IWj8iGcIK9p8hk/edit',
u'appDataContents': False,
u'createdDate': u'2013-01-20T08:39:55.314Z',
u'editable': True,
u'embedLink': u'https://docs.google.com/document/d/1S9amlqcwLl9FOYmHFhv_RK_NwPI47IWj8iGcIK9p8hk/preview',
u'etag': u'"Q0cVodxX8sh4vfxZTlOyWcmmc0k/MTM1ODY3MTE5NzY5Nw"',
u'exportLinks': {u'application/pdf': u'https://docs.google.com/feeds/download/documents/export/Export?   id=1S9amlqcwLl9FOYmHFhv_RK_NwPI47IWj8iGcIK9p8hk&exportFormat=pdf',
                 u'application/rtf':     u'https://docs.google.com/feeds/download/documents/export/Export?id=1S9amlqcwLl9FOYmHFhv_RK_NwPI47IWj8iGcIK9p8hk&exportFormat=rtf',
              u'application/vnd.oasis.opendocument.text': u'https://docs.google.com/feeds/download/documents/export/Export?id=1S9amlqcwLl9FOYmHFhv_RK_NwPI47IWj8iGcIK9p8hk&exportFormat=odt',
              u'application/vnd.openxmlformats-officedocument.wordprocessingml.document': u'https://docs.google.com/feeds/download/documents/export/Export?id=1S9amlqcwLl9FOYmHFhv_RK_NwPI47IWj8iGcIK9p8hk&exportFormat=docx',
              u'text/html': u'https://docs.google.com/feeds/download/documents/export/Export?id=1S9amlqcwLl9FOYmHFhv_RK_NwPI47IWj8iGcIK9p8hk&exportFormat=html',
              u'text/plain': u'https://docs.google.com/feeds/download/documents/export/Export?id=1S9amlqcwLl9FOYmHFhv_RK_NwPI47IWj8iGcIK9p8hk&exportFormat=txt'},
u'iconLink': u'https://ssl.gstatic.com/docs/doclist/images/icon_11_document_list.png',
u'id': u'1S9amlqcwLl9FOYmHFhv_RK_NwPI47IWj8iGcIK9p8hk',
u'kind': u'drive#file',
u'labels': {...},
u'lastModifyingUserName': u'Max Ferguson',
u'lastViewedByMeDate': u'2013-01-20T08:39:57.697Z',
u'mimeType': u'application/vnd.google-apps.document',
u'modifiedByMeDate': u'2013-01-20T08:39:57.697Z',
u'modifiedDate': u'2013-01-20T08:39:57.697Z',
u'ownerNames': [u'Max Ferguson'],
u'parents': [{u'id': u'0BxiZtRrps_bKaXM1OXJtUXBkOTg',
           u'isRoot': False,
           u'kind': u'drive#parentReference',
           u'parentLink': u'https://www.googleapis.com/drive/v2/files/0BxiZtRrps_bKaXM1OXJtUXBkOTg',
           u'selfLink': u'https://www.googleapis.com/drive/v2/files/1S9amlqcwLl9FOYmHFhv_RK_NwPI47IWj8iGcIK9p8hk/parents/0BxiZtRrps_bKaXM1OXJtUXBkOTg'}],

I can't understand why there is no downloadUrl. This may because the mimetype is application/vnd.google-apps.document but the file is definitely an image. The thumbnail links ect point to an image. Any help would be appreciated.

解决方案

Documents in Google native formats (application/vnd.google-apps.XXX) do not have the downloadUrl property, instead you can use the exportLinks collection to download them in one of the supported formats:

https://developers.google.com/drive/manage-downloads#downloading_google_documents

这篇关于从Google云端硬盘下载图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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