Skip to content

fs: add acknowledge_abuse parameter #214

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions docs/filemanagement.rst
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,14 @@ is downloaded. Just set the `remove_bom` parameter in `GetContentString()` or
`GetContentFile()` - see `examples/strip_bom_example.py` in the GitHub
repository for an example.

Abusive files
-------------

Files identified as `abusive`_ (malware, etc.) are only downloadable by the owner.
If you see a
'This file has been identified as malware or spam and cannot be downloaded'
error, set 'acknowledge_abuse=True' parameter in `GetContentFile()`. By using
Copy link
Contributor Author

@efiop efiop Jul 21, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Decided not to add this flag to GetContentString for now because it is tiny bit more involved and we have no use for it anyway.

Also didn't add GetContentIOBuffer to this doc, because it is not documented at all yet 🙁

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@shcheklein I suppose you meant v2 https://developers.google.com/drive/api/v2/reference/files/get#acknowledgeAbuse . But I'm not sure how useful that is, who cares what we use internally.

it you indicate that you acknowledge the risks of downloading potential malware.

.. _`GoogleDriveFile`: /PyDrive2/pydrive2/#pydrive2.files.GoogleDriveFile
.. _`Upload()`: /PyDrive2/pydrive2/#pydrive2.files.GoogleDriveFile.Upload
Expand All @@ -251,3 +259,4 @@ repository for an example.
.. _`GetContentString()`: ./PyDrive2/pydrive2/#pydrive2.files.GoogleDriveFile.GetContentString
.. _`official documentation`: https://developers.google.com/drive/v2/reference/files#resource-representations
.. _`known`: https://productforums.google.com/forum/#!topic/docs/BJLimQDGtjQ
.. _`abusive`: https://support.google.com/docs/answer/148505
7 changes: 7 additions & 0 deletions docs/fsspec.rst
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,12 @@ Using json keyfile 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
----------------

Expand All @@ -114,4 +120,5 @@ about and manipulating files, refer to fsspec docs on
.. _`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
21 changes: 19 additions & 2 deletions pydrive2/files.py
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,7 @@ def GetContentFile(
remove_bom=False,
callback=None,
chunksize=DEFAULT_CHUNK_SIZE,
acknowledge_abuse=False,
):
"""Save content of this file as a local file.

Expand All @@ -308,6 +309,9 @@ def GetContentFile(
:type param: callable
:param chunksize: chunksize in bytes (standard 100 MB(1024*1024*100))
:type chunksize: int
:param acknowledge_abuse: Acknowledging the risk and download file
identified as abusive.
:type acknowledge_abuse: bool
:raises: ApiRequestError, FileNotUploadedError
"""
files = self.auth.service.files()
Expand All @@ -331,7 +335,12 @@ def download(fd, request):
# But that would first require a slow call to FetchMetadata().
# We prefer to try-except for speed.
try:
download(fd, files.get_media(fileId=file_id))
download(
fd,
files.get_media(
fileId=file_id, acknowledgeAbuse=acknowledge_abuse
),
)
except errors.HttpError as error:
exc = ApiRequestError(error)
if (
Expand Down Expand Up @@ -362,6 +371,7 @@ def GetContentIOBuffer(
encoding=None,
remove_bom=False,
chunksize=DEFAULT_CHUNK_SIZE,
acknowledge_abuse=False,
):
"""Get a file-like object which has a buffered read() method.

Expand All @@ -373,6 +383,9 @@ def GetContentIOBuffer(
:type remove_bom: bool
:param chunksize: default read()/iter() chunksize.
:type chunksize: int
:param acknowledge_abuse: Acknowledging the risk and download file
identified as abusive.
:type acknowledge_abuse: bool
:returns: MediaIoReadable -- file-like object.
:raises: ApiRequestError, FileNotUploadedError
"""
Expand All @@ -386,7 +399,11 @@ def GetContentIOBuffer(
# But that would first require a slow call to FetchMetadata().
# We prefer to try-except for speed.
try:
request = self._WrapRequest(files.get_media(fileId=file_id))
request = self._WrapRequest(
files.get_media(
fileId=file_id, acknowledgeAbuse=acknowledge_abuse
)
)
return MediaIoReadable(
request, encoding=encoding, chunksize=chunksize
)
Expand Down
17 changes: 12 additions & 5 deletions pydrive2/fs/spec.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,14 +159,15 @@ def __init__(
self,
path,
google_auth=None,
trash_only=True,
client_id=None,
client_secret=None,
client_user_email=None,
client_json=None,
client_json_file_path=None,
use_service_account=False,
profile=None,
trash_only=True,
acknowledge_abuse=False,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

style/minor - move trash only nearby to this one

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Strictly speaking, that would be breaking backwards compatibility. But we can add an *args guard (which is also breaking it but at least only once).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Introducing the guard forces changing tests, which is out of scope here. Can get back to this separately in a followup. For now moved those flags nearby, as requested.

**kwargs,
):
"""Create an instance of GDriveFileSystem.
Expand All @@ -175,8 +176,6 @@ def __init__(
:type path: str.
:param google_auth: Authenticated GoogleAuth instance.
:type google_auth: GoogleAuth.
:param trash_only: Move files to trash instead of deleting.
:type trash_only: bool.
:param client_id: Client ID of the application.
:type client_id: str
:param client_secret: Client secret of the application.
Expand All @@ -192,6 +191,11 @@ def __init__(
:type use_service_account: bool.
:param profile: Profile name for caching credentials
(ignored for service account).
:param trash_only: Move files to trash instead of deleting.
:type trash_only: bool.
:param acknowledge_abuse: Acknowledging the risk and download file
identified as abusive.
:type acknowledge_abuse: bool
:type profile: str.
:raises: GDriveAuthError
"""
Expand Down Expand Up @@ -229,6 +233,7 @@ def __init__(

self.client = GoogleDrive(google_auth)
self._trash_only = trash_only
self._acknowledge_abuse = acknowledge_abuse

def split_path(self, path):
parts = path.replace("//", "/").rstrip("/").split("/", 1)
Expand Down Expand Up @@ -549,7 +554,7 @@ def _gdrive_get_file(self, item_id, rpath, callback=None, block_size=None):
# it does not create a file on the remote
gdrive_file = self.client.CreateFile(param)

extra_args = {}
extra_args = {"acknowledge_abuse": self._acknowledge_abuse}
if block_size:
extra_args["chunksize"] = block_size

Expand Down Expand Up @@ -577,7 +582,9 @@ def _gdrive_open_file(self, item_id):
param = {"id": item_id}
# it does not create a file on the remote
gdrive_file = self.client.CreateFile(param)
fd = gdrive_file.GetContentIOBuffer()
fd = gdrive_file.GetContentIOBuffer(
acknowledge_abuse=self._acknowledge_abuse
)
return IterStream(iter(fd))

def rm_file(self, path):
Expand Down