Updating app to use Azure Storage blob Containers instead of kentico attachment not working

Leo Rockswold asked on March 18, 2024 17:45

Updating app to use Azure Storage blob Containers instead of kentico attachment not working

Overview: In the past, files were uploaded thru the web app to kentico directly. However, new requirements have come


to "virus scan" the files before upload.

To do this, we reconfiguring kentico so that the storage is azure storage. This will allow azure to scan the files as they are uploaded.

However, when doing so, we are running into issues.

Here is the code inside the webapp.

var postedfile = Helpers.FileHelper.ConstructHttpPostedFile(Helpers.FileHelper.ToByteArray(model.DocFile.InputStream), model.DocFile.FileName, model.DocFile.ContentType);

IUploadedFile uploadedFile =  postedfile.ToUploadedFile();

DocumentAttachment ai =  DocumentHelper.AddAttachment(kenticofile, "DocFile", uploadedFile);

//ai.Update();

kenticofile.Update(false);

DocumentHelper.UpdateAttachment(kenticofile, kenticofile.Fields.DocFile);
                kenticofile.Update(false);

Disclaimer: this code has been thru many iterations trying to fix this problem, so there might be some unnecessary code in here.

We have been following this documentation: https://docs.kentico.com/13api/content-management/attachments

The problem When uploading a file thru the app, the file gets attached to the kentico field on the page, but the file does not show up in azure.

To fix this, we go to the page in kentico with the attachment, the attachment is showing on the page, we click the "edit" button to edit the image on the page, we don't make any changes, and we then click "save".

After clicking "save" the file shows up in azure storage and download works as expected.

What can be done so we no longer have to go into kentico and "save" the image to make it show up in azure.

Correct Answer

Brenden Kehren answered on March 18, 2024 21:51

Willing to bet you don't have your attachemnt processing set up in your custom Azure storage provider. Can you share your custom Azure storage provider code? You need to map the storage path for the files similar to how you do it for the media files. I'd suggest checking this documentation out to find out where files are stored in the file system and then mapping them accordingly.

1 votesVote for this answer Unmark Correct answer

Recent Answers


Gary Gill answered on March 19, 2024 21:55 (last edited on March 19, 2024 21:57)

Here is the code:

[assembly: RegisterModule(typeof(CustomInitializationModule))]

public class CustomInitializationModule : Module
{
    // Module class constructor, the system registers the module under the name "CustomInit"
    public CustomInitializationModule()
        : base("AzureBlobStorage")
    {
    }

    // Contains initialization code that is executed when the application starts
    protected override void OnInit()
    {
        base.OnInit();

        // Creates a new StorageProvider instance for Azure
        var mediaProvider = StorageProvider.CreateAzureStorageProvider();

        // Specifies the target container
        mediaProvider.CustomRootPath = "oewebappsblob-dev";

        // Makes the container publicly accessible
        mediaProvider.PublicExternalFolderObject = true;

        // Maps the local media library directory to the storage provider
        StorageHelper.MapStoragePath("~/MarketConnect/files/", mediaProvider);
    }
}
0 votesVote for this answer Mark as a Correct answer

Leo Rockswold answered on March 19, 2024 22:24

FYI, Gary (who posted the code above) is one of the developers working on this with me, so see the "custom Azure storage provider" code above in his comment.

0 votesVote for this answer Mark as a Correct answer

Brenden Kehren answered on March 22, 2024 15:26

Our code looks different and supports multiple sites in one instance. When we use Azure storage, we use it for all areas, not just one or two. See below:

public class CustomAzureStorage : Module
{
    public CustomAzureStorage()
        : base("CustomerName.Custom.CustomAzureStorage") { }

    protected override void OnInit()
    {
        base.OnInit();
        var sites = SiteInfo.Provider.Get().WhereEquals("SiteStatus", "RUNNING");

        // loop through all sites in this instance and create a new provider for each one
        foreach (SiteInfo site in sites)
        {
            // Creates a new StorageProvider instance for media files
            AbstractStorageProvider mediaProvider = CustomAzureStorage.CreateAzureStorageProvider();

            // Maps a directory to the provider
            string path = string.Format("/{0}/media", site.SiteName);
            StorageHelper.MapStoragePath("~" + path, mediaProvider);

            // Creates a new StorageProvider instance for files
            AbstractStorageProvider fileProvider = CustomAzureStorage.CreateAzureStorageProvider();

            string filePath = string.Format("/{0}/files", site.SiteName);
            StorageHelper.MapStoragePath("~" + filePath, fileProvider);

            // Creates a new StorageProvider instance for bizform files
            AbstractStorageProvider bizFormFileProvider = CustomAzureStorage.CreateAzureStorageProvider();

            string bizFormFilePath = string.Format("/{0}/bizformfiles", site.SiteName);
            StorageHelper.MapStoragePath("~" + bizFormFilePath, bizFormFileProvider);

            // Creates a new StorageProvider instance for smart search files
            AbstractStorageProvider smartSearchProvider = CustomAzureStorage.CreateAzureStorageProvider();
            StorageHelper.MapStoragePath("~/App_Data/CMSModules/SmartSearch", smartSearchProvider);
        }
    }

    public static AbstractStorageProvider CreateAzureStorageProvider()
    {
        // Creates a new StorageProvider instance
        AbstractStorageProvider azureStorageProvider = new StorageProvider("Azure", "CMS.AzureStorage");

        // Specifies the target container
        azureStorageProvider.CustomRootPath = ValidationHelper.GetString(SettingsHelper.AppSettings["CMSAzureRootContainer"], "");

        // Makes the container publicly accessible
        azureStorageProvider.PublicExternalFolderObject = ValidationHelper.GetBoolean(SettingsHelper.AppSettings["AzureIsStoragePublic"], false);

        return azureStorageProvider;
    }
}
0 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.