Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 537
Hermawan Wiwid Avatar asked Nov 20 '19 15:11

Hermawan Wiwid


People also ask

How do I share a link on Google Colab?

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.

How do I download a link from colab to Google Drive?

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.

Can Google colab access local files?

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.

How to share Google Drive links?

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.

How to share a Colab notebook?

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.

What is a shareable link on Google?

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.

How do I share files from Google Drive to my iPhone?

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.


Video Answer


3 Answers

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.

like image 153
korakot Avatar answered Oct 17 '22 11:10

korakot


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!

like image 2
ZektorH Avatar answered Oct 17 '22 10:10

ZektorH


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")
like image 2
J.K Avatar answered Oct 17 '22 11:10

J.K