I’ve been working on some new batch features for the Kraken-PowerShell module, which I would like to demonstrate within this blog post. Also, you will find some helpful automation scripts at the end.
Kraken-PowerShell – batch image compression
For those who are new to Kraken-Powershell, Kraken-PowerShell centers around minimizing images using the kraken.io image optimization web API. However, it isn’t a 1-on-1 representation. The module focuses on batch operations, compressing existing image assets both from local storage and the web.
Therefore, the Kraken-PowerShell module has a couple of unique features that aren’t available when using the REST API directly. Now, instead of talking, Let’s take a look at the one of the functions called Keeppath.
Note: You can find more information about kraken-powershell on the GitHub project page.
KeepPath keeps the original URL structure intact when compressing public images and saving them within Azure Blob Storage or Amazon S3. It’s easier to understand this by presenting an example;
https://kraken.io/assets/images/kraken-logotype.png (source) and will be stored as
https://kraken.s3.eu-central-1.amazonaws.com/assets/images/kraken-logotype.png without having to set the path manually.
Code example Amazon S3:
Azure Blob Storage:
You can use KeepPath to compress:
- Compress and move all your existing public image assets to AWS or Azure.
- Compress existing Azure Blob or AWS S3 image assets (overwriting the original).
- Compress and copy your image assets from Azure Blob to AWS S3 and vice versa.
Note: Azure’s Blob Storage doesn’t support virtual-hosted–style URLs, (Which means that the Container name is always part of the URL). -KeepPath and therefore automatically detects when the container name should be ignored.
Metadata & Headers
Given that Kraken-PowerShell focuses on batch operations, it’s important to know how metadata and headers can be provided when you are using Azure Blob Storage or AWS as your storage provider.
Note: All existing metadata will be lost if not resubmitted within your update call!
Batch Level data
The Metadata and Headers parameters are used in conjunction with
-FilePath. Since Metadata and Headers are specified at the global batch level, all processed items will be created using the same data.
Image level data
It’s possible to specify unique headers and or metadata If your scenario requires unique headers and or metadata information for each image. To do this, it’s necessary to use the
-OptimizeImageItems parameter. This parameter accepts a collection of the item type
Finally, the following samples are for Azure Blob storage only; however, the Amazon S3 cmdlet does precisely the same. Just make sure to uses the AWS Tools for Windows PowerShell for querying your storage buckets.
Process all items for a given container.
Only process new items for a given container.
Only process new items and keep all existing metadata.