iterative / PyDrive2

Google Drive API Python wrapper library. Maintained fork of PyDrive.
https://docs.iterative.ai/PyDrive2
Other
565 stars 70 forks source link

AssertionError on using fsspec with service account #330

Closed Braintelligence closed 6 months ago

Braintelligence commented 6 months ago

Hello everyone,

on trying this code from the documentation:

from pydrive2.fs import GDriveFileSystem

fs = GDriveFileSystem(
    "root",
    use_service_account=True,
    client_json_file_path="token.json",
)

for root, dnames, fnames in fs.walk(""):
    for dname in dnames:
        print(f"dir: {root}/{dname}")

    for fname in fnames:
        print(f"file: {root}/{fname}")

I run into this error:

Traceback (most recent call last):
  File "PROJECTPATH\main.py", line 9, in <module>
    for root, dnames, fnames in fs.walk(""):
  File "PROJECTPATH\.venv\Lib\site-packages\fsspec\spec.py", line 420, in walk
    listing = self.ls(path, detail=True, **kwargs)
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "PROJECTPATH\.venv\Lib\site-packages\pydrive2\fs\spec.py", line 429, in ls
    assert bucket == self.root
AssertionError

The token is definitely working as I have successfully connected via the vanilla package and also the older PyDrive2 API.

Is the documentation outdated?

shcheklein commented 6 months ago

Yes, I think this an outdate docs issue. I don't think was actually properly supported. By it I mean any path that is relative to the root. ls (and thus walk that is using `ls) are expecting an "absolute" path.

I would try to use walk("root") (and I'll update the docs).

Note, that walk("root") with a service account will show you files on the service account's drive.

https://stackoverflow.com/questions/14124946/google-drive-service-account-view-files-from-another-account https://stackoverflow.com/questions/28534822/location-of-google-drive-service-account-uploads

To deal with files / directories in your account, you need to use ID of the drive/directory instead of root (it can be found in UI in url), and you need to give permission to a service account to view the files.