Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Write text to a file in Azure storage

I have a text file uploaded in my Azure storage account.Now, in my worker role , what i need to do is every time it is run, it fetches some content from Database, and that content must be written in the Uploaded text file, specifically , each time the content of Text file should be overwritten with some new content.

Here, they have given a way to upload a text file to your storage and also delete a file.But i don't want to do that, need to just MODIFY the already present text file each time.

like image 834
Spandan Avatar asked Feb 17 '23 06:02

Spandan


1 Answers

I'm assuming you're referring to storing a file in a Windows Azure blob. If that's the case: A blob isn't a file system; it's just a place to store data (and the notion of a file is a bit artificial - it's just... a blob stored in a bunch of blocks).

To modify the file, you would need to download it and save it to local disk, modify it (again, on local disk), then do an upload. A few thoughts on this:

  • For this purpose, you should allocate a local disk within your worker role's configuration. This disk will be a logical disk, created on a local physical disk within the machine your vm is running on. In other words, it'll be attached storage and perfect for this type of use.
  • The bandwidth between your vm instance and storage is 100Mbps per core. So, grabbing a 10MB file, while on a Small instance, would take maybe a second. On an XL, maybe around a tenth of a second. really fast, and varies with VM series (A, D, G) and size.
  • Because your file is in blob storage, if you felt so inclined to do so (or had the need for this), you could take a snapshot prior to uploading an updated version. Snapshots are like link-lists to your stored data blocks. And there's no cost to snapshots until, one day, you make a change to existing data (and now you'd have blocks representing both old and new data). An excellent way to preserve versions of a blob on a blob-by-blob basis (and it's trivial to delete snapshots).

Just to make sure this download/modify/upload pattern is clear, here's a very simple example (I just typed this up quickly in Visual Studio but haven't tested it. Just trying to illustrate the point):

        // initial setup
        var acct = CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("StorageConnectionString"));
        var client = acct.CreateCloudBlobClient();

        // what you'd call each time you need to update a file stored in a blob
        var blob = client.GetContainerReference("mycontainer").GetBlockBlobReference("myfile.txt");
        using (var fileStream = System.IO.File.OpenWrite(@"path\myfile.txt"))
        {
            blob.DownloadToStream(fileStream);
        }

        // ... modify file...

        // upload modified file
        using (var fileStream = System.IO.File.OpenWrite(@"path\myfile.txt"))
        {
            blob.UploadFromStream(fileStream);
        }
like image 187
David Makogon Avatar answered Feb 25 '23 09:02

David Makogon