Skip to navigation Skip to content
GeralexGR

Personal blog

  • Home
  • About
  • Contact
  • Home
  • About
  • Contact
Posted on March 16, 2022March 16, 2022 by geralexgr — 2 Comments

Write blob file data on Azure storage account – C# .NET

In this article I will demonstrate how you can write a simple .NET console application to write data inside a blob container on an Azure storage account.

Storage accounts support the below four data structures.

For the cases of an application, I had to write every x seconds data on a file in order to get the coordinates of an app. For this purpose I selected a storage account container on which I will store the Latitude and Longitude in a .json format.

Inside my storage account I created a container which I will use named application.

Inside application container I created a file named locations.json. This will be the file that will get updated from the mobile app.

My console app consists of two .cs files. One is the location class which will be my model for the serialization.

   public class Location
    {
        public Double Latitude { get; set; }    
        public Double Longitude { get; set; }
    }

And the main code which consists of two functions. The RetrieveBlobContents function can read the stream content of a requested file from the blob. As I have specific names for my scenario, I hard coded them on the application. My containerName is application and the file which I want to read is called locations.json

The WriteContentOnBlob function will serialize a random Location object and will update the file every 5 seconds. The result is accomplished by writing a file locally and pushing this local file on the blob storage. The content of the file will be first extracted on the current directory/data folder.

Program.cs

using System;
using System.Threading.Tasks;
using System.Configuration;
using System.Collections.Generic;
using System.Net;
using System.Timers;
using Azure.Storage.Blobs;
using Azure.Storage.Blobs.Models;
using System;
using System.IO;
using System.Threading.Tasks;
using System.Text;
using storageaccount;
using Newtonsoft.Json;
public class Program
{
public static readonly string BlobConnectionString = ""
public static readonly string containerName = "application";
public static readonly string fileName = "locations.json";
static async Task Main(string[] args)
{
//await RetrieveBlobContents();
while (true)
{
Thread.Sleep(5000);
await WriteContentOnBlob();
}
}
private static async Task RetrieveBlobContents()
{
BlobServiceClient blobServiceClient = new BlobServiceClient(BlobConnectionString);
BlobContainerClient containerClient = blobServiceClient.GetBlobContainerClient(containerName);
BlobClient blobClient = containerClient.GetBlobClient(fileName);
var response = await blobClient.DownloadAsync();
using (var streamReader = new StreamReader(response.Value.Content))
{
while (!streamReader.EndOfStream)
{
var line = await streamReader.ReadLineAsync();
Console.WriteLine(line);
}
}
}
private static async Task WriteContentOnBlob()
{
BlobServiceClient blobServiceClient = new BlobServiceClient(BlobConnectionString);
BlobContainerClient containerClient = blobServiceClient.GetBlobContainerClient(containerName);
BlobClient blobClient = containerClient.GetBlobClient(fileName);
Directory.CreateDirectory("./data");
string localPath = "./data/";
Random rnd = new Random();
Double _longtitude = rnd.NextDouble();
Double _latitude = rnd.NextDouble();
Location blobLocation = new Location();
blobLocation.Latitude = _latitude;
blobLocation.Longitude = _longtitude;
string json = JsonConvert.SerializeObject(blobLocation);
//blobClient.write(json);
string localFilePath = Path.Combine(localPath, fileName);
await File.WriteAllTextAsync(localFilePath, json);
await blobClient.UploadAsync(localFilePath, true);
Console.WriteLine("Latitude: {0} Longtitude: {1} ",_latitude,_longtitude);
}
}
view raw azure-storageaccount-demo hosted with ❤ by GitHub

By running the application the values will be replaced every time on the locations.json file.

Share article

  • Share
  • Click to share on Facebook (Opens in new window)
  • Click to share on Twitter (Opens in new window)
  • Click to share on LinkedIn (Opens in new window)
  • Click to share on Pocket (Opens in new window)
  • Click to share on Reddit (Opens in new window)
  • Click to email a link to a friend (Opens in new window)
