Configuring Optimizely Azure Blob Storage
Tags: optimizely, blobstorage, dotnetcore, episerver
Published: 2023-04-25 00:07:46

Working in web development you are more than likely will come across a conversation at some point around [image optimization](https://dev.to/paulmcgann/optimizely-dxp-image-optimization-2ip). 

In the world of [Optimizely](https://world.optimizely.com/), there are a number of tools / libraries to help with image optimization some of these include:

- Cloudflare - Used when working within the [Optimizely DXP](https://world.optimizely.com/products/#dxp), has great built in features for automatically optimizing images.

- [Responsive images](url) using imageresizer, for the older .net framework versions of Optimizely. Requires a bit more effort however the output can make a real difference to the sites performance.

- Responsive images CMS 12 using [picturerenderer](https://hacksbyme.net/2021/10/18/picturerenderer-for-optimizely-cms-12/)

All these solutions help with image optimization, however none of these will work if we have configured our blob storage setup incorrectly.

In the post I will focus on the last point [Optimizely CMS 12](https://world.optimizely.com/products/#contentmanagement) and configuring azure blobstorage provider.

The first thing you need to do it add a connectionstring to your appsettings.json file:

```json
"EPiServerAzureBlobs": "DefaultEndpointsProtocol=https;AccountName=your_account_name;AccountKey=your_account_key;"
```

Next in the your Startup.cs or where your configure services, you can now configure the azure blobstorage:

```csharp
services.AddAzureBlobProvider(o =>
        {
            o.ConnectionString = _configuration.GetConnectionString("EPiServerAzureBlobs");
            o.ContainerName = "your_mediafolder";
        });
```

This is the basic steps needed to configure the azure blobstorage. However we also should configure some image optimization.

There are a number of discussions around the web on how best to perform this and it is still in discussion as we read. One approach that is being used in the Optimizely world, is to use the follow package [Baaijte.Optimizely.ImageSharp.Web](https://github.com/vnbaaij/Baaijte.Optimizely.ImageSharp.Web), this builds upon the SixLabors.ImageSharp library already present in the Optimizely system.

To configure the Baaijte.Optimizely.ImageSharp.Web library perform the following:

Add the following to the configure service in your Startup.cs, as early as possible in the configuration.

```csharp
services.AddImageSharp()
            .Configure<AzureBlobStorageCacheOptions>(options =>
            {
                options.ConnectionString = _configuration.GetConnectionString("EPiServerAzureBlobs");
                options.ContainerName = "your_mediafolder";
            })
            .ClearProviders()
            .AddProvider<BlobImageProvider>()
            .SetCache<AzureBlobStorageCache>();
```

Note as part of this you will also need to install the 'SixLabors.ImageSharp.Web.Providers.Azure' package also.

Next you will need to configure the app by adding the following:

```csharp
 public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
        app.UseBaaijteOptimizelyImageSharp();
}
```

With all the configuration in place now you should now be able to render images from azure blobstorage and also be able to resize them appropriately.

Thanks for reading! Please feel free to comment and share your thoughts.