Skip to content

Commit 2e6ac10

Browse files
committed
fs: add acknowledge_abuse parameter
Related #62 Pre-requisite for iterative/dvc#7832
1 parent 40c831e commit 2e6ac10

File tree

4 files changed

+47
-7
lines changed

4 files changed

+47
-7
lines changed

docs/filemanagement.rst

+9
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,14 @@ is downloaded. Just set the `remove_bom` parameter in `GetContentString()` or
239239
`GetContentFile()` - see `examples/strip_bom_example.py` in the GitHub
240240
repository for an example.
241241

242+
Abusive files
243+
-------------
244+
245+
Files identified as `abusive`_ (malware, etc.) are only downloadable by the owner.
246+
If you see a
247+
'This file has been identified as malware or spam and cannot be downloaded'
248+
error, set 'acknowledge_abuse=True' parameter in `GetContentFile()`. By using
249+
it you indicate that you acknowledge the risks of downloading potential malware.
242250

243251
.. _`GoogleDriveFile`: /PyDrive2/pydrive2/#pydrive2.files.GoogleDriveFile
244252
.. _`Upload()`: /PyDrive2/pydrive2/#pydrive2.files.GoogleDriveFile.Upload
@@ -251,3 +259,4 @@ repository for an example.
251259
.. _`GetContentString()`: ./PyDrive2/pydrive2/#pydrive2.files.GoogleDriveFile.GetContentString
252260
.. _`official documentation`: https://developers.google.com/drive/v2/reference/files#resource-representations
253261
.. _`known`: https://productforums.google.com/forum/#!topic/docs/BJLimQDGtjQ
262+
.. _`abusive`: https://support.google.com/docs/answer/148505

docs/fsspec.rst

+7
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,12 @@ Using json keyfile string:
9595
9696
Use `client_user_email` if you are using `delegation of authority`_.
9797

98+
Additional parameters
99+
---------------------
100+
101+
:trash_only (bool): Move files to trash instead of deleting.
102+
:acknowledge_abuse (bool): Acknowledging the risk and download file identified as abusive. See `Abusive files`_ for more info.
103+
98104
Using filesystem
99105
----------------
100106

@@ -114,4 +120,5 @@ about and manipulating files, refer to fsspec docs on
114120
.. _`fsspec`: https://filesystem-spec.readthedocs.io/en/latest/
115121
.. _`GDriveFileSystem`: /PyDrive2/pydrive2/#pydrive2.fs.GDriveFileSystem
116122
.. _`delegation of authority`: https://developers.google.com/admin-sdk/directory/v1/guides/delegation
123+
.. _`Abusive files`: /PyDrive2/filemanagement/index.html#abusive-files
117124
.. _`how to use a filesystem`: https://filesystem-spec.readthedocs.io/en/latest/usage.html#use-a-file-system

pydrive2/files.py

+19-2
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,7 @@ def GetContentFile(
295295
remove_bom=False,
296296
callback=None,
297297
chunksize=DEFAULT_CHUNK_SIZE,
298+
acknowledge_abuse=False,
298299
):
299300
"""Save content of this file as a local file.
300301
@@ -308,6 +309,9 @@ def GetContentFile(
308309
:type param: callable
309310
:param chunksize: chunksize in bytes (standard 100 MB(1024*1024*100))
310311
:type chunksize: int
312+
:param acknowledge_abuse: Acknowledging the risk and download file
313+
identified as abusive.
314+
:type acknowledge_abuse: bool
311315
:raises: ApiRequestError, FileNotUploadedError
312316
"""
313317
files = self.auth.service.files()
@@ -331,7 +335,12 @@ def download(fd, request):
331335
# But that would first require a slow call to FetchMetadata().
332336
# We prefer to try-except for speed.
333337
try:
334-
download(fd, files.get_media(fileId=file_id))
338+
download(
339+
fd,
340+
files.get_media(
341+
fileId=file_id, acknowledgeAbuse=acknowledge_abuse
342+
),
343+
)
335344
except errors.HttpError as error:
336345
exc = ApiRequestError(error)
337346
if (
@@ -362,6 +371,7 @@ def GetContentIOBuffer(
362371
encoding=None,
363372
remove_bom=False,
364373
chunksize=DEFAULT_CHUNK_SIZE,
374+
acknowledge_abuse=False,
365375
):
366376
"""Get a file-like object which has a buffered read() method.
367377
@@ -373,6 +383,9 @@ def GetContentIOBuffer(
373383
:type remove_bom: bool
374384
:param chunksize: default read()/iter() chunksize.
375385
:type chunksize: int
386+
:param acknowledge_abuse: Acknowledging the risk and download file
387+
identified as abusive.
388+
:type acknowledge_abuse: bool
376389
:returns: MediaIoReadable -- file-like object.
377390
:raises: ApiRequestError, FileNotUploadedError
378391
"""
@@ -386,7 +399,11 @@ def GetContentIOBuffer(
386399
# But that would first require a slow call to FetchMetadata().
387400
# We prefer to try-except for speed.
388401
try:
389-
request = self._WrapRequest(files.get_media(fileId=file_id))
402+
request = self._WrapRequest(
403+
files.get_media(
404+
fileId=file_id, acknowledgeAbuse=acknowledge_abuse
405+
)
406+
)
390407
return MediaIoReadable(
391408
request, encoding=encoding, chunksize=chunksize
392409
)

pydrive2/fs/spec.py

+12-5
Original file line numberDiff line numberDiff line change
@@ -159,14 +159,15 @@ def __init__(
159159
self,
160160
path,
161161
google_auth=None,
162-
trash_only=True,
163162
client_id=None,
164163
client_secret=None,
165164
client_user_email=None,
166165
client_json=None,
167166
client_json_file_path=None,
168167
use_service_account=False,
169168
profile=None,
169+
trash_only=True,
170+
acknowledge_abuse=False,
170171
**kwargs,
171172
):
172173
"""Create an instance of GDriveFileSystem.
@@ -175,8 +176,6 @@ def __init__(
175176
:type path: str.
176177
:param google_auth: Authenticated GoogleAuth instance.
177178
:type google_auth: GoogleAuth.
178-
:param trash_only: Move files to trash instead of deleting.
179-
:type trash_only: bool.
180179
:param client_id: Client ID of the application.
181180
:type client_id: str
182181
:param client_secret: Client secret of the application.
@@ -192,6 +191,11 @@ def __init__(
192191
:type use_service_account: bool.
193192
:param profile: Profile name for caching credentials
194193
(ignored for service account).
194+
:param trash_only: Move files to trash instead of deleting.
195+
:type trash_only: bool.
196+
:param acknowledge_abuse: Acknowledging the risk and download file
197+
identified as abusive.
198+
:type acknowledge_abuse: bool
195199
:type profile: str.
196200
:raises: GDriveAuthError
197201
"""
@@ -229,6 +233,7 @@ def __init__(
229233

230234
self.client = GoogleDrive(google_auth)
231235
self._trash_only = trash_only
236+
self._acknowledge_abuse = acknowledge_abuse
232237

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

552-
extra_args = {}
557+
extra_args = {"acknowledge_abuse": self._acknowledge_abuse}
553558
if block_size:
554559
extra_args["chunksize"] = block_size
555560

@@ -577,7 +582,9 @@ def _gdrive_open_file(self, item_id):
577582
param = {"id": item_id}
578583
# it does not create a file on the remote
579584
gdrive_file = self.client.CreateFile(param)
580-
fd = gdrive_file.GetContentIOBuffer()
585+
fd = gdrive_file.GetContentIOBuffer(
586+
acknowledge_abuse=self._acknowledge_abuse
587+
)
581588
return IterStream(iter(fd))
582589

583590
def rm_file(self, path):

0 commit comments

Comments
 (0)