How to Check a GCP Storage File for Viruses in Ruby

Cloudmersive
2 min readJun 14, 2021

Entrusting confidential information to cloud storage is becoming an increasingly popular option for individuals and businesses alike. Cloud storage containers, such as Google Cloud Platform’s storage files, are accessible, scalable, and cost-effective, but the technology community still has some reservations about the security of these options. To add an extra layer of protection to your information, we can utilize the following API in Ruby to automatically check a GCP storage container for multiple threats.

To begin the process, we will add the Ruby client to our Gemfile:

gem 'cloudmersive-virus-scan-api-client', '~> 2.0.3'

Next, we can input the GCP bucket name, object/file name, JSON credential file, and your API key into the below code to call the virus scanning function:

# load the gem
require 'cloudmersive-virus-scan-api-client'
# setup authorization
CloudmersiveVirusScanApiClient.configure do |config|
# Configure API key authorization: Apikey
config.api_key['Apikey'] = 'YOUR API KEY'
# Uncomment the following line to set a prefix for the API key, e.g. 'Bearer' (defaults to nil)
#config.api_key_prefix['Apikey'] = 'Bearer'
end
api_instance = CloudmersiveVirusScanApiClient::ScanCloudStorageApi.newbucket_name = 'bucket_name_example' # String | Name of the bucket in Google Cloud Storageobject_name = 'object_name_example' # String | Name of the object or file in Google Cloud Storagejson_credential_file = File.new('/path/to/inputfile') # File | Service Account credential for Google Cloud stored in a JSON file.begin
#Scan an Google Cloud Platform (GCP) Storage file for viruses
result = api_instance.scan_cloud_storage_scan_gcp_storage_file(bucket_name, object_name, json_credential_file)
p result
rescue CloudmersiveVirusScanApiClient::ApiError => e
puts "Exception when calling ScanCloudStorageApi->scan_cloud_storage_scan_gcp_storage_file: #{e}"
end

This will provide a clean or infected result in less than a second; if there is an infected file, the virus name and type will be identified so you can address it immediately. To retrieve your API key, head to the Cloudmersive website to register for a free account that will give you access to 800 calls/month across any of our APIs.

--

--

Cloudmersive

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