Skip to content

Commit a2efceb

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

File tree

4 files changed

+42
-4
lines changed

4 files changed

+42
-4
lines changed

docs/filemanagement.rst

Lines changed: 9 additions & 0 deletions
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 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?visit_id=637940058767312674-3438409752&rd=1

docs/fsspec.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,13 @@ Using json keyfile string:
9595
9696
Use `client_user_email` if you are using `delegation of authority`_.
9797

98+
Abusive files
99+
-------------
100+
101+
Use `acknowledge_abuse=True` parameter if you see
102+
'This file has been identified as malware or spam and cannot be downloaded'
103+
error. See `Abusive files`_ for more info.
104+
98105
Using filesystem
99106
----------------
100107

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

pydrive2/files.py

Lines changed: 17 additions & 2 deletions
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,8 @@ 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: Allow abusive files.
313+
:type acknowledge_abuse: bool
311314
:raises: ApiRequestError, FileNotUploadedError
312315
"""
313316
files = self.auth.service.files()
@@ -331,7 +334,12 @@ def download(fd, request):
331334
# But that would first require a slow call to FetchMetadata().
332335
# We prefer to try-except for speed.
333336
try:
334-
download(fd, files.get_media(fileId=file_id))
337+
download(
338+
fd,
339+
files.get_media(
340+
fileId=file_id, acknowledgeAbuse=acknowledge_abuse
341+
),
342+
)
335343
except errors.HttpError as error:
336344
exc = ApiRequestError(error)
337345
if (
@@ -362,6 +370,7 @@ def GetContentIOBuffer(
362370
encoding=None,
363371
remove_bom=False,
364372
chunksize=DEFAULT_CHUNK_SIZE,
373+
acknowledge_abuse=False,
365374
):
366375
"""Get a file-like object which has a buffered read() method.
367376
@@ -373,6 +382,8 @@ def GetContentIOBuffer(
373382
:type remove_bom: bool
374383
:param chunksize: default read()/iter() chunksize.
375384
:type chunksize: int
385+
:param acknowledge_abuse: Allow abusive files.
386+
:type acknowledge_abuse: bool
376387
:returns: MediaIoReadable -- file-like object.
377388
:raises: ApiRequestError, FileNotUploadedError
378389
"""
@@ -386,7 +397,11 @@ def GetContentIOBuffer(
386397
# But that would first require a slow call to FetchMetadata().
387398
# We prefer to try-except for speed.
388399
try:
389-
request = self._WrapRequest(files.get_media(fileId=file_id))
400+
request = self._WrapRequest(
401+
files.get_media(
402+
fileId=file_id, acknowledgeAbuse=acknowledge_abuse
403+
)
404+
)
390405
return MediaIoReadable(
391406
request, encoding=encoding, chunksize=chunksize
392407
)

pydrive2/fs/spec.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,7 @@ def __init__(
167167
client_json_file_path=None,
168168
use_service_account=False,
169169
profile=None,
170+
acknowledge_abuse=False,
170171
**kwargs,
171172
):
172173
"""Create an instance of GDriveFileSystem.
@@ -192,6 +193,8 @@ def __init__(
192193
:type use_service_account: bool.
193194
:param profile: Profile name for caching credentials
194195
(ignored for service account).
196+
:param acknowledge_abuse: Allow downloading abusive files.
197+
:type acknowledge_abuse: bool
195198
:type profile: str.
196199
:raises: GDriveAuthError
197200
"""
@@ -229,6 +232,7 @@ def __init__(
229232

230233
self.client = GoogleDrive(google_auth)
231234
self._trash_only = trash_only
235+
self._acknowledge_abuse = acknowledge_abuse
232236

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

552-
extra_args = {}
556+
extra_args = {"acknowledge_abuse": self._acknowledge_abuse}
553557
if block_size:
554558
extra_args["chunksize"] = block_size
555559

@@ -577,7 +581,9 @@ def _gdrive_open_file(self, item_id):
577581
param = {"id": item_id}
578582
# it does not create a file on the remote
579583
gdrive_file = self.client.CreateFile(param)
580-
fd = gdrive_file.GetContentIOBuffer()
584+
fd = gdrive_file.GetContentIOBuffer(
585+
acknowledge_abuse=self._acknowledge_abuse
586+
)
581587
return IterStream(iter(fd))
582588

583589
def rm_file(self, path):

0 commit comments

Comments
 (0)