1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
|
fsspec filesystem
=================
*PyDrive2* provides easy way to work with your files through `fsspec`_
compatible `GDriveFileSystem`_.
Installation
------------
.. code-block:: sh
pip install 'pydrive2[fsspec]'
Local webserver
---------------
.. code-block:: python
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:
.. code-block:: python
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):
.. code-block:: python
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):
.. code-block:: python
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:
.. code-block:: python
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:
.. code-block:: python
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
----------------
.. code-block:: python
# 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`_.
.. _`fsspec`: https://filesystem-spec.readthedocs.io/en/latest/
.. _`GDriveFileSystem`: /PyDrive2/pydrive2/#pydrive2.fs.GDriveFileSystem
.. _`delegation of authority`: https://developers.google.com/admin-sdk/directory/v1/guides/delegation
.. _`Abusive files`: /PyDrive2/filemanagement/index.html#abusive-files
.. _`how to use a filesystem`: https://filesystem-spec.readthedocs.io/en/latest/usage.html#use-a-file-system
|