fsspec filesystem#

PyDrive2 provides easy way to work with your files through fsspec compatible GDriveFileSystem.

Installation#

pip install 'pydrive2[fsspec]'

Local webserver#

from pydrive2.fs import GDriveFileSystem

fs = GDriveFileSystem(
    "root",
    client_id="my_client_id",
    client_secret="my_client_secret",
)

By default, credentials will be cached per ‘client_id’, but if you are using multiple users you might want to use ‘profile’ to avoid accidentally using someone else’s cached credentials:

from pydrive2.fs import GDriveFileSystem

fs = GDriveFileSystem(
    "root",
    client_id="my_client_id",
    client_secret="my_client_secret",
    profile="myprofile",
)

Writing cached credentials to a file and using it if it already exists (which avoids interactive auth):

from pydrive2.fs import GDriveFileSystem

fs = GDriveFileSystem(
    "root",
    client_id="my_client_id",
    client_secret="my_client_secret",
    client_json_file_path="/path/to/keyfile.json",
)

Using cached credentials from json string (avoids interactive auth):

from pydrive2.fs import GDriveFileSystem

fs = GDriveFileSystem(
    "root",
    client_id="my_client_id",
    client_secret="my_client_secret",
    client_json=json_string,
)

Service account#

Using json keyfile path:

from pydrive2.fs import GDriveFileSystem

fs = GDriveFileSystem(
    # replace with ID of a drive or directory and give service account access to it
    "root",
    use_service_account=True,
    client_json_file_path="/path/to/keyfile.json",
)

Using json keyfile string:

from pydrive2.fs import GDriveFileSystem

fs = GDriveFileSystem(
    # replace with ID of a drive or directory and give service account access to it
    "root",
    use_service_account=True,
    client_json=json_string,
)

Use client_user_email if you are using delegation of authority.

Additional parameters#

trash_only (bool):

Move files to trash instead of deleting.

acknowledge_abuse (bool):

Acknowledging the risk and download file identified as abusive. See Abusive files for more info.

Using filesystem#

# replace `root` with ID of a drive or directory and give service account access to it
for root, dnames, fnames in fs.walk("root"):
    for dname in dnames:
        print(f"dir: {root}/{dname}")

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

Filesystem instance offers a large number of methods for getting information about and manipulating files, refer to fsspec docs on how to use a filesystem.