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?
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.
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:
// 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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With