【问题标题】:Accessing a file from google drive within Python在 Python 中从谷歌驱动器访问文件
【发布时间】:2020-12-11 23:52:44
【问题描述】:
我正在处理一项机器学习任务,并保存了一个 Keras 模型并希望将其部署到 Github(以便我可以使用 Streamlit 和/或 Flask 托管一个 Web 演示)。但是,模型文件太大(> 1 GB),我无法将其免费上传到 Github。
我对替代方案的思考过程是将其上传到云服务,例如谷歌驱动器(或保管箱、盒子等),然后使用某种 Python 模块从那里访问它。
所以我的问题是,我可以将包含腌制 Keras 模型的腌制文件上传到 Google Drive,然后从 Python 脚本访问该对象吗?如果是这样,我该怎么做?
谢谢!
【问题讨论】:
标签:
python
google-drive-api
urllib
pydrive
【解决方案1】:
我相信你可以,你需要 pip oauth2client 和 gspread。要访问数据,您需要在您的谷歌驱动器上启用 API 管理器并以 json 文件的形式获取凭据。然后,您需要与授予其权限的凭据中的电子邮件共享文件。然后,您可以根据需要移植信息,我不确定 Keras 是如何工作的,但这将是第一步。
另一个重要因素是 Google api 在处理快速请求时非常敏感,为了克服这个问题,在每个请求之间放置睡眠命令,但如果你这样做,这种方法可能会降低你的想法.
scope = ["https://spreadsheets.google.com/feeds", 'https://www.googleapis.com/auth/spreadsheets',
"https://www.googleapis.com/auth/drive.file"、"https://www.googleapis.com/auth/drive"]
creds = ServiceAccountCredentials.from_json_keyfile_name("Your json file here.json", scope)
client = gspread.authorize(creds)
sheet = client.open("your google sheets name or whatever").sheet1 # Open the spreadhseet
data = sheet.get_all_records() # you can call all the information with this.