Kentico CMS 7.0 Developer's Guide

Configuring storage providers

Configuring storage providers

Previous topic Next topic Mail us feedback on this topic!  

Configuring storage providers

Previous topic Next topic JavaScript is required for the print function Mail us feedback on this topic!  

Using different storage providers for specific parts of the application's file repository

 

CMS.IO allows you to access different sections of the application's file repository using different storage providers. For example, you can store media files in the Azure blob storage, while all other files stay in the default Windows file system.

 

1. Open the CMSHttpApplication.cs file from the App_Code\Application folder.

 

2. Add a using statement for CMS.IO.

 

3. In the Application_Start method, create a new instance of the StorageProvider class.

 

AbstractStorageProvider mediaProvider = new StorageProvider("Azure", "CMS.AzureStorage");

 

The value in the first parameter must match the value of the CMSExternalStorageName settings key value of the provider that you want to use. The value in the second parameter must match the assembly name of the provider. Use the constructor without parameters to create an instance of CMSStorage - the provider for Windows file system. See the table in Storage provider names and assemblies for values belonging to built-in storage providers.

 

4. Use the following code to map a directory to the provider you've instantiated in step 3.

 

StorageHelper.MapStoragePath("~/MySite/Media/", mediaProvider);

 

5. Save the file.

 

6. If you're using web application, rebuild the solution.

 

The application now uses the storage provider specified in step 3 to access the project folder specified in step 4.

 

Changing paths to content files

 

CMS.IO allows you to change default file paths. For example, when using the Windows file system, you can store media files on a different disk drive.

 

You utilize a similar procedure to store files in a different container in Azure storage or to store files in a different bucket on Amazon S3.

 

1. Open the CMSHttpApplication.cs file from the App_Code\Application folder.

 

2. Add a using statement for CMS.IO.

 

3. In the Application_Start method, create a new instance of the StorageProvider class.

 

AbstractStorageProvider mediaProvider = new StorageProvider();

 

Use the constructor without parameters to create an instance of CMSStorageProvider - the provider for Windows file system. Otherwise, the method expects two string parameters. The value in the first parameter must match the value of the CMSExternalStorageName settings key value of the provider that you want to use. The value in the second parameter must match the assembly name of the provider. See the table in Storage provider names and assemblies for values belonging to built-in storage providers.

 

4. Specify the target directory using the CustomRootPath property of the provider. This is the directory that you want to use instead of the default one.

 

mediaProvider.CustomRootPath = "D:\MySiteMedia";

 

5. (Optional) If the custom path is accessible via a URL (i.e. it is located on a file server), you can make the system access files via that URL. Insert the following code.

 

mediaProvider.CustomRootUrl = "http://www.example.com/MySiteMedia";

 

6. Use the following code to map a directory to the provider you've instantiated in step 3. This is the directory that you want to change.

 

StorageHelper.MapStoragePath("~/MySite/Media/", mediaProvider);

 

7. Save the file.

 

8. If you're using web application, rebuild the solution.

 

This code ensures that every time the application requests a file from the ~/MySite/Media directory, the file will be served from the location specified in the CustomRootPath property. If you specified the CustomRootUrl property, links to the file will point to that URL.

 

Storage provider names and assemblies

 

The following table lists storage providers built into Kentico CMS, the CMSExternalStorageName setting values associated with the providers, and the assemblies where the providers reside.

 

Storage provider

CMSExternalStorageName value

Assembly

Amazon S3

Amazon

CMS.AmazonStorage

Azure blob storage

Azure

CMS.AzureStorage

Windows file system (default provider)

-

CMS.CMSStorage