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.

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 Umbraco Storage Providers package or MediaFileManager.FileSystem abstraction from the Custom File Systems (IFileSystem) 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. Clone down your Umbraco Cloud Project. You can find more information on how to clone a project in the Working Locally article.

  2. Run your project.

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

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

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

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

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

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.

  3. Connect to your Blob Storage and there you will find the folder and file that has been created programmatically:

Blob folder created programmatically
Blob folder created programmatically

Now that you are connected to Blob Storage programmatically, you can customize it to suit your upload needs.

References

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

Last updated

Was this helpful?