This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// <copyright file="BlobStorageService.cs" company="Automate The Planet Ltd."> | |
// Copyright 2021 Automate The Planet Ltd. | |
// Unauthorized copying of this file, via any medium is strictly prohibited | |
// Proprietary and confidential | |
// </copyright> | |
// <author>Anton Angelov</author> | |
// <site>https://bellatrix.solutions/</site> | |
using System; | |
using System.Collections.Generic; | |
using System.IO; | |
using System.Text; | |
using System.Threading.Tasks; | |
using Microsoft.WindowsAzure.Storage; | |
using Microsoft.WindowsAzure.Storage.Blob; | |
namespace Bellatrix.Cloud.Infrastructure.Blob | |
{ | |
public class BlobStorageService | |
{ | |
private string _connectionString; | |
public BlobStorageService(string connectionString = null) | |
{ | |
_connectionString = connectionString ?? Environment.GetEnvironmentVariable("AZURE_BOT_STORAGE_CONNECTION_STRING"); | |
} | |
public string DownloadFileAsText(string fileName, string blobContainerName = "") | |
{ | |
var storageAccount = CloudStorageAccount.Parse(_connectionString); | |
var blobClient = storageAccount.CreateCloudBlobClient(); | |
// Get Blob Container | |
var container = blobClient.GetContainerReference(blobContainerName); | |
// Get reference to blob (binary content) | |
var pageBlob = container.GetBlobReference(fileName); | |
var resultText = string.Empty; | |
using (var ms = new MemoryStream()) | |
{ | |
pageBlob.DownloadToStreamAsync(ms).Wait(); | |
resultText = Encoding.UTF8.GetString(ms.ToArray()); | |
} | |
return resultText; | |
} | |
public async Task<string> UploadHtmlFileAsync(string content, string fileName, string blobContainerName = "") | |
{ | |
var storageAccount = CloudStorageAccount.Parse(_connectionString); | |
var blobClient = storageAccount.CreateCloudBlobClient(); | |
var container = blobClient.GetContainerReference(blobContainerName); | |
var pageBlob = container.GetBlockBlobReference($"{fileName}_{Guid.NewGuid()}.html"); | |
pageBlob.Properties.ContentType = "text/html"; | |
await pageBlob.UploadTextAsync(content); | |
return pageBlob.Uri.AbsoluteUri; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment