9

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'?
Leo
  • 1,176
  • 1
  • 13
  • 33
  • id = link.split('?')[0].split('/')[-1] – korakot Jun 23 '19 at 10:51
  • You have changed the 'folder' but not enough. You need to change every file within it too. See the error is for the file (1AiNv..Vd), not the folder – korakot Jun 24 '19 at 04:39
  • @Korakot Chaovavanich I have made everything shareable (see EDIT 2). is it that I am not sharing it the proper way? I did it by putting everything to:' anyone can view with the link'. should I put something else ? – Leo Jun 24 '19 at 17:26

4 Answers4

3

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']]
korakot
  • 37,818
  • 16
  • 123
  • 144
  • ok, so I would have to mount my drive first or how do I run it? I've added an edit with what I think you mean – Leo Jun 23 '19 at 08:33
  • You don’t need to mount at all. Just call auth.authenticate_user(). See gist for full code, including downloading with gdown. – korakot Jun 23 '19 at 10:39
  • There is some issue with how I am setting it up I think? I have edited the question to include what I have done following your gist. But I still get permission denied – Leo Jun 23 '19 at 13:50
  • This works - as you had seen, the permission denied i was getting was from sub-folders for which you need to pass the ID. But it still downloads the content of the top folder, then passing the sub-folder ID ti works. – Leo Oct 21 '21 at 08:50
1

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.

heitorsf
  • 41
  • 5
1

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.

-3

1). After using the code given below you will get the list of directories in your google drive, then you can use the folder whichever you want to use.

from google.colab import drive
drive.mount('/content/drive')

import os
os.listdir('/content/drive/My Drive')
Rahul charan
  • 765
  • 7
  • 15
  • I want to mount a **public** folder, so that anyone else can access it from their colab, this would still require access – Leo Jun 22 '19 at 09:41