Search code examples
pythongoogle-api-python-client

Reliably upload large files to Google Drive


I am using the Python googleapiclient to upload some large files to Google Drive. I want to make sure the files are uploaded correctly. I looked for ways to get the file's MD5 checksum on Google Drive with no luck. Here is the code:

def print_file_metadata(service, file_id):
    """Print a file's metadata.

  Args:
    service: Drive API service instance.
    file_id: ID of the file to print metadata for.
  """
    try:
        file = service.files().get(fileId=file_id).execute()

        print('Title: %s' % file['title'])
        print('MIME type: %s' % file['mimeType'])
    except errors.HttpError as error:
        print('An error occurred: %s' % error)

For the files I tested, it appears the file dict does not contain its MD5 checksum. Is there any way to get it from the API? Or is there another way of checking if file has been uploaded correctly?


Solution

    • You want to retrieve the file's MD5 checksum using Drive API.
    • You have already been able to put and get files with Drive API.
      • From your script, it seems that you are using Drive API v2.
    • You want to achieve this using google-api-python-client with python.

    If my understanding is correct, how about this answer?

    When Drive API v2 is used, the values returned from files().get() include the file's MD5 checksum. In this case, please modify your script as follows.

    Modified script: For Drive API v2

    file = service.files().get(fileId=file_id).execute()
    
    print('Title: %s' % file['title'])
    print('MIME type: %s' % file['mimeType'])
    print('MD5: %s' % file['md5Checksum'])  # <--- Added
    

    Modified script: For Drive API v3

    In the case of Drive API v3, the values returned from files().get() don't include the file's MD5 checksum. So as one of several methods, you can use fields='*' like below. But in this case, the filename is file['name']. Please be careful this.

    file = service.files().get(fileId=file_id, fields='*').execute()  # <--- Modified
    print('Title: %s' % file['name'])  # <--- Modified
    print('MIME type: %s' % file['mimeType'])
    print('MD5: %s' % file['md5Checksum'])  # <--- Added
    

    References:

    If I misunderstood your question and this was not the result you want, I apologize.