Virus Scan a File (Advanced Scan, 17+ Million Threat Signatures) in SharePoint Online Site Drive in Python

Cloudmersive
3 min readFeb 21, 2022
Cloudmersive Advanced Virus Scan for SharePoint Online Response Model

The best way to make sure your database storage is secure is by scanning your data in storage. If even one infected file gets past your existing web of security policies, the resulting damage to your company’s ‘vital organs’ can be significant — and even fatal. Using the Cloudmersive Virus Scan API, you can mitigate over 17 million known threat signatures (with continuous cloud updates) including viruses, executables, scripts, invalid files, etc. to your SharePoint Online Site Drive storage with ready-to-run code from the Cloudmersive API console. Let’s run through how you can use this in Python.

You’ll first need to run the below command (for Python SDK installation):

pip install cloudmersive-virus-api-client

After that, copy the first half of the callback function below, and add your Cloudmersive API key where indicated in the second snippet:

from __future__ import print_function
import time
import cloudmersive_virus_api_client
from cloudmersive_virus_api_client.rest import ApiException
from pprint import pprint
# Configure API key authorization: Apikey
configuration = cloudmersive_virus_api_client.Configuration()
configuration.api_key['Apikey'] = 'YOUR_API_KEY'

Finally, complete the function with the rest of the code from the Cloudmersive API Console. It’s important to read the documentation carefully at this stage and select various “true” and “false” values to fully customize your scan:

# create an instance of the API class
api_instance = cloudmersive_virus_api_client.ScanCloudStorageApi(cloudmersive_virus_api_client.ApiClient(configuration))
client_id = 'client_id_example' # str | Client ID access credentials; see description above for instructions on how to get the Client ID from the Azure Active Directory portal.
client_secret = 'client_secret_example' # str | Client Secret access credentials; see description above for instructions on how to get the Client Secret from the Azure Active Directory portal
sharepoint_domain_name = 'sharepoint_domain_name_example' # str | SharePoint Online domain name, such as mydomain.sharepoint.com
site_id = 'site_id_example' # str | Site ID (GUID) of the SharePoint site you wish to retrieve the file from
tenant_id = 'tenant_id_example' # str | Optional; Tenant ID of your Azure Active Directory (optional)
file_path = 'file_path_example' # str | Path to the file within the drive, such as 'hello.pdf' or '/folder/subfolder/world.pdf' (optional)
item_id = 'item_id_example' # str | SharePoint itemID, such as a DriveItem Id (optional)
allow_executables = true # bool | Set to false to block executable files (program code) from being allowed in the input file. Default is false (recommended). (optional)
allow_invalid_files = true # bool | Set to false to block invalid files, such as a PDF file that is not really a valid PDF file, or a Word Document that is not a valid Word Document. Default is false (recommended). (optional)
allow_scripts = true # bool | Set to false to block script files, such as a PHP files, Python scripts, and other malicious content or security threats that can be embedded in the file. Set to true to allow these file types. Default is false (recommended). (optional)
allow_password_protected_files = true # bool | Set to false to block password protected and encrypted files, such as encrypted zip and rar files, and other files that seek to circumvent scanning through passwords. Set to true to allow these file types. Default is false (recommended). (optional)
allow_macros = true # bool | Set to false to block macros and other threats embedded in document files, such as Word, Excel and PowerPoint embedded Macros, and other files that contain embedded content threats. Set to true to allow these file types. Default is false (recommended). (optional)
allow_xml_external_entities = true # bool | Set to false to block XML External Entities and other threats embedded in XML files, and other files that contain embedded content threats. Set to true to allow these file types. Default is false (recommended). (optional)
restrict_file_types = 'restrict_file_types_example' # str | Specify a restricted set of file formats to allow as clean as a comma-separated list of file formats, such as .pdf,.docx,.png would allow only PDF, PNG and Word document files. All files must pass content verification against this list of file formats, if they do not, then the result will be returned as CleanResult=false. Set restrictFileTypes parameter to null or empty string to disable; default is disabled. (optional)
try:
# Advanced Virus Scan a file in a SharePoint Online Site Drive
api_response = api_instance.scan_cloud_storage_scan_share_point_online_file_advanced(client_id, client_secret, sharepoint_domain_name, site_id, tenant_id=tenant_id, file_path=file_path, item_id=item_id, allow_executables=allow_executables, allow_invalid_files=allow_invalid_files, allow_scripts=allow_scripts, allow_password_protected_files=allow_password_protected_files, allow_macros=allow_macros, allow_xml_external_entities=allow_xml_external_entities, restrict_file_types=restrict_file_types)
pprint(api_response)
except ApiException as e:
print("Exception when calling ScanCloudStorageApi->scan_cloud_storage_scan_share_point_online_file_advanced: %s\n" % e)

--

--

Cloudmersive

There’s an API for that. Cloudmersive is a leader in Highly Scalable Cloud APIs.