Skip to content

Instantly share code, notes, and snippets.

@RachidAZ
Created January 16, 2022 18:04
Show Gist options
  • Save RachidAZ/66e4ed3c443b0dcc169e1f3cfd81e8ef to your computer and use it in GitHub Desktop.
Save RachidAZ/66e4ed3c443b0dcc169e1f3cfd81e8ef to your computer and use it in GitHub Desktop.
read azure blob as stream
using Azure.Storage.Blobs;
using System.IO;
static internal MemoryStream GetBlobStream(string connectionString, string blobContainer, string blobName)
{
BlobServiceClient blobServiceClient = new BlobServiceClient(connectionString);
BlobClient blobClient = new BlobClient(connectionString, blobContainer, blobName);
var memoryStream = new MemoryStream();
blobClient.DownloadTo(memoryStream);
return memoryStream;
}
static internal List<string> ParseFileStream(MemoryStream memoryStream, string fileName)
{
string[] fileNameSplit = fileName.Split(".");
string fileType = fileNameSplit[fileNameSplit.Length-1];
if (fileType.ToUpper().Equals("CSV"))
{
memoryStream.Seek(0, SeekOrigin.Begin);
TextFieldParser parser = new TextFieldParser(memoryStream);
parser.TextFieldType = FieldType.Delimited;
parser.SetDelimiters(",");
// get header:
string[] header = parser.ReadFields();
while (!parser.EndOfData)
{
// handle the file lines here
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment