Skip to content

Instantly share code, notes, and snippets.

@angelovstanton
Created August 24, 2021 12:51
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 angelovstanton/833eb22d4161372a8e6ae80b6e356f4e to your computer and use it in GitHub Desktop.
Save angelovstanton/833eb22d4161372a8e6ae80b6e356f4e to your computer and use it in GitHub Desktop.
// <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