【问题标题】:Get a shareable link of a file in our google drive using Colab notebook使用 Colab 笔记本在我们的谷歌驱动器中获取文件的可共享链接
【发布时间】:2020-03-16 09:29:28
【问题描述】:

谁能告诉我如何使用 Colab notebook 自动获取我们谷歌驱动器中文件的可共享链接?

谢谢。

【问题讨论】:

    标签: python url google-drive-api google-colaboratory


    【解决方案1】:

    你可以使用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 中打开它。

    【讨论】:

      【解决方案2】:

      如果您查看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。
      猜你喜欢
      • 2018-07-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-15
      • 1970-01-01
      • 2019-11-02
      • 1970-01-01
      相关资源
      最近更新 更多