ColdFusion and GCP Storage

Overview

Google Cloud Storage enables the storage and retrieval of data at any time. You can use Google Cloud Storage to serve content, store data for disaster recovery and archival, and make large clusters of data available for download.

ColdFusion (2021 release) supports AWS S3 storage and Azure Blob storage. In ColdFusion (2023 release), we have extended the capabilities of multi-cloud service by adding a GCP storage service so that you can access a wide variety of cloud storage services.

Like AWS S3, GCP storage also deals with buckets and objects.

  • Bucket: Each project can contain multiple buckets, which are containers to store your objects. For example, you might create a photos bucket for all the image files your app generates and a separate videos bucket.
  • Object: An individual file, such as, an image called holiday.png.

Get started

Install gcpstorage package

To install the package gcpstorage, use one of the following methods:

ColdFusion Administrator

  1. Navigate to ColdFusion Administrator > Package Manager.
  2. In the section Available Packages, search for gcpstorage.
  3. Select the package and click Install.

The installed package then gets listed in the Available Packages section.

ColdFusion Package Manager

Follow the steps below:

  1. Navigate to <CF_HOME>/cfusion/bin. Enter the command:
    1. Windows: cfpm.bat
    2. Non-Windows: ./cfpm.sh
  2. Enter the command, install gcpstorage.

Wait for the package to get installed.

Get credentials to access GCP Storage

When you interact with GCP, you specify your GCP security credentials to verify your credentials and check whether you have permission to access the resources that you are requesting.

GCP uses the security credentials to authenticate and authorize your requests.

For more information, see GCP API Keys Overview.

Add cloud service credentials and configuration

In ColdFusion, there is a method getCloudService() that gives you a handle to create objects for accessing various cloud services.

The syntax of the service handle is as follows:

service=getCloudService(cloudCred,cloudConfig), where:

  • cloudCred: Defines the credentials for the cloud service. It could either be a struct or a string (also known as credential alias).
  • cloudConfig: Defines the cloud service configuration details. It could either be a struct or a string (also known as config alias).

ColdFusion Administrator

Set credentials

In the ColdFusion Administrator, click Data & Services > Cloud Credentials. 

An alias is a named representation of a cloud service and its configuration details. You can set the config alias through ColdFusion Administrator.

After entering the details, click Add Credential.

Set configuration options

In the ColdFusion Administrator, click Data & Services > Cloud Configuration. 

Enter the following details, like configuration Alias, Vendor, and the name of the service.

Create the object

Once you've created the aliases for GCP credential and configuration options, you can create the object by using the getCloudService API, and include the following in your CFM. 

storageObject= getCloudService("gcpCred", "gcpConf")

Application.cfc

You can specify the file containing the GCP credentials and configuration options in Application.cfc. For example,

component{
this.name ="gcp"
void function onApplicationStart(){
application.gcpCred = {
projectId : "my-project",
credentialJsonFilePath : "path-to-creds.json"
};
alias.gcpConf = {
serviceName : "STORAGE",
alias : "gcpConfig",
retrySettings : {
initialRetryDelay : "2s",
initialRpcTimeout : "2s",
maxAttempts : 5 ,
maxRetryDelay : "30s",
maxRpcTimeout : "30s",
retryDelayMultiplier : 2 ,
rpcTimeoutMultiplier : 2,
totalTimeOut : "60s"
},
transportOptions : {
connectTimeout : "5000",
readTimeout : "5000"
}
};
}
}
component{ this.name ="gcp" void function onApplicationStart(){ application.gcpCred = { projectId : "my-project", credentialJsonFilePath : "path-to-creds.json" }; alias.gcpConf = { serviceName : "STORAGE", alias : "gcpConfig", retrySettings : { initialRetryDelay : "2s", initialRpcTimeout : "2s", maxAttempts : 5 , maxRetryDelay : "30s", maxRpcTimeout : "30s", retryDelayMultiplier : 2 , rpcTimeoutMultiplier : 2, totalTimeOut : "60s" }, transportOptions : { connectTimeout : "5000", readTimeout : "5000" } }; } }
component{
    this.name ="gcp"
      void function onApplicationStart(){
    application.gcpCred = {
        projectId : "my-project",
        credentialJsonFilePath : "path-to-creds.json"
    };
    alias.gcpConf = {
        serviceName : "STORAGE",                      
          alias : "gcpConfig",
          retrySettings : {
                   initialRetryDelay : "2s",   
                   initialRpcTimeout : "2s",   
                   maxAttempts : 5 ,    
                   maxRetryDelay : "30s", 
                   maxRpcTimeout : "30s", 
                   retryDelayMultiplier : 2 ,  
                   rpcTimeoutMultiplier : 2,  
                   totalTimeOut : "60s"
                  },
         transportOptions : {
                   connectTimeout : "5000",   
                   readTimeout : "5000"  
                   }

    };
    }
}

Create an object. Use the snippet below:

storageObject = getCloudService(application.gcpCred, application.gcpConf)

On CFM page

On a CFM page, you can specify the GCP credentials and configuration options in one of the four methods, specified below:

Credential and configuration aliases

After you've created the aliases for GCP credential and configuration options (in app.cfc or administrator) , you can use these aliases in the getCloudService handle as shown below:

Mention where the alias was created. Either in app.cfc or administrator.

<cfscript>
// define the credential and the configuration aliases in the ColdFusion Admin
storageObject=getCloudService("gcpCred","gcpConf")
// code below.
...........
</cfscript>
<cfscript> // define the credential and the configuration aliases in the ColdFusion Admin storageObject=getCloudService("gcpCred","gcpConf") // code below. ........... </cfscript>
<cfscript> 
  // define the credential and the configuration aliases in the ColdFusion Admin 
  storageObject=getCloudService("gcpCred","gcpConf") 
  // code below. 
  ........... 
</cfscript>

Credential alias and configuration struct

<cfscript>
// Using credential alias and struct for service config
gcpConf = {
"alias":"gcpConf",
"serviceName" : "STORAGE"
}
storageObject= getCloudService("gcpCred", gcpConf)
// code below
.....................
</cfscript>
<cfscript> // Using credential alias and struct for service config gcpConf = { "alias":"gcpConf", "serviceName" : "STORAGE" } storageObject= getCloudService("gcpCred", gcpConf) // code below ..................... </cfscript>
<cfscript> 
    // Using credential alias and struct for service config 
    gcpConf = { 
            "alias":"gcpConf", 
            "serviceName" : "STORAGE"
          
    } 
    storageObject= getCloudService("gcpCred", gcpConf) 
   
    // code below 
    ..................... 
</cfscript>

Configuration alias and credential struct

<cfscript>
// Using config alias and struct for service credentials
// GCP credentials
gcpCreds={
"vendorName":"GCP",
"alias": "gcpCred",
"projectId":"1234",
" credentialJsonFilePath ": "file path"
}
storageObject= getCloudService(gcpCreds, "gcpConf")
// code below
.....................................
</cfscript>
<cfscript> // Using config alias and struct for service credentials // GCP credentials gcpCreds={ "vendorName":"GCP", "alias": "gcpCred", "projectId":"1234", " credentialJsonFilePath ": "file path" } storageObject= getCloudService(gcpCreds, "gcpConf") // code below ..................................... </cfscript>
<cfscript> 
    // Using config alias and struct for service credentials 
    // GCP credentials 
    gcpCreds={ 
      "vendorName":"GCP", 
      "alias": "gcpCred", 
      "projectId":"1234", 
      " credentialJsonFilePath ": "file path"
    } 
    storageObject= getCloudService(gcpCreds, "gcpConf") 
    // code below 
    ..................................... 
</cfscript>

Credential and configuration structs

<cfscript>
// Using Structs for both cloud credential and config
gcpCreds={
"vendorName":"GCP",
"alias": "gcpCred",
"projectId":"1234",
" credentialJsonFilePath ": "file path"
}
gcpConf = {
"alias":"gcpConf",
"serviceName" : "STORAGE",
"clientOverrideConfig":{
"retryPolicy":{
"numRetries":4
}
},
"httpClientConfig":{
"maxConnections":50
}
}
storageObject= getCloudService(gcpCreds, gcpConf)
// code below
...................................................................
</cfscript>
<cfscript> // Using Structs for both cloud credential and config gcpCreds={ "vendorName":"GCP", "alias": "gcpCred", "projectId":"1234", " credentialJsonFilePath ": "file path" } gcpConf = { "alias":"gcpConf", "serviceName" : "STORAGE", "clientOverrideConfig":{ "retryPolicy":{ "numRetries":4 } }, "httpClientConfig":{ "maxConnections":50 } } storageObject= getCloudService(gcpCreds, gcpConf) // code below ................................................................... </cfscript>
<cfscript> 
  // Using Structs for both cloud credential and config 
 gcpCreds={ 
      "vendorName":"GCP", 
      "alias": "gcpCred", 
      "projectId":"1234", 
      " credentialJsonFilePath ": "file path"
    }
    gcpConf = { 
            "alias":"gcpConf", 
            "serviceName" : "STORAGE", 
            "clientOverrideConfig":{ 
                "retryPolicy":{ 
                  "numRetries":4 
                } 
            }, 
            "httpClientConfig":{ 
                "maxConnections":50 
            } 
      }
  storageObject= getCloudService(gcpCreds, gcpConf) 
   
  // code below 
  ................................................................... 
</cfscript>

Add credentials and configuration using CFSetup

Add:

  • Cloudconfiguration: add cloudconfigutaion alias=gcp_config serviceName=STORAGE <cf_alias_name>
  • Cloudconfiguration: add cloudconfigutaion alias=gcp_config serviceName=STORAGE maxRpcTimeout=50s retryDelayMultiplier=5 maxAttempts=8 maxRetryDelay=40s rpcTimeoutMultiplier=3 totalTimeout=50s <cf_alias_name>
  • Cloudcredential: add cloudcredential vendorName=”GCP” alias=<cloudcredential_alias> projectId=<project_id> credentialJSONFilePath=<credential_JSON_File_Path> <cf_alias_name>

Show:

  • Cloudconfiguration: show cloudconfiguration <cf_alias_name>
  • Cloudcredential: show cloudconfiguration <cloudconfiguration_name>

Set:

  • Cloudconfiguration: set cloudconfiguration <cloudconfiguration_name> initialRetryDelay=2s initialRpcTimeout=40s maxAttempts=5 maxRetryDelay=30s maxRpcTimeout=40s retryDelayMultiplier=3 rpcTimeoutMultiplier=2 totalTimeout=40s <cf_alias_name>
  • Cloudcredential: set cloudcredential <cloudcredential_name> projectId=<project_id> credentialJSONFilePath=<credential_JSON_File_Path> <cf_alias_name>

Get:

  • Cloudconfiguration: get cloudconfiguration <cloudconfiguration_name> <cf_alias_name>
  • Cloudcredential: get cloudcredential <cloudcredential_name> <cf_alias_name>

APIs for storage object

APIs for bucket object

Get help faster and easier

New user?