在 Colab 中访问 public Google 驱动器文件夹(不是来自我的驱动器)?

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

我有一个 public link 用于 GoogleDrive 文件夹:https://drive.google.com/drive/folders/19RUYQNOzMJEA-IJ3EKKUf0qGyyOepzGk?usp=sharing

我想访问 colab 笔记本中的内容。 我希望任何打开笔记本的人都能够访问该文件夹,因此无需安装我自己的驱动器。 Downloading public files in Google Drive (Python) 等其他答案似乎建议对 ID 进行切片。 我尝试按照说明进行操作 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)

然而我的 id 只是 'sharing'

编辑代码仍然无效

我已经像这样更改了文件共享的权限

然后运行代码:

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)

但出现此错误:

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

但是我已经将权限更改为“任何拥有 link

编辑 2:确保所有文件夹都具有可共享活动Korakot Chaovavanich 评论之后,我确保每个 file/folder 都是可分享的:

urllink指的是这个文件夹:

里面有这个文件夹:

只有一个文件,也可以共享:

然而 运行编辑 1 中提到的代码: 我收到此错误:

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

1).使用下面给出的代码后,您将获得 google 驱动器中的目录列表,然后您可以使用您想要使用的文件夹。

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

import os
os.listdir('/content/drive/My Drive')

您的 folder_id 在“/”和“?”之间。可以使用split两次或者使用regexp来解压。

之后,您可能想要列出其中的所有文件。这是 gist 示例。关键部分是

'''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']]

我刚刚测试过的一个解决方案是将您的文件存储在 Google 驱动器以外的 public 存储库中,然后使用 ! 调用 shell 命令从那里检索文件。这是将文件从 public Github 存储库下载到 Colab 环境中的工作示例代码:

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

因此您将在 Colab 上获得该文件。您可以使用 !cat README.md.

检查它

注意:执行此操作的最佳方法是对文件的 "Raw" 版本使用 URL。

只需创建一个从 public 文件夹到您的驱动器的快捷方式。为此,请右键单击 public 文件夹,然后选择 select 选项以在该文件夹中创建 link。这将在您自己的驱动器中的 public 文件夹中创建一个 link。然后,您可以按照通常的方式使用 Colab 连接到您自己的 Google 驱动器,并像访问驱动器中的任何其他文件夹一样访问该文件夹。