Home > Software design >  Kentico multiple media libraries linked to multiple Azure containers
Kentico multiple media libraries linked to multiple Azure containers

Time:07-01

I have a requirement from the client to create 2 Media Libraries and Link them to 2 different Azure storage containers. I tried the following code but it still saves the files into the same container. Doesn't matter to which media library I upload files to, it always saves to the same container (ConfigurationManager.AppSettings["CMSAzureContainerName"]).

    string[] subDirectories = new string[] { "cms", "memberdocuments" };

    if (subDirectories != null)
    {
        for (int i = 0; i < subDirectories.Length; i  )
        {
            if (!string.IsNullOrEmpty(ConfigurationManager.AppSettings["CMSAzureMemberDocsContainerName"]) && 
                subDirectories[i].ToLower().Contains("memberdocuments"))
            {
                var mediaProvider = StorageProvider.CreateAzureStorageProvider();
                mediaProvider.CustomRootPath = ConfigurationManager.AppSettings["CMSAzureMemberDocsContainerName"];
                mediaProvider.PublicExternalFolderObject = false;
                StorageHelper.MapStoragePath("~/rthealth", mediaProvider);
            }
            else if (!string.IsNullOrEmpty(ConfigurationManager.AppSettings["CMSAzureAccountName"]))
            {
                var mediaProvider = StorageProvider.CreateAzureStorageProvider();
                mediaProvider.CustomRootPath = ConfigurationManager.AppSettings["CMSAzureContainerName"];
                mediaProvider.PublicExternalFolderObject = false;
                StorageHelper.MapStoragePath("~/rthealth", mediaProvider);
            }
        }
    }

Any tips to fix this?

CodePudding user response:

You need to create new storage provider instance for each folder. Something like:

string contentContainer = "container1";

        // Creates a new StorageProvider instance
        var coreStorageProvider = new StorageProvider("Azure", "CMS.AzureStorage")
        {
            // Specifies the target container which should represent the site/codebase
           
            CustomRootPath = contentContainer
        };

        // Maps a directory to the provider
        StorageHelper.MapStoragePath("~/foo1", coreStorageProvider);

string contentContainer = "container2";

        // Creates a new StorageProvider instance
        var coreStorageProvider = new StorageProvider("Azure", "CMS.AzureStorage")
        {
            // Specifies the target container which should represent the site/codebase
           
            CustomRootPath = contentContainer
        };

        // Maps a directory to the provider
        StorageHelper.MapStoragePath("~/foo2", coreStorageProvider);
  • Related