Search code examples
pythonurlgoogle-drive-apigoogle-colaboratory

Get a shareable link of a file in our google drive using Colab notebook


could anyone please inform me how to automatically get a shareable link of a file in our google drive using Colab notebook?

Thank you.


Solution

  • You can use xattr to get 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>")
    

    Here I access my notebook file at /Colab Notebooks/R.ipynb and make a link to open it in Colab.