[英]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”)。 该文件将每天更新,无需代码,只需将其从谷歌驱动器中删除并上传一个新文件。 我需要代码来做以下事情。
我需要最简单的方法,所以我使用的是 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.