简体   繁体   中英

How to download files from selected folder of Google Drive replacing existing on Heroku?

I have files located on Google Drive (link "https://drive.google.com/drive/folders/1CuiRmfX2ORIzb62EM-L1x6UCm6t2uu4M?usp=sharing"). This file will be updated every day without code, just removing it from google drive and uploading a new one. I need code to do following things.

  • Log into this link or just google drive
  • Download all the files from csv folder (which is inside of different folder like drive/salescontrol7/csv) to folder 'project_csv' which is for now my python directory but will be deployed to heroku.
  • Save downloaded files by replacing old ones in the current directory (it means if movies_metadata exists it should replace it not save as movies_matadata(1)).

I need the simplest way, so I'm using PyDrive.

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


gauth = GoogleAuth()


gauth.LoadCredentialsFile("mycreds.txt")

if gauth.credentials is None:
    gauth.GetFlow()
    gauth.flow.params.update({'access_type': 'offline'})
    gauth.flow.params.update({'approval_prompt': 'force'})

    gauth.LocalWebserverAuth()

elif gauth.access_token_expired:

    gauth.Refresh()
else:


    gauth.Authorize()

gauth.SaveCredentialsFile("mycreds.txt")

drive = GoogleDrive(gauth)

file_list = drive.ListFile({'q': "'root' in parents and trashed=false"}).GetList()
for file1 in file_list:
  print('title: %s, id: %s' % (file1['title'], file1['id']))

But all I was able to do get list of folders and files on my-drive. How can access to folder salescontrol7/csv and then download everything from there replacing files in project folder if there are?

Also maybe there is a way to do this without PyDrive just using publically shared link?

Thank you in advance!

I found the best solution using gdown instead ( https://github.com/wkentaro/gdown ). It does download files from public link, and replaces folder each time.

# a folder
url = "https://drive.google.com/drive/folders/15uNXeRBIhVvZJIhL4yTw4IsStMhUaaxl"
gdown.download_folder(url, quiet=True, use_cookies=False)

# same as the above, but with the folder ID
id = "15uNXeRBIhVvZJIhL4yTw4IsStMhUaaxl"
gdown.download_folder(id=id, quiet=True, use_cookies=False)

These two lines can help with that!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM