【发布时间】:2020-03-16 09:29:28
【问题描述】:
谁能告诉我如何使用 Colab notebook 自动获取我们谷歌驱动器中文件的可共享链接?
谢谢。
【问题讨论】:
标签: python url google-drive-api google-colaboratory
谁能告诉我如何使用 Colab notebook 自动获取我们谷歌驱动器中文件的可共享链接?
谢谢。
【问题讨论】:
标签: python url google-drive-api google-colaboratory
你可以使用xattr获取file_id
from subprocess import getoutput
from IPython.display import HTML
from google.colab import drive
drive.mount('/content/drive') # access drive
# need to install xattr
!apt-get install xattr > /dev/null
# get the id
fid = getoutput("xattr -p 'user.drive.id' '/content/drive/My Drive/Colab Notebooks/R.ipynb' ")
# make a link and display it
HTML(f"<a href=https://colab.research.google.com/drive/{fid} target=_blank>notebook</a>")
在这里,我通过/Colab Notebooks/R.ipynb 访问我的笔记本文件并创建一个链接以在 Colab 中打开它。
【讨论】:
如果您查看documentation,您会看到一个部分解释了如何列出云端硬盘中的文件。
使用它并阅读documentation of the library used,我创建了这个脚本:
from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive
from google.colab import auth
from oauth2client.client import GoogleCredentials
auth.authenticate_user()
gauth = GoogleAuth()
gauth.credentials = GoogleCredentials.get_application_default()
drive = GoogleDrive(gauth)
files = drive.ListFile().GetList()
for file in files:
keys = file.keys()
if 'webContentLink' in keys:
link = file['webContentLink']
elif 'webViewLink' in keys:
link = file['webViewLink']
else:
link = 'No Link Available. Check your sharing settings.'
if 'name' in keys:
name = file['name']
else:
name = file['id']
print('name: {} link: {}'.format(name, link))
这是当前列出所有文件并提供指向它的链接。
然后您可以编辑该函数以查找特定文件。
希望这会有所帮助!
【讨论】:
{'q': QUERY} 参数过滤文件,使其parent 属性为文件夹的ID。