could anyone please inform me how to automatically get a shareable link of a file in our google drive using Colab notebook?
Thank you.
Simply click the Share button at the top right of any Colab notebook, or follow these Google Drive file sharing instructions. If you choose to share a notebook, the full contents of your notebook (text, code, output, and comments) will be shared.
To import google drive, write this code in code section of colab and run it by Ctrl+Enter . On running code, one blue link and a text box will appear we need to provide a permission text. So click the link and a new tab will open where you will be asked for permission to access google drive.
Colaboratory lets you connect to a local runtime using Jupyter. This allows you to execute code on your local hardware and have access to your local file system.
There are several ways to share Google Drive links. One way is to get the shareable link. This way& every member of an organization can receive the link and access the files or folders linked. It is also possible to share files with external individuals.
To publish the notebook to general audience, you may share it from your GitHub repository. There is one more way to share your work and that is by clicking on the SHARE link at the top right hand corner of your Colab notebook.
Just as the name implies& it is simply a link that users can share with other people. The link may be to a file or folder& and the creator can decide on access permissions. This method of sharing on Google is seen as very convenient. However& it is also the most vulnerable. This is because data may get misused& misunderstood& or breached sometimes.
Go ahead and open the Drive app, then navigate to the file you want to share. Tap the three-dot button on the file, then tap “Share link.” On iOS, this actually reads “Get link.” This is where the only real difference between Android and iOS happens: on iOS, the link is simply copied to the clipboard, so you can share it.
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.
If you look into the documentation you can see a section that explain how to list files from Drive.
Using that and reading the documentation of the library used, I've created this script:
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))
This is currently listing all files and providing a link to it.
You can then edit the function to find a specific file instead.
Hope this helps!
In my case, the suggested solution doesn't work. So I replaced the colab URL with "https://drive.google.com//file/d/"
Below what I used:
def get_shareable_link(file_path):
fid = getoutput("xattr -p 'user.drive.id' " + "'" + file_path + "'")
print(fid)
# make a link and display it
return HTML(f"<a href=https://drive.google.com/file/d/{fid} target=_blank>file URL</a>")
get_shareable_link("/content/drive/MyDrive/../img_01.jpg")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With