繁体   English   中英

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

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

我在 Google Drive 上有文件(链接“https://drive.google.com/drive/folders/1CuiRmfX2ORIzb62EM-L1x6UCm6t2uu4M?usp=sharing”)。 该文件将每天更新,无需代码,只需将其从谷歌驱动器中删除并上传一个新文件。 我需要代码来做以下事情。

  • 登录到这个链接或只是谷歌驱动器
  • 从 csv 文件夹(位于不同文件夹,如驱动器/salescontrol7/csv 中)下载所有文件到文件夹“project_csv”,该文件夹现在是我的 python 目录,但将部署到 Z3115FB34DFCB5BA8AA049707C596B73
  • 通过替换当前目录中的旧文件来保存下载的文件(这意味着如果 movies_metadata 存在,它应该替换它而不是保存为 movies_matadata(1))。

我需要最简单的方法,所以我使用的是 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']))

但是我所能做的就是获取驱动器上的文件夹和文件列表。 如何访问文件夹 salescontrol7/csv,然后从那里下载所有内容,替换项目文件夹中的文件(如果有)?

另外,也许有一种方法可以在没有 PyDrive 的情况下仅使用公共共享链接来做到这一点?

先感谢您!

我找到了使用 gdown 的最佳解决方案( https://github.com/wkentaro/gdown )。 它确实从公共链接下载文件,并且每次都替换文件夹。

# 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)

这两行可以帮助解决这个问题!

暂无
暂无

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

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