How to Scan GCP Storage Files for Viruses in Go

Cloudmersive
2 min readJun 11, 2021

Protecting your GCP cloud storage from threats has become quite the challenge; attackers are constantly developing and creating new ways to target cloud data, and it’s difficult to keep up with each new threat. However, by utilizing the following API in Go, you will be able to automatically scan the contents of a GCP storage file for threats by referencing a continuously updated threat database of over 17 million virus and malware signatures.

To begin the process you will need the GCP storage bucket name, object or file name, JSON service account credential, and your API key to input into the following code:

package mainimport (
"fmt"
"bytes"
"mime/multipart"
"os"
"path/filepath"
"io"
"net/http"
"io/ioutil"
)
func main() {url := "https://api.cloudmersive.com/virus/scan/cloud-storage/gcp-storage/single"
method := "POST"
payload := &bytes.Buffer{}
writer := multipart.NewWriter(payload)
file, errFile1 := os.Open("/path/to/file")
defer file.Close()
part1,
errFile1 := writer.CreateFormFile("jsonCredentialFile",filepath.Base("/path/to/file"))
_, errFile1 = io.Copy(part1, file)
if errFile1 != nil {
fmt.Println(errFile1)
return
}
err := writer.Close()
if err != nil {
fmt.Println(err)
return
}
client := &http.Client {
}
req, err := http.NewRequest(method, url, payload)
if err != nil {
fmt.Println(err)
return
}
req.Header.Add("bucketName", "<string>")
req.Header.Add("objectName", "<string>")
req.Header.Add("Content-Type", "multipart/form-data")
req.Header.Add("Apikey", "YOUR-API-KEY-HERE")
req.Header.Set("Content-Type", writer.FormDataContentType())
res, err := client.Do(req)
if err != nil {
fmt.Println(err)
return
}
defer res.Body.Close()
body, err := ioutil.ReadAll(res.Body)
if err != nil {
fmt.Println(err)
return
}
fmt.Println(string(body))
}

Your response will contain several pieces of information including the clean status, name of the virus (if applicable, error details (if applicable), and file size. If you need to obtain an API key, you can do so by visiting the Cloudmersive website and registering for a free API key; this will provide 800 monthly calls across any of our APIs.

--

--

Cloudmersive

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