简体   繁体   English

如何从 Google Drive 的选定文件夹中下载文件,替换 Heroku 上的现有文件?

[英]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").我在 Google Drive 上有文件(链接“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.从 csv 文件夹(位于不同文件夹,如驱动器/salescontrol7/csv 中)下载所有文件到文件夹“project_csv”,该文件夹现在是我的 python 目录,但将部署到 Z3115FB34DFCB5BA8AA049707C596B73
  • 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)).通过替换当前目录中的旧文件来保存下载的文件(这意味着如果 movies_metadata 存在,它应该替换它而不是保存为 movies_matadata(1))。

I need the simplest way, so I'm using PyDrive.我需要最简单的方法,所以我使用的是 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?如何访问文件夹 salescontrol7/csv,然后从那里下载所有内容,替换项目文件夹中的文件(如果有)?

Also maybe there is a way to do this without PyDrive just using publically shared link?另外,也许有一种方法可以在没有 PyDrive 的情况下仅使用公共共享链接来做到这一点?

Thank you in advance!先感谢您!

I found the best solution using gdown instead ( https://github.com/wkentaro/gdown ).我找到了使用 gdown 的最佳解决方案( 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!这两行可以帮助解决这个问题!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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