Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access public Google drive folder (not from my drive) in Colab?

I have a public link for a GoogleDrive folder: https://drive.google.com/drive/folders/19RUYQNOzMJEA-IJ3EKKUf0qGyyOepzGk?usp=sharing

And I want to access the content in a colab notebook. I want anyone who opens the notebook to be able to access the folder, so without mounting my own drive. Other answers like Downloading public files in Google Drive (Python) seem to suggest slicing the ID. I tried following the instructions https://towardsdatascience.com/3-ways-to-load-csv-files-into-colab-7c14fcbdcb92

link= 'https://drive.google.com/drive/folders/19RUYQNOzMJEA-IJ3EKKUf0qGyyOepzGk?usp=sharing'

fluff, id = link.split('=')
print (id)

however my id is just 'sharing'

EDIT CODE STILL NOT WORKING

I have changed the permission of filesharing like so changing sharing permission

and then run the code:

from google.colab import auth

auth.authenticate_user()  # must authenticate


'''list all ids of files directly under folder folder_id'''

def folder_list(folder_id):

  from googleapiclient.discovery import build

  gdrive = build('drive', 'v3').files()

  res = gdrive.list(q="'%s' in parents" % folder_id).execute()

  return [f['id'] for f in res['files']]



'''download all files from a gdrive folder to current directory'''

def folder_download(folder_id):

  for fid in folder_list(folder_id):

    !gdown -q --id $fid

link='https://drive.google.com/drive/folders/1I6FwS5qB2bIwoPE4ueu8ZNH3upBqMB7S?usp=sharing'

folder_id="1I6FwS5qB2bIwoPE4ueu8ZNH3upBqMB7S"

folder_download(folder_id)

but get This error:

Permission denied: https://drive.google.com/uc?id=1AiNvRugUOWIthoSdBMBB5p5GLpyj6_Vd
Maybe you need to change permission over 'Anyone with the link'?

However I have changed the permission to 'anyone with the link

EDIT 2: making sure all folders have shareable active Following Korakot Chaovavanich comment, I have made sure every file/folder is shareable:

the url link refers to this folder: link share 1

inside it has this folder: link share 2

which has only one file, also shareable: link share 3

however running the code mentioned in EDIT 1: I get this error:

Permission denied: https://drive.google.com/uc?id=1AiNvRugUOWIthoSdBMBB5p5GLpyj6_Vd
Maybe you need to change permission over 'Anyone with the link'?
like image 719
Leo Avatar asked Jun 22 '19 09:06

Leo


3 Answers

Your folder_id is between '/' and '?'. You can use split twice or use regexp to extract it.

After that, you may want to list all files inside. Here's the gist example. The key part is

'''list all ids of files directly under folder folder_id'''
def folder_list(folder_id):
  from googleapiclient.discovery import build
  gdrive = build('drive', 'v3').files()
  res = gdrive.list(q="'%s' in parents" % folder_id).execute()
  return [f['id'] for f in res['files']]
like image 128
korakot Avatar answered Sep 29 '22 18:09

korakot


A solution I have just tested is to store your files in a public repository other than Google Drive, and then use ! to invoke a shell command do retrieve the file from there. Here's and working example code for downloading a file from a public Github repo into a Colab environment:

!wget https://raw.githubusercontent.com/heitorsf/pimpom/master/README.md

So you will have the file available on Colab. You can check it with !cat README.md.

Note: the best way of doing this is to use the URL for the "Raw" version of the file.

like image 42
heitorsf Avatar answered Sep 29 '22 18:09

heitorsf


Just create a shortcut from the public folder to your drive. To do so, right-click on the public folder and select the option to create a link to this folder. This will create a link to the public folder in your own drive. Then you can just connect to your own Google Drive using Colab in the usual way and access the folder just like any other folder in your drive.

like image 25
Kelison Bessa Avatar answered Sep 29 '22 17:09

Kelison Bessa