Categories: Azure, C# & .NET, Cloud
Tags: .net, account, Azure, blob storage c#, c#, containers, file, storage account, uploadAsync blob, write data, write storage account

Post navigation

Previous post: Insert and retrieve data on Azure cosmos DB – C# SDK
Next post: Push multiple docker container images using a loop – Azure DevOps

2 thoughts on “Write blob file data on Azure storage account – C# .NET”

  1. Ashley Alex
    November 21, 2022

    wrong code , no need to create an intermediate file to upload . misleading

    Reply
    1. geralexgr
      November 21, 2022

      There is no need to create an intermediate file, but in my scenario I wanted to have this file stored in a storage account in order to have the latest latitude and longitude available for clients. You should not mention a “misleading code” , it depends on your case and what you need to implement.

      Reply
Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Mastering Azure Devops CI/CD Pipelines with YAML
Learn how to create advanced automation scenarios using YAML and Azure Devops: https://www.udemy.com/course/mastering-azure-devops-cicd-pipelines-with-yaml
Youtube
Subscribe on my YouTube channel for more tutorials:
Azure DevOps platform Fundamentals – Build CI/CD pipelines
Start your journey with Azure DevOps platform: https://www.udemy.com/course/azure-devops-platform-fundamentals-build-cicd-pipelines/
Adblock notice
Support this blog to maintain its operational costs by turning off Adblock or donate a small amount using the button below
Blog Stats
  • 135,703 Views

Join 6 other subscribers
Recent Posts
  • Pass variables values inside terraform modules
  • Install linux azure devops agent on docker container
  • Install windows azure devops agent on docker container
  • Containerize a .NET app with Docker and vs code
  • error NU1202: Package PowerShell 7.3.0 is not compatible with net6.0
Top Posts
  • Pass parameters from build to release pipelines on Azure devops
  • Trigger Azure Devops build pipelines using REST API
  • Error MSB3073 The command "npm run build" exited with code 1 - Visual Studio
  • Build pipeline on tag push - Azure DevOps build triggers
  • Parameters and variables - GitHub workflows
  • Ansible loop over nested dictionary subelements - list object has no attribute
  • Trigger azure Devops pipeline from another repository
  • Azure DevOps best practices - jobs and stages
  • Build triggers on Azure devops pipelines
  • Pass secrets as ENV variables on docker build - Azure devops
Categories
  • Ansible
  • Automation
  • Azure
  • BotFramework
  • C# & .NET
  • Cloud
  • Devops
  • Docker
  • Github
  • Infrastructure
  • Kubernetes
  • Linux
  • Scripts
  • Security
  • Terraform
  • Testing
  • UWP apps – Windows 10
  • VMware
  • Web Development
  • Windows
  • Xamarin
Tags
.net agent ansible apache appservice automation az cli Azure azuredevops azure devops build c# centos cloud container containers deploy devops docker files github group identity install linux Mysql parameters PHP pipeline pipelines postman powershell redhat repository rest security terraform users variables visual studio vmware web app windows xamarin xamarin.forms
Archives
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022
  • August 2022
  • July 2022
  • June 2022
  • May 2022
  • April 2022
  • March 2022
  • February 2022
  • January 2022
  • November 2021
  • October 2021
  • September 2021
  • August 2021
  • July 2021
  • June 2021
  • May 2021
  • April 2021
  • March 2021
  • February 2021
  • November 2020
  • October 2020
  • September 2020
  • August 2020
  • July 2020
  • June 2020
  • May 2020
  • April 2020
  • March 2020
  • December 2019
  • August 2019
  • April 2019
  • March 2019
  • October 2018
  • September 2018
  • August 2018
  • July 2018
  • March 2018
  • July 2017
  • May 2017
  • February 2016
  • October 2015
  • August 2015
  • February 2015
  • November 2014
  • April 2014
  • March 2014
  • February 2014
  • January 2014
Privacy & Cookies: This site uses cookies. By continuing to use this website, you agree to their use.
To find out more, including how to control cookies, see here: Cookie Policy
Check out my Azure DevOps Udemy course
© GeralexGR 2023
Built with Storefront.