Downloading files in Google Drive folder to Local C-Drive through Python

3.2k views Asked by At

I am trying to download a .csv file that populates to a Google Drive Folder through the process of Microsoft Flow that retrieves an email attachment document every 6 hours through email. I attempted to follow the documentation of using the Pydrive Module. I know how to create a file to upload & download to Google Drive but do not know how to download based on a link URL of an existing file. Here is my code.

from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive

gauth = GoogleAuth()
gauth.LocalWebserverAuth()

drive = GoogleDrive(gauth)

file_obj = drive.CreateFile({'id': '1j2vJVOB-_Xltmp9xkQJZzGb-5DAB_Imu'})
print(file_obj["title"], file_obj["mimeType"])
file_obj.GetContentFile('Assigned_Agency_AZS_Overhead_EXCEL.csv', mimetype='text/csv')

The error message that I am receiving is.

    runfile('C:/Google_Python_Test/Google/Google_Drive1.py', wdir='C:/Google_Python_Test/Google')
Your browser has been opened to visit:

    https://accounts.google.com/o/oauth2/auth?client_id=1078392182164-kone7oddogt31qfcg1qvp5u13fc3tivi.apps.googleusercontent.com&redirect_uri=http%3A%2F%2Flocalhost%3A8080%2F&scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fdrive&access_type=offline&response_type=code

Authentication successful.
Overhead application/vnd.google-apps.folder
Traceback (most recent call last):

  File "<ipython-input-26-6344c96b3f6e>", line 1, in <module>
    runfile('C:/Google_Python_Test/Google/Google_Drive1.py', wdir='C:/Google_Python_Test/Google')

  File "C:\Users\shrevee\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 668, in runfile
    execfile(filename, namespace)

  File "C:\Users\shrevee\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 108, in execfile
    exec(compile(f.read(), filename, 'exec'), namespace)

  File "C:/Google_Python_Test/Google/Google_Drive1.py", line 11, in <module>
    file_obj.GetContentFile('Assigned_Agency_AZS_Overhead_EXCEL.csv', mimetype='text/csv')

  File "C:\Users\shrevee\Anaconda3\lib\site-packages\pydrive\files.py", line 210, in GetContentFile
    self.FetchContent(mimetype, remove_bom)

  File "C:\Users\shrevee\Anaconda3\lib\site-packages\pydrive\files.py", line 43, in _decorated
    return decoratee(self, *args, **kwargs)

  File "C:\Users\shrevee\Anaconda3\lib\site-packages\pydrive\files.py", line 265, in FetchContent
    'No downloadLink/exportLinks for mimetype found in metadata')

FileNotDownloadableError: No downloadLink/exportLinks for mimetype found in metadata
1

There are 1 answers

11
Karl On

You might need to specify the MIME type for the file to make sure it downloads it correctly. If you are absolutely sure the file is a csv and not converted to a Google Document then you can do this:

from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive

gauth = GoogleAuth()
gauth.LocalWebserverAuth()

drive = GoogleDrive(gauth)

file_obj = drive.CreateFile({'id': '1j2vJVOB-_Xltmp9xkQJZzGb-5DAB_Imu'})
file_obj.GetContentFile('Assigned_Agency_AZS_Overhead_EXCEL.csv', mimetype='text/csv')