0

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
Eric Shreve
  • 21
  • 1
  • 7
  • Possible duplicate of [Pydrive error: No downloadLink/exportLinks for mimetype found in metadata](https://stackoverflow.com/questions/46155300/pydrive-error-no-downloadlink-exportlinks-for-mimetype-found-in-metadata) – omri_saadon Nov 02 '18 at 15:33
  • Are you sure the file is a csv and that the id is correct? Or is the file a Google mime-type? – Karl Nov 02 '18 at 15:40
  • Yes, the file is a csv, as well as the correct File ID. – Eric Shreve Nov 02 '18 at 15:43
  • 1
    @EricShreve as shown by what is printed out, the file id given is not actually correct that is the id for the folder 'Overhead' which is why you are getting the error. – Karl Nov 02 '18 at 21:09

1 Answers1

0

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')
Karl
  • 1,664
  • 2
  • 12
  • 19
  • Karl, I received `FileNotDownloadableError: No downloadLink/exportLinks for mimetype found in metadata` when I attempted to execute that script. – Eric Shreve Nov 02 '18 at 15:50
  • Did you try `text/csv` as the mimetype? I think that's the correct one actually based on [this](https://github.com/gsuitedevs/PyDrive/issues/8) github issue related to downloading CSV's. Also, can you do `print(file_obj["title"], file_obj["mimeType"])` after doing the `drive.CreateFile()` and show what the output is? That way we can verify what mimetype the file is stored as in GDrive – Karl Nov 02 '18 at 15:52
  • After following this `file = drive.CreateFile({'id': '1j2vJVOB-_Xltmp9xkQJZzGb-5DAB_Imu'}) print(file_obj["title"], file_obj["mimeType"]) file.GetContentFile('Assigned_Agency_AZS_Overhead_EXCEL.csv', mimetype='text/csv')` I still get the No downloadLink/exportLinks error – Eric Shreve Nov 02 '18 at 18:15
  • But what did it print out? That was the purpose of the print statement. It is going to output the file name and mimeType which can be used to figure out what is wrong. – Karl Nov 02 '18 at 18:19
  • After adding your code I received this in the console. `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')` I am not getting a print result. – Eric Shreve Nov 02 '18 at 19:43
  • I updated the original question with the error message that I am getting in the console. – Eric Shreve Nov 02 '18 at 19:55
  • As you can see what was printed out, you are not downloading the csv but you are downloading the folder. `Overhead application/vnd.google-apps.folder` shows that the mimeType of what you are trying to download is a folder. You need to give the id of the file. – Karl Nov 02 '18 at 20:50
  • That worked, changing the ID to the actual CSV File. But the process that I am attempting to do is to upload a new CSV file every 6 hours and then download it locally to my machine using the same Google Drive Directory via Microsoft Flow. The CSV ID changes every time so this process will not work if I attempt to automate it through the windows task scheduler. Is there a way to download all files within a Google Drive Folder through the PyDrive Workflow? – Eric Shreve Nov 02 '18 at 21:36
  • You can query for all files in that folder and download any ones that are of type csv or matching the file name title you are looking for. – Karl Nov 02 '18 at 21:37
  • Stupid question from me. Do you have any code snippets to get me pointed in the right direction? – Eric Shreve Nov 02 '18 at 21:47
  • `file_list = drive.ListFile({'q': "'{}' in parents and trashed=false".format(folder_id)}).GetList()` where `folder_id` is the id for the parent folder. It will return a list of each file object in the folder which you can loop through to then get the csv file and download it. Since this problem is different then the current one (and this one seems to be solved), please ask it in a different question if you run into issues. – Karl Nov 02 '18 at 21:52