How to Scan a Google Cloud Storage File for Viruses & Malware in PHP

Cloudmersive
2 min readMar 22, 2024

--

Scanning suspicious files in Google Cloud Storage helps reveal hidden virus and malware threats. Using complementary PHP code examples, we can scan files with ease.

We’ll be calling a free virus & malware scanning API using the PHP code examples below. This scan references 17 million+ virus & malware signatures and leverages predictive threat scanning techniques.

Before we copy code to make our API call, let’s first gather the below information from our Google Cloud Storage account:

  1. Bucket Name
  2. Object Name (file name; if it contains Unicode characters, please base64 encode the file and prepend with ‘base64’)
  3. JSON Credential File (service account credential for Google Cloud Storage in a JSON file)

Now we can install the PHP client using Composer by executing the following command from the command line:

composer require cloudmersive/cloudmersive_virusscan_api_client

Next, we can call the virus & malware scanning function using the following code:

<?php
require_once(__DIR__ . '/vendor/autoload.php');

// Configure API key authorization: Apikey
$config = Swagger\Client\Configuration::getDefaultConfiguration()->setApiKey('Apikey', 'YOUR_API_KEY');



$apiInstance = new Swagger\Client\Api\ScanCloudStorageApi(


new GuzzleHttp\Client(),
$config
);
$bucket_name = "bucket_name_example"; // string | Name of the bucket in Google Cloud Storage
$object_name = "object_name_example"; // string | Name of the object or file in Google Cloud Storage. If the object name contains Unicode characters, you must base64 encode the object name and prepend it with 'base64:', such as: 'base64:6ZWV6ZWV6ZWV6ZWV6ZWV6ZWV'.
$json_credential_file = "/path/to/inputfile"; // \SplFileObject | Service Account credential for Google Cloud stored in a JSON file.

try {
$result = $apiInstance->scanCloudStorageScanGcpStorageFile($bucket_name, $object_name, $json_credential_file);
print_r($result);
} catch (Exception $e) {
echo 'Exception when calling ScanCloudStorageApi->scanCloudStorageScanGcpStorageFile: ', $e->getMessage(), PHP_EOL;
}
?>

To authorize our API calls for free, we’ll just need a free Cloudmersive API key (this allows a limit of 800 API calls per month with zero commitments).

That’s all there is to it — now we can easily scan individual Google Cloud Storage files for viruses and malware in PHP.

--

--

Cloudmersive
Cloudmersive

Written by Cloudmersive

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

No responses yet