[英]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.