Skip to content

Instantly share code, notes, and snippets.

@chris-hmmr
Last active July 6, 2022 17:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chris-hmmr/9649759dc0dcd2bd427382ed643ed490 to your computer and use it in GitHub Desktop.
Save chris-hmmr/9649759dc0dcd2bd427382ed643ed490 to your computer and use it in GitHub Desktop.
Sitecore 9.3+/10.x Azure Blob Storage Provider for Sitecore Forms, using Azure.Storage v12 (based on deprecated v11 implementation by bverdonck)
using System;
using System.Collections.Generic;
using System.IO;
using Azure;
using Azure.Storage.Blobs;
using Azure.Storage.Blobs.Models;
using Sitecore.ExperienceForms.Data;
using Sitecore.ExperienceForms.Data.Entities;
namespace Feature.Forms.Extensions
{
public class AzureBlobStorageFileStorageProvider : IFileStorageProvider
{
private const string OriginalFileNameKey = "originalFileName";
private const string CommitedKey = "commited";
private BlobServiceClient _blobServiceClient;
public AzureBlobStorageFileStorageProvider()
{
_blobServiceClient = new BlobServiceClient(Sitecore.Configuration.Settings.GetSetting("Feature.Feature.FormsExtensions.FileStorageProviders.AzureBlobStorageFileStorageProvider.ConnectionString"));
}
private string BlobContainer => Sitecore.Configuration.Settings.GetSetting("Feature.Feature.FormsExtensions.FileStorageProviders.AzureBlobStorageFileStorageProvider.BlobContainer");
private string Folder => Sitecore.Configuration.Settings.GetSetting("Feature.Feature.FormsExtensions.FileStorageProviders.AzureBlobStorageFileStorageProvider.Folder");
public Guid StoreFile(Stream file, string fileName)
{
var id = Guid.NewGuid();
var fileReference = GetFileReference(id);
var blobContainer = _blobServiceClient.GetBlobContainerClient(BlobContainer);
var blobClient = blobContainer.GetBlobClient(fileReference);
try
{
blobClient.Upload(file);
AddBlobMetadata(blobClient, new Dictionary<string, string>() {
{OriginalFileNameKey, fileName}, {CommitedKey, bool.FalseString}
});
}
catch (RequestFailedException e)
{
Log.WriteErrorToLog($"HTTP error code {e.Status}: {e.ErrorCode}");
Log.WriteErrorToLog(e.Message);
}
return id;
}
public Sitecore.ExperienceForms.Data.Entities.StoredFile GetFile(Guid fileId)
{
var blockClient = GetBlobClient(fileId);
var storedFile = new Sitecore.ExperienceForms.Data.Entities.StoredFile
{
FileInfo = new StoredFileInfo
{
FileId = fileId,
FileName = GetMetadata(blockClient, OriginalFileNameKey)
},
File = GetFileStream(blockClient)
};
return storedFile;
}
private string GetMetadata(BlobClient blockClient, string key)
{
BlobProperties properties = blockClient.GetProperties().Value;
return properties.Metadata[key];
}
public void DeleteFiles(IEnumerable<Guid> fileIds)
{
foreach (var fileId in fileIds)
{
var blobClient = GetBlobClient(fileId);
blobClient.DeleteIfExists(DeleteSnapshotsOption.IncludeSnapshots);
}
}
public void CommitFiles(IEnumerable<Guid> fileIds)
{
foreach (var fileId in fileIds)
{
var blobClient = GetBlobClient(fileId);
BlobProperties properties = blobClient.GetProperties().Value;
var metaData = properties.Metadata;
metaData[CommitedKey] = bool.TrueString;
blobClient.SetMetadata(metaData);
}
}
public void Cleanup(TimeSpan timeSpan)
{
throw new NotImplementedException();
}
private string GetFileReference(Guid id)
{
return Path.Combine(Folder, id.ToString());
}
private static Stream GetFileStream(BlobClient blobClient)
{
var fileStream = new MemoryStream();
try
{
blobClient.DownloadTo(fileStream);
fileStream.Position = 0;
}
catch (RequestFailedException e)
{
Log.WriteErrorToLog($"HTTP error code {e.Status}: {e.ErrorCode}");
Log.WriteErrorToLog(e.Message);
}
return fileStream;
}
private BlobClient GetBlobClient(Guid fileId)
{
var blobContainer = _blobServiceClient.GetBlobContainerClient(BlobContainer);
return blobContainer.GetBlobClient(GetFileReference(fileId));
}
private static void AddBlobMetadata(BlobClient blobClient, Dictionary<string, string> metadata)
{
try
{
blobClient.SetMetadata(metadata);
}
catch (RequestFailedException e)
{
Log.WriteErrorToLog($"HTTP error code {e.Status}: {e.ErrorCode}");
Log.WriteErrorToLog(e.Message);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment