Our office is in the process of determining which cloud service to work with. AWS is the dominant in the ColdFusion community - at least as far as the resources are concerned, but I know my office will lean towards Azure since Microsoft is already embedded into the workplace. So how does the configuration of Azure Blob compare to AWS S3? 

Configuring Azure Blob, compared to AWS S3

Cloud account configuration between the two services is similar. With Azure, Microsoft recognized my registered office account, and so the first step in account creation was taken care of. Also, I have a $200 credit that came with the setup of the platform; almost enough to buy one share of $MSFT stock, but I digress. 

Like AWS, Azure keeps track of my recent resources and places I've been for ease-of-use. I've set up a Storage account called ITMilestoneReports. In my Storage account, I've created several containers dynamically using CFML, after configuring the connection in ColdFusion 2021. Let's take a look. Below is a peek at my Azure Blob containers on Azure Blob. A litter further down in this post, I'll show you the code will create a container for you.

But before we can create these containers, we have to set up our cloud credentials in Azure. This is done is a bit differently in Azure than in AWS. an Azure Connection String is what ColdFusion 2021 wants, and so you simply copy it from Azure once you create it:

Configuring CF Admin to work with Azure Blob

Now we can add this to the ColdFusion 2021 Administrator. This is different than AWS's S3 requirements for a Region, Access Key, and Secret Key.  (Note that a key is also part of the Azure configuration, but not required by CF Admin.)

So now we create our Alias named BlobCred cloud credentials in CF 2021: 

Next,  we add the Cloud Configuration. Name an Azure Blob service called blobConf:

Write the Code

Now that BlobCred and BlobConfig are set up, let's take a look at some code that will get some files to the cloud, by creating a Container within the Blob Storage account, adding a file to it, and then downloading that file. 

<cfscript>

    // define the credential and the configuration aliases in the ColdFusion Admin

    storageService = getCloudService("blobCred", "blobConf")

    // upload a File

    uploadStruct = {

      "key" : "key001", // this is the name of the reference as it is viewed in Blob

      "srcFile" : "#ExpandPath('./')#/file.txt" // make sure this file exists

    }

    try {

      rootObj=storageService.root("root015","true") // createIfNotExists=FALSE

      uploadResponse=rootObj.uploadFile(uploadStruct) 

      writeOutput("File uploaded successfully ")

    }

    catch(any e){

      writeOutput("File upload failed")

    }

    // download the file

    downloadStruct={

      "key": "key001",

      "destinationFile": "fileDL.txt"

    }

    try{

      downloadResponse=rootObj.downloadToFile(downloadStruct);

      writeOutput("File downloaded successfully");

    }

    catch (any e){

      writedump(e);

    }

</cfscript>

If everything worked, you will notice that the downloaded file will appear in your directory. 

So we have been able to upload and download a file to a cloud container that we also created - all in under 30 lines of code. Pretty great!

Future blog posts on the Azure Blob / Adobe ColdFusion series will clarify additional configuration settings as well as perhaps going deeper into public vs private containers. But suffice it to say that, if you made it all the way through this walkthrough, you can say with confidence that you know how to easily store and retrieve files in the cloud using ColdFusion 2021 and Azure Blob. The CF docs have once again proven to be an excellent resource, and what I used to get started.