Connect and Upload Files Programmatically to Azure Blob Storage
Connect to Cloud environment's Azure Blob Storage programmatically.
Connect to Azure Blob Storage programmatically
This article provides the steps needed to programmatically connect to your Umbraco Cloud Environment's Azure Blob Storage containers, to persist files programmatically. You will need access to the Blob Storage credentials to authenticate and find the files created programmatically in the Azure Blob Storage.
By the end of this article you will have connected and uploaded a file to your Cloud Blob Storage. The list of the files within the folder will only be available on the Azure Storage so they are not visible publicly. This is with the exception of each individual file that can be shared publicly via the *.blob.core.windows.net URL.
Add a new class called BlobStorageController which serves as the Surface Controller:
BlobStorageController.cs
usingAzure.Storage.Blobs;usingMicrosoft.AspNetCore.Mvc;usingUmbraco.Cms.Core.Cache;usingUmbraco.Cms.Core.Logging;usingUmbraco.Cms.Core.Routing;usingUmbraco.Cms.Core.Services;usingUmbraco.Cms.Core.Web;usingUmbraco.Cms.Infrastructure.Persistence;usingUmbraco.Cms.Web.Website.Controllers;namespaceUmbracoProject;publicclassBlobStorageController:SurfaceController{privatereadonlyBlobStorageService _blobStorageService;publicBlobStorageController(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/BlobUpdatepublicasyncTask<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 fileawaitusing (StreamWriter writer =newStreamWriter(localFilePath)) {awaitwriter.WriteLineAsync("Hello, World! This file is created programatically!"); } }catch (Exception) { // ignored } // Get a reference to a blobstring blobName ="FolderProgramatically/"+Guid.NewGuid().ToString() +".txt"; //the blobName can be anythingBlobClient blobClient =containerClient.GetBlobClient(blobName);Console.WriteLine("Uploading to Blob storage as blob:\n\t {0}\n",blobClient.Uri); // Upload data from the local fileawaitblobClient.UploadAsync(localFilePath,true);returnContent("Check your Blob Storage to see your new file!"); }}
Here, the controller is used to create a directory named FolderProgramatically and a .txt file in Azure Blob Storage.
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:
References
For more information, see the following articles from Microsoft: