2

Is it possible to set up colab so it can access only one of two folders on my google drive?

I know how to mount a colab book to the drive, but I'd like my collaborator to be able to read and write only from the folder I shared with them, not from the folder I've kept private. How do I restrict the notebook's access to only that one folder?

Zaq
  • 21
  • 1

1 Answers1

2
  1. Share Only the Specific Folder

    • In Google Drive, right-click the folder you want to share with your collaborator and select Share.
    • Add your collaborator's email with Viewer or Editor permissions (depending on whether they need write access).
    • Do not share your entire Drive or the private folder.
  2. Mount Google Drive in Colab
    When mounting Drive in Colab, use the standard:

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

    This gives the notebook access to the entire Drive, but you can restrict access programmatically.

  3. Restrict Access Programmatically
    After mounting, modify the notebook code to only interact with the shared folder. For example:

    shared_folder_path = '/content/drive/MyDrive/SharedFolderName'
    

    Example: List contents (only the shared folder will be visible to the collaborator)

    import os print(os.listdir(shared_folder_path))

    All file operations should be restricted to this path

    • Your collaborator will only be able to access files within SharedFolderName if that's the only path they work with.
  4. Avoid Exposing Private Files

    • Do not hardcode or expose paths to private folders in the notebook.
    • If your collaborator tries to access other parts of Drive (e.g., /content/drive/MyDrive/PrivateFolder), they will only succeed if they have permissions (which they shouldn’t, since you didn’t share it).
  • Mounting Drive gives full access (based on the Google Account's permissions). If your collaborator runs !ls /content/drive/MyDrive, they might see folder names (but cannot access contents of unshared folders).
  • No true "hard" restriction in Colab: Colab inherits the permissions of the Google Account used to mount Drive. If you want strict isolation:
    • Consider creating a separate Google Account, then share only the specific folder with that account.
    • Use that account to mount Drive in Colab (this way, even if the collaborator tries to explore, they won’t see your private files).

Alternative: Use a Service Account (Advanced)

For stricter control, you could use a Google Cloud Service Account with access only to the shared folder, but this requires GCP setup and is more complex.