Just had to optimize a bunch images (well actually quite a lot) stored within Azure Blob storage and wanted to share the initial PowerShell script and some details of how everything got together and got started. If you’re not interested in the details, just click here for the script.
You will need a Kraken.io account to follow along but don’t worry, the free plan should be sufficient for simple test cases.
Some details on how all this came to be
The problem is that the application responsible for creating the Blobs is barely applying any compression, and in addition generating images in a rapid pace. Ideally, you would alter the application first… but of course this wasn’t an option.
Given that Azure storage doesn’t support triggers, it’s required to iterate through all Blobs entries and update the content with a compressed version. Knowing that the application is still generating new storage objects, it’s also necessary to schedule this process as a job. Now, having done some work with Azure Automation, I’m aware of its capabilities and therefore want to make sure that the initial solution can be hosted into Azure Automation without to many alterations. The key point is to keep the compression part outside of the Automation context. And this is where Kraken Image Optimizer comes in to play.
Kraken Image Optimizer
I’m not in any way related to Kraken, so feel free to use another offering. I just discovered their service and was able to get things working in PowerShell within 5 minutes, which includes the signup process.
There are a couple of options when it comes to API calls. I’m currently using the “Wait” approach however there are alternatives if required. Detailed API information can be found here: https://kraken.io/docs/getting-started
Oh, and they have a cool dashboard as well. Now let’s move on to the important part.
The PowerShell script
The following script can be executed locally or on a management VM, but won’t run within the Azure Automation context. This will require converting it to the PowerShell Workflow format. This will not only allow you to schedule Automation jobs but supports parallel execution as well. The Set-AzureStorageBlobContent call will need to be replaced with a different approach, which most likely requires saving the metadata, deleting and recreating the item. I will add this versions as well if time permits