簡體   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