Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get file size from Google Drive Api (python)

I'm trying to figure out how to retrieve the file size of a file uploaded to Google Drive. According to the docs this should be in the file metadata... but when I request it, file size is not in the metadata at all.

file = self.drive_service.files().get(fileId=file_id).execute()
print(file)

>>> {u'mimeType': u'application/x-zip', u'kind': u'drive#file', u'id': u'0B3JGbAfem1CrWnhtWq5qYlkzSXf', u'name': u'myfile.ipa'}

What am I missing here? How can I check the file size?

like image 588
learningKnight Avatar asked Jun 02 '17 07:06

learningKnight


People also ask

What is Google Drive in Python?

Google Drive enables you to store your files in the cloud, which you can access anytime and everywhere in the world. In this tutorial, you will learn how to list your Google drive files, search over them, download stored files, and even upload local files into your drive programmatically using Python.

How do I set up a Google Drive API?

First, you need to have a Google account with Google Drive enabled. Head to this page and click the "Enable the Drive API" button as shown below: A new window will pop up; choose your type of application. I will stick with the "Desktop app" and then hit the "Create" button. After that, you'll see another window appear saying you're all set:

What is Google Drive REST API?

// Success. // ... The Drive REST API lets you create web apps that access files stored in Google Drive. The Drive REST API lets you create web apps that access files stored in Google Drive.

How to filter text/plain files using Google Drive API?

So we're filtering text/plain files here by using "mimeType='text/plain'" as query parameter, if you want to filter by name instead, you can simply use "name='filename.ext'" as query parameter. See Google Drive API documentation for more detailed information.


1 Answers

Per default, only a few select attributes are included in the metadata.

To request specific attributes, use the fields parameter:

file = self.drive_service.files().get(fileId=file_id, fields='size,modifiedTime').execute()

This would query a file's size and modification time.

By the way, the link you posted refers to the old v2 API. You can find a list of all file attributes in the current v3 API here.

like image 154
Aran-Fey Avatar answered Sep 22 '22 00:09

Aran-Fey