CF2021 introduces integration with Azure Blob, Microsoft’s object storage solution for the cloud. Operations such as Creating containers, Deleting container, Uploading File, Downloading File, Block Upload, Shared Access Signature are supported.
You have already seen AWS S3 with ColdFusion. In this blog I will cover cloud storage using Azure BLOB.
Pre-requisites:
- Azure account
- Azure Connection String
- Installing ‘azureblob’ package. For more information, see ColdFusion Package Manager.
Introduction
Blob or Binary Large OBject include images, text files, videos, or audio. Azure BLOB storage is a persistent data storage in cloud, which you can utilize to store BLOB data.
Term | Description |
Container | A container in an Azure Blob is a place where you store your objects, blobs, files, and so on. |
Snapshot | A snapshot is a read-only version of a blob that’s taken at a single point in time. After a snapshot has been created, it can be read, copied, or deleted, but not modified. Snapshots provide a way to back up a blob as it appears at a moment of time. |
Policy | Use policies to enforce additional level of control over blobs, containers, and so on. You can use a stored access policy to change the start time, expiry time, or permissions for a signature, or to revoke it after it has been issued. |
Shared Access Signature (SAS) | A Shared Access Signature (SAS) provides secure access to resources in your storage account without compromising the security of your data. With a SAS, you have granular control over how a client can access your data. |
Lease | Leasing a blob allows you to take ownership of that blob for a specified time. During that time, the Blob can still be read, but it cannot be modified or deleted until lease expires, or it is released. A blob can only have one active lease applied to it at a time. Once a lease has expired or been released, another user or process could then acquire a lease on that blob. |
Blob | In Azure, a blob is any object, file, etc. that you upload and store in a container. |
Blob Name | Every blob inside a container is given a name and must meet certain naming conventions. |
Getting Started
Create an AzureBlobService object using an Azure connection string.
<cfscript>
// In this example AZURE is the vendor
//Example for connection string: Endpoint=sb://{namespace}.servicebus.windows.net/;SharedAccessKeyName=key};SharedAccessKey={key}
credential = {
"vendorName" : "AZURE",
"connectionString" : "xxxxxxx"
}
// configuration object tells to create service object for Azure BLOB.
// many other things can be passed with in struct for azure blob but let’s keep it simple for this blog
configuration = {
"serviceName" : "AZURE_BLOB"
}
//getCloudService method give service handle for Azure Blob,
blobService = getCloudService(credential, configuration)
</cfscript>
Create a container
You can create a container using ‘container’ or ‘createContainer’. Let’s see an example to create container using blobService obtained in previous step.
<cfscript>
containerName = "first-container" & now().getTime() //now().getTime() is appended to make the container unique.
newContainerRequest = {
"containerName" : containerName,
"publicAccessType"= "blob"
}
contObj = blobService.createContainer(newContainerRequest);
// for already existing Container->
// contObj = blobService.container(containerName);
</cfscript>
List containers
To list all the containers within an account “listAll” method of blobService object will be used.
<cfscript>
containerList=blobService.listAll();
writeDump(containerList)
</cfscript>
Delete a container
To delete a container in an account, the “delete” method of blobService object will be used.
<cfscript>
blobService.delete(containerName)
</cfscript>
Upload a file to a container
Now let’s upload a file to the container using “uploadFile” method of blobService object. We can pass attributes such as accessConditions, context and options. But let’s keep it simple in the example.
<cfscript>
fileToBeUploaded = "xyz.txt";
key= "xyz";
uploadRequest = {
"srcFile": fileToBeUploaded,
"blobName": blobName
}
uploadResponse = contObj.uploadFile(uploadRequest);
</cfscript>
Download a BLOB to a file
We have already uploaded a file to the container, now let’s download it using ‘downloadToFile’.
<cfscript>
downloadedFile = "downloaded-xyx.txt";
downloadRequest = {
"destinationFIle": downloadedFile ,
"blobName": blobName
}
downloadResponse = contObj.downloadToFile(downloadRequest);
</cfscript>
List BLOBs in a container
We can list the blobs in the container using ‘listAll’. This method can also take a struct with few parameters.
<cfscript>
containerObjects = contObj.listAll();
writeDump(containerObjects);
</cfscript>
Delete a Blob in a container
You can delete a blob using ‘delete’ method.
<cfscript>
contObjects= contObj.delete(blobName);
</cfscript>
Copy Blobs from one container to another
In the below example we are copying a blob from the container ‘sourceContainer’ to another container ‘destinationContainer’
<cfscript>
destContainerObj = blobService.container(destinationContainer);
copyRequest = {
"source": sourceContainer/sourceBlobName,
"storageClass" : "HOT",
"blobName" : "destBlobName"
}
copyResponse= destContainerObj.copy(copyRequest);
</cfscript>
Store different versions of an object
You can create different versions of an already existing blob, which is present inside a container. Use the method “CreateSnapshot” for creating a version.
<cfscript>
versionRequest = {
"blobName" : "Version-1"
}
snapshot1 = contObj.Createsnapshot(versionRequest);
</cfscript>
Adding Shared Access Signature (SAS) to a Blob
Consider a scenario where you’ve uploaded a Blob to a Container. Now you want to share the container with read-only access. You can create an SAS with limited access (read-only) and share the container with another user. The result is that the second user can download the blob from the container but cannot upload data in the container. The method used to add SAS to a blob is ‘GenerateSas’.
<cfscript>
sasRequest = {
"blobName": blobName,
"policy": {
"permissions": ["READ"]
}}
sharedAccessSignature = contObj.GenerateSas (sasRequest);
</cfscript>
Upload a Blob parallelly
An object can be uploaded in parts parallelly. The part size can be mentioned in ‘singleBlobPutThresholdInBytes’.
<cfscript>
fileToBeUploaded = "xyz.txt";
key= "xyz";
uploadRequest = {
"srcFile": fileToBeUploaded,
"blobName" : blobName,
"options" : {
"concurrentRequestCount": 30,
"singleBlobPutThresholdInBytes":6.4e+7
}
}
uploadResponse = contObj.parallelUploadFile(uploadRequest);
</cfscript>
Download a blob parallelly
We have already uploaded a blob in parallel, now let’s download that blob in parallel.
<cfscript>
downloadedFile = "downloaded-xyx.txt"
downloadRequest = {
"destinationFIle": downloadedFile ,
"blobName" : blobName,
"options" : {
"concurrentRequestCount": 30,
"singleBlobPutThresholdInBytes":6.4e+7
}
}
downloadResponse = contObj.parallelDownloadFile(downloadRequest);
</cfscript>
Upload directories
You can upload a directory to the container using ‘uploadDirectory’
<cfscript>
directoryToBeUploaded = "abc_directory";
prefix= "abc";
uploadRequest = {
"sourceDirectory": directoryToBeUploaded,
"prefix": prefix, // prefix that will be added to the blob name.
"isIncludeSubDirectories" : "true"
}
uploadResponse = contObj.uploadDirectory(uploadRequest);
</cfscript>
Upload an object
ColdFusion Objects such as array, list, string etc can be uploaded to a container using ‘uploadObject’
<cfscript>
blobName = "xyz";
array1=[1,2,3,4,5];
uploadRequestsstruct ={
"object" : array1,
"blobName" : blobName,
"type" :"json",
"options":{
"maximumExecutionTimeInMs" : 1000
}
}
uploadResponse = contObj.uploadObject(uploadRequest);
</cfscript>
Download an object
You can also download an object that was uploaded using ‘downloadObject’
<cfscript>
blobName = "xyz";
downloadRequestsstruct ={
"blobName" : blobName,
"type" :"json”
}
uploadResponse = contObj.downloadObject(downloadRequestsstruct);
</cfscript>
Lease
A lease on a Blob enables you to be granted ownership to a Blob. You can perform different operations such as Acquire lease, Renew lease, Change lease, Release lease and Break lease. Let’s see an example for Acquire lease and break lease.
<cfscript>
aquireLeaseRequest = {
“blobName”: blobName,
“leaseTimeInSeconds”: 3600,
“proposedLeaseID”: “9876543” // optional, if proposedLeaseId is a value of leaseId proposed by user.
}
contObj.acuireLease(aquireLeaseRequest);
breakLeaseRequest = {
"blobName": blobName,
"accessCondition" : { "leaseID" :" 9876543" }
}
contObj.breakLease(breakLeaseRequest)
</cfscript>
Upload a file in blocks
It takes place in three steps.
1. Break file into blocks
<cfscript>
fileToBeUploaded=”xyz.zip”;
blobName=”xyz”;
createPartFileRequest = {
"srcFile": fileToBeUploaded,
"blockSizeInBytes": 102400,
"destinationDirectory": "Destination"
}
partFileSet = contObj.createPartFiles(createPartFileRequest) //The file has been divided into parts
</cfscript>
2. Upload block with Block Ids.
<cfscript>
blockIds = ArrayNew(1); //Array to store the block ids
blockIdItr = "ddd"
count=1
for(partFile in partFileSet ["partFiles"]) {
bid=""&(1000 + count) //toBase64(URLEncodedFormat(“ddd”&count))
structUpload = {
"blockId": bid,
"blobName": blobName,
"srcFile": partFile
}
contObj.blockUpload(structUpload);
arrayAppend(blockIds, bid)
count=count+1
}
</cfscript>
3. Commit the Block Ids
<cfscript>
commitBlockList = {
"blockIds": blockIds,
"blobName": blobName
}
contObj.commitBlockList(commitBlockList);
</cfscript>
You must be logged in to post a comment.