Umbraco Cloud
CMSHeartcoreDXPMarketplace
  • What is Umbraco Cloud?
  • Frequently asked questions
  • Security
    • Web Application Firewall
  • Sustainability Best Practices
  • Getting Started
    • Explore Umbraco Cloud
    • The Cloud Portal
      • Organizations
      • Sustainability Dashboard
    • Project Overview
    • Environments
    • Flexible Environments (beta)
    • Baselines
      • Baseline Merge Conflicts
      • Break Reference between Baseline and Child Project
      • Handling configuration files
      • Pushing Upgrades to a Child Project
    • Plans
    • Migrate to Umbraco Cloud
    • Repositories in a Cloud Project
    • Best Practice for Working in Teams
    • Migrate between regions
  • Set up
    • Ready to Set Up Your Project?
    • Working with a Local Clone
      • Legacy Umbraco Visual Studio Setup
    • Manage Environments
    • Project Settings
      • Managing Transport Security
      • CDN Caching and Optimizations
      • Dedicated Resources
      • Upgrade your Plan
      • Public Access
      • Managing Hostnames
        • New Certificate Authority for custom hostnames
        • Rewrite rules
        • Custom Certificates
      • Management API Security
      • Umbraco CI/CD Flow
        • Cloud API For CI/CD Flow
        • Configuring a CI/CD pipeline
          • Azure DevOps
          • GitHub Actions
        • Troubleshooting
        • Known Limitations and Considerations
      • External Services
      • Usage
        • Bandwidth
      • Availability and Performance
      • Team Members
        • Technical Contact
      • Secrets Management
      • Project History
    • Private NuGet Feed on Umbraco Cloud
    • Going Live
    • Media
    • External Login Providers
    • Azure Blob Storage
      • Connect to Azure Storage Explorer to upload files manually
      • Connect and Upload Files Programmatically to Azure Blob Storage
    • Users
    • Multi-Factor Authentication
    • Application Insights
    • Config Transforms
    • SMTP Settings
    • Payments
      • Subscription migration information and FAQ
    • Power Tools (Kudu)
      • View the Files on your Cloud Environments
      • Generate UDA files
      • Manually run Extractions on your Cloud Environments
  • Deployments
    • Deployment
    • Deploying between environments
    • Transferring Content, Media, Members, and Forms
    • Deploying Deletions
    • Deployment Webhook
    • Deploying Changes
    • Umbraco Forms on Cloud
    • Deploy Dashboard
    • Hotfixes
      • Apply hotfix by manually moving files
      • Apply hotfix by using Git
    • Restoring Content
      • Partial Restores
  • Databases
    • Keep Your Data Secure and Accessible
    • Working with databases
    • Database backups
    • Database
      • Connecting to the Database on Mac
    • Working with a Cloud database locally
  • Product Upgrades
    • Stay Up to Date with Umbraco Cloud
    • Product Upgrades
    • Major Upgrades
    • Minor Upgrades
    • Version Specific Upgrades
      • Migrate from Umbraco 8 to the latest version
      • Migrate from Umbraco 7 to Umbraco 8 on Umbraco Cloud
    • Upgrade your projects manually
      • Manual upgrade of Umbraco CMS
      • Manual upgrade of Umbraco Deploy
    • Dependencies on Umbraco Cloud
  • Troubleshooting
    • Resolve Issues Quickly and Efficiently
    • Troubleshooting FAQ
    • Log files
    • The Umbraco Backoffice
    • The Frontend
    • The Umbraco Cloud Portal
    • Site Performance checklist
    • Troubleshooting deployments
      • Extraction error: Config transforms failing
      • Extraction error: Data Type collisions
      • Dependency Exception
      • Merge Conflicts on Flexible Environments
      • Troubleshooting deployments failing with no error message
      • Troubleshooting duplicate dictionary items
      • Troubleshooting language mismatches
      • Path too long Exception
      • Schema Mismatches
      • How to resolve collision errors
      • Extraction error: "Type not found! "
    • Cloud Errors
  • Release Notes
    • Overview 2025
      • April 2025
      • March 2025
      • February 2025
      • January 2025
    • Overview 2024
      • December 2024
      • November 2024
      • October 2024
      • September 2024
      • August 2024
      • July 2024
      • May 2024
      • April 2024
      • March 2024
      • February 2024
      • January 2024
    • Overview 2023
      • December 2023
      • October 2023
      • September 2023
      • August 2023
      • June 2023
      • May 2023
      • April 2023
      • March 2023
      • February 2023
      • January 2023
    • Overview 2022
      • December 2022
      • November 2022
      • September 2022
      • August 2022
      • June 2022
      • May 2022
      • April 2022
      • March 2022
      • February 2022
      • January 2022
Powered by GitBook
On this page
  • Getting the Azure Blob Storage credentials
  • Connecting programmatically to Azure Blob Storage
  • References

Was this helpful?

Edit on GitHub
Export as PDF
  1. Set up
  2. Azure Blob Storage

Connect and Upload Files Programmatically to Azure Blob Storage

There might be use cases, where you want to upload certain files to your Blob Storage programmatically rather than using Azure Storage Explorer.

PreviousConnect to Azure Storage Explorer to upload files manuallyNextUsers

Last updated 8 months ago

Was this helpful?

In this article, we provide the steps to programmatically connect to your Umbraco Cloud Environments Azure Blob Storage containers and persist files programmatically.

These files within the folder will only be available on Azure Storage and are not publicly visible in Umbraco CMS. The only exception is that the files that can be shared publicly via the *.blob.core.windows.net URL.

By the end of this article, you will have connected and uploaded a file to your Cloud Blob Storage.

You will need access to the Blob Storage credentials to authenticate and find the files created programmatically in the Azure Blob Storage.

An alternative to this guide is to use the package or MediaFileManager.FileSystem abstraction from the article.

Getting the Azure Blob Storage credentials

The first thing to do if you want to connect to the Azure Blob Storage container of your environment is the credentials.

To find the connection details for your environment's Blob Storage, follow the steps below:

  1. Go to your project on Umbraco Cloud.

  2. Go to Configuration in the side menu.

  3. Go to Connections.

  4. Scroll down to Blob Storage Connection Details

  5. Copy the credentials needed for connecting to Azure Blob Storage.

Connecting programmatically to Azure Blob Storage

Follow the steps below to get started connecting to Azure Blob Storage programmatically:

  1. Run your project.

  2. Run the project to complete the installation of the package.

  3. Add a new class called BlobStorageService which serves as a service that has a method to connect to Blob Storage:

BlobStorageService.cs
using Azure.Storage.Blobs;

namespace UmbracoProject
{
    public class BlobStorageService
    {
        public BlobContainerClient GetContainerClient(string connectionString, string containerName)
        {
            BlobServiceClient blobServiceClient = new BlobServiceClient(connectionString);
            BlobContainerClient containerClient = blobServiceClient.GetBlobContainerClient(containerName);
            return containerClient;

        }
    }
}
  1. Add a new class called BlobStorageComposer to inject the service:

BlobStorageComposer.cs
using Umbraco.Cms.Core.Composing;

namespace UmbracoProject;

public class BlobStorageComposer : IComposer
{
    public void Compose(IUmbracoBuilder builder)
    {
        builder.Services.AddScoped<BlobStorageService>();

    }
}
  1. Add a new class called BlobStorageController which serves as the Surface Controller:

BlobStorageController.cs
using Azure.Storage.Blobs;
using Microsoft.AspNetCore.Mvc;
using Umbraco.Cms.Core.Cache;
using Umbraco.Cms.Core.Logging;
using Umbraco.Cms.Core.Routing;
using Umbraco.Cms.Core.Services;
using Umbraco.Cms.Core.Web;
using Umbraco.Cms.Infrastructure.Persistence;
using Umbraco.Cms.Web.Website.Controllers;

namespace UmbracoProject;

public class BlobStorageController : SurfaceController
{

    private readonly BlobStorageService _blobStorageService;

    public BlobStorageController(
        IUmbracoContextAccessor umbracoContextAccessor,
        IUmbracoDatabaseFactory databaseFactory,
        ServiceContext services,
        AppCaches appCaches,
        IProfilingLogger profilingLogger,
        IPublishedUrlProvider publishedUrlProvider,
        BlobStorageService blobStorageService)
        : base(umbracoContextAccessor, databaseFactory, services, appCaches, profilingLogger, publishedUrlProvider)
    {
        _blobStorageService = blobStorageService;
    }

    // access the endpoint in backoffice via /umbraco/surface/BlobStorage/BlobUpdate
    public async Task<IActionResult> BlobUpdate()
    {

        string SASUrl = "Replace this with the Shared access signature URL (SAS) from Umbraco Cloud settings"; 
        string containerName = "Replace this with the Container Name from the Umbraco Cloud settings"; 

        string connectionString = $"BlobEndpoint={SASUrl}";

        BlobContainerClient containerClient = _blobStorageService.GetContainerClient(connectionString, containerName);

        string localPath = "data";
        Directory.CreateDirectory(localPath);
        string fileName = Guid.NewGuid().ToString() + ".txt";
        string localFilePath = Path.Combine(localPath, fileName);

        try
        {
            // Write some content to the file
            await using (StreamWriter writer = new StreamWriter(localFilePath))
            {
                await writer.WriteLineAsync("Hello, World! This file is created programmatically!");
            }

        }
        catch (Exception)
        {
            // ignored
        }

        // Get a reference to a blob
        string blobName = "FolderProgramatically/" + Guid.NewGuid().ToString() + ".txt"; //the blobName can be anything
        BlobClient blobClient = containerClient.GetBlobClient(blobName);

        Console.WriteLine("Uploading to Blob storage as blob:\n\t {0}\n", blobClient.Uri);

        // Upload data from the local file
        await blobClient.UploadAsync(localFilePath, true);

        return Content("Check your Blob Storage to see your new file!");
    }

}

The controller is used to create a directory named FolderProgramatically and a .txt file in Azure Blob Storage.

  1. Run the project.

  2. Visit the {{yourProjectURL}}/umbraco/surface/BlobStorage/BlobUpdate endpoint in the backoffice of your project to manually trigger the creation of the file to the Blob Storage.

Now you have connected to your Blob Storage programmatically you can extend it to fit your needs in terms of what you need to upload to the blob container.

References

For more information on how to work with Azure Blob Storage, see the following articles from Microsoft:

Clone down your Umbraco Cloud Project. You can find more information on how to clone a project in the article.

Install Azure.Storage.Blobs package on your project. You can do it either via NuGet Package Manager on Visual Studio or install it via .

In the above code, update the SASUrl and containerName values with your own from the Umbraco Cloud Settings. To find these values, refer to the instructions in the article.

You can also secure the values in Secrets Management in the project Settings on Umbraco Cloud so you do not store them in code. For more information, see the article.

and there you will find the folder and file that has been created programmatically:

Umbraco Storage Providers
Custom File Systems (IFileSystem)
Working Locally
NuGet
Secrets Management
Connect to your Blob Storage
Get started with Azure Blob Storage and .NET
Quickstart: Azure Blob Storage client library for .NET
Blob folder created programmatically
Blob folder created programmatically
Connect to Azure Storage Explorer to upload files manually