2013-01-20 9 views
1

J'essaie de télécharger un fichier image à partir de Google Drive à l'aide de l'API python. Le code que j'utilise d'abord récupère l'objet ressource de fichier, trouve le fichier downloadUrl et l'utilise pour télécharger le fichier. Le problème est que les fichiers gif et png ne semblent pas avoir d'URL de téléchargement.Téléchargement d'images à partir de Google Drive

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 
... 

Cela fonctionne très bien pour le téléchargement de fichiers texte et html. Lorsque j'essaie de télécharger des fichiers image, ils ont la propriété downloadUrl. Voici un exemple de ressource de fichier image

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'}], 

Je ne comprends pas pourquoi il n'y a pas downloadURL. Cela peut être dû au fait que le type mime est application/vnd.google-apps.document mais le fichier est définitivement une image. Les liens des vignettes ect pointent vers une image. Toute aide serait appréciée.

+1

Lorsque vous avez importé les images, avez-vous activé la conversion? Le type mime étant application/vnd.google-apps.document me conduirait à croire qu'il a été converti. – David

+0

Oui, la conversion a été activée et les fichiers que j'ai téléchargés ont été convertis. Lorsque j'ai désactivé la conversion, tous les nouveaux fichiers image contenaient le fichier downloadUrl comme demandé. Merci. –

Répondre

Questions connexes