1. Home
  2. Data
  3. File Storage Services
  4. Storage – Azure Implementation

Storage – Azure Implementation

Configuration

Azure Implementation supports the Azure Container and BLOB, Azure Container and BOLB PRIVATE, and Azure File Share storage types. To setup a connection to this service you need the following:

Container and BLOB

For this storage type public or private, you will need to specify the URL to the storage location and the Connection String to the container/blob from Azure.

File Share

For this storage type you will need to specify the Connection String to the file share from Azure and you will need the specify the Share Name and Directory Path of the share.

Implementation

Container and BLOB

Save

public static async Task SaveBLOB(StorageInformation storageInformation)
{
    try
    {
        XElement storageXML = XElement.Parse(storageInformation.StorageDefinition);
        string connectionString = storageXML.GetElement("ConnectionString", "");
        string containerName = storageInformation.WorkspaceGUID.ToString();
        string fileType = storageInformation.IsFileCompressed ? "zip" : $"{storageInformation.FileType.ToLower()}";
        string blobName = $"{storageInformation.FileName.ToLower()}.{fileType}";
        string URL = storageXML.GetElement("URL", "");

        // Get a reference to the container, if it doesn't exist it will be created.
        BlobContainerClient container = new(connectionString, containerName);
        await container.CreateIfNotExistsAsync();

        // Set the access permissions of the container.
        if (storageInformation.IsFilePublic)
        {
            // The blob can be accessed by the public.
            await container.SetAccessPolicyAsync(PublicAccessType.Blob);
            storageInformation.FileURL = $"{URL}{storageInformation.WorkspaceGUID}/{blobName}";
        }
        else
        {
            // The blob can only be accessed by the owner.
            await container.SetAccessPolicyAsync(PublicAccessType.None);
            storageInformation.FileURL = $"PRIVATE:{URL}{storageInformation.WorkspaceGUID}/{blobName}";
        }

        // Get a reference to the specified BLOB in the container, if the blob exists it will overwrite it.
        BlobClient blob = container.GetBlobClient(blobName);
        await blob.DeleteIfExistsAsync();

        // Set the memory stream position back to the start and upload new BLOB.
        storageInformation.MemoryStream.Position = 0;
        await blob.UploadAsync(storageInformation.MemoryStream, new BlobHttpHeaders { ContentType = MimeTypes.MimeTypeMap.GetMimeType(storageInformation.FileType)});
    }
    catch (Exception)
    {
        storageInformation.ErrorMessage = "AzureStorage.SaveBLOB - Unknown error";
    }
}

Read

public static async Task ReadBLOB(StorageInformation storageInformation)
{
    try
    {
        XElement storageXML = XElement.Parse(storageInformation.StorageDefinition);
        string connectionString = storageXML.GetElement("ConnectionString", "");
        string containerName = storageInformation.WorkspaceGUID.ToString();
        string fileType = storageInformation.IsFileCompressed ? "zip" : $"{storageInformation.FileType.ToLower()}";
        string blobName = $"{storageInformation.FileName.ToLower()}.{fileType}";

        // Get a reference to the container
        BlobContainerClient container = new(connectionString, containerName);
        if (await container.ExistsAsync())
        {
            // Get a reference the the BLOB and download it
            BlobClient blob = container.GetBlobClient(blobName);
            if (await blob.ExistsAsync())
            {
                await blob.DownloadToAsync(storageInformation.MemoryStream);
            }
            else
            {
                storageInformation.ErrorMessage = $"BLOB {blobName} was not found";
            }
        }
        else
        {
            storageInformation.ErrorMessage = $"Container {containerName} was not found";
        }
    }
    catch (Exception)
    {
        storageInformation.ErrorMessage = "AzureStorage.ReadBLOB - Unknown error";
    }
}

File Share

Save

public static async Task SaveFileShare(StorageInformation storageInformation)
{
    try
    {
        XElement storageXML = XElement.Parse(storageInformation.StorageDefinition);
        string connectionString = storageXML.GetElement("ConnectionString", "");
        string shareName = storageXML.GetElement("ShareName", "");
        string dirName = storageXML.GetElement("DirectoryPath", "");
        string fileType = storageInformation.IsFileCompressed ? "zip" : $"{storageInformation.FileType.ToLower()}";
        string fileName = $"{storageInformation.FileName}.{fileType}";

        // Get a reference to the share
        ShareClient share = new(connectionString, shareName);
        if (await share.ExistsAsync())
        {
            // Get a reference to the directory
            ShareDirectoryClient directory = share.GetDirectoryClient(dirName);
            if (!await directory.ExistsAsync())
            {
                await directory.CreateAsync();
            }

            // Get a reference to the file and upload it. If a file already exists, delete it then upload the new file.
            ShareFileClient file = directory.GetFileClient(fileName);
            await file.DeleteIfExistsAsync();
            await file.CreateAsync(storageInformation.MemoryStream.Length);

            // Set the stream position back to the start for upload
            storageInformation.MemoryStream.Position = 0;
            await file.UploadRangeAsync(new HttpRange(0, storageInformation.MemoryStream.Length), storageInformation.MemoryStream);
        }
        else
        {
            storageInformation.ErrorMessage = $"File share {shareName} not found";
        }
    }
    catch (Exception)
    {
        storageInformation.ErrorMessage = "AzureStorage.SaveFileShare - Unknown error";
    }
}

Read

public static async Task ReadFileShare(StorageInformation storageInformation)
{
    try
    {
        XElement storageXML = XElement.Parse(storageInformation.StorageDefinition);
        string connectionString = storageXML.GetElement("ConnectionString", "");
        string shareName = storageXML.GetElement("ShareName", "");
        string dirName = storageXML.GetElement("DirectoryPath", "");
        string fileType = storageInformation.IsFileCompressed ? "zip" : $"{storageInformation.FileType.ToLower()}";
        string fileName = $"{storageInformation.FileName}.{fileType}";

        // Get a reference to the file share
        ShareClient share = new(connectionString, shareName);
        // Get a reference to the directory and check if it exists
        ShareDirectoryClient directory = share.GetDirectoryClient(dirName);
        if (await directory.ExistsAsync())
        {
            // Get a reference to the file and check if it exists
            ShareFileClient file = directory.GetFileClient(fileName);
            if (await file.ExistsAsync())
            {
                // Download the file
                ShareFileDownloadInfo download = await file.DownloadAsync();
                await download.Content.CopyToAsync(storageInformation.MemoryStream);
            }
            else
            {
                storageInformation.ErrorMessage = $"File {fileName} does not exist";
            }
        }
        else
        {
            storageInformation.ErrorMessage = $"File share directory {dirName} does not exist";
        }
    }
    catch (Exception)
    {
        storageInformation.ErrorMessage = "AzureStorage.ReadFileShare - Unknown error";
    }
}
Updated on January 2, 2024

Was this article helpful?

Related Articles

Need Support?
Can’t find the answer you’re looking for? Don’t worry we’re here to help!
Contact Support

Leave a Comment