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.
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:
Go to your project on Umbraco Cloud.
Go to Configuration in the side menu.
Go to Connections.
Scroll down to Blob Storage Connection Details
Copy the credentials needed for connecting to Azure Blob Storage.
Follow the steps below to get started connecting to Azure Blob Storage programmatically:
Clone down your Umbraco Cloud Project. You can find more information on how to clone a project in the Working Locally article.
Run your project.
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.
Run the project to complete the installation of the package.
Add a new class called BlobStorageService
which serves as a service that has a method to connect to Blob Storage:
Add a new class called BlobStorageComposer
to inject the service:
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.
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 Connect to Azure Storage Explorer to upload files manually 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 Secrets Management article.
Run the project.
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.
Connect to your Blob Storage and there you will find the folder and file that has been created programmatically:
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.
For more information on how to work with Azure Blob Storage, see the following articles from Microsoft:
Azure Blob Storage is an external storage system, that the Umbraco Cloud service uses to store all media files on Umbraco Cloud projects.
This includes everything that is added to the Media library through the Umbraco backoffice, like images, PDFs, and other document formats.
In some cases, you might want to store other files in the Blob Storage than Media files.
Below are two articles describing how to connect to your environment Blob Storage, and upload files either through Azure Storage Explorer or programmatically.
You can learn more about Azure Blob Storage in the Microsoft Documentation.
If you want to manually upload files to the Azure Blob Storage container provided to your Cloud environments, you can use "Microsoft Azure Storage Explorer" software.
This article provides the steps you need, to connect to your Azure Blob Storage containers using Azure Storage Explorer.
We strongly recommend that you add all the media items to your Cloud environments through the backoffice. Clone your environment to your local machine to manage the files of your media library.
Important: If you upload your media files manually using this method, they will not be available in the backoffice.
All media needs to be added through the Umbraco backoffice.
The first thing to sort out, 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:
Go to your project on Umbraco Cloud.
Go to Configuration in the side menu.
Go to Connections.
Scroll down to Blob Storage Connection Details
Copy down the credentials needed for connecting to Azure Blob Storage.
The next step is to have Azure Storage Explorer installed on your local computer. Download the storage explorer, and run through the installer.
Let's use the information you have gathered, and connect Azure Storage Explorer to the Blob storage container:
Click the Open Connect Dialog button to get the Connect dialogue.
Select the Blob container in the first prompt.
Select Shared access signature URL (SAS) in the second prompt.
Input the information you have gathered earlier in the following format [Endpoint][ContainerName][SharedAccessSignature]
, in the URI field. See below for an example.
Ensure that the credentials are correctly set in the Connection Summary prompt.
Select Connect.
You are now connected to the blob storage for your environment and you can upload your files to Azure Blob Storage through the explorer.
Important: If you upload your media files manually using this method, they will not be available in the backoffice.
All media needs to be added through the Umbraco backoffice.