4

After looking at this tutorial on blobs: channel 9, I was thinking of using a blob container to save a bunch of tweets (storing the json of each tweet that is). Ideally I would like to create a blob reference for each hour of the day, and append new tweets to this blob as they come in. The issue is that the method UploadText(string) overwrites the existing content of the blob, is there an easy way to append text to an existing blob ?

Thanks!

        fun (json:string) ->  
                    let account = CloudStorageAccount.Parse(RoleEnvironment.GetConfigurationSettingValue("DataConnectionString"))
                    let blobs = account.CreateCloudBlobClient();
                    let tempBlob = blobs.GetBlobReference("tweets/2010-9-26/17/201092617.txt")
                    tempBlob.Properties.ContentType <- "text/plain"
                    tempBlob.UploadText(json)
jlezard
  • 1,417
  • 2
  • 15
  • 32

3 Answers3

8

Azure now supports Append Blobs. When you create a new blob, you must define it as an Append Block. You can't append to existing block blobs.

Here's some simple code which you can use.

Append:

CloudAppendBlob appendBlob = container.GetAppendBlobReference("myblob.txt")
appendBlob.AppendText("new line");

Read:

appendBlob.DownloadText()

Technet contains a good tutorial on the subject. Also the official Azure documentation now includes help for using Append Blob.

Mikael Koskinen
  • 12,306
  • 5
  • 48
  • 63
4

Page Blobs are the way to go for this need. (vs block blobs)

You create the blob with a Put Blob operation: http://msdn.microsoft.com/en-us/library/dd179451.aspx

Then you can add "pages" using a Put page operation: http://msdn.microsoft.com/en-us/library/ee691975.aspx

Page Blobs will amend the page(s) added with a put immediately, more accurately mimicking traditional file systems.

Block blobs are going to expect a more ridig construction, and require a two-phase submit/commit construction. Once compiled, you have to overwrite to ammend to the blob. Block blobs are designed for streaming of static (loose definition) content, vs as a read/write store. Page Blobs were added to support those scenarios.

Taylor Bird
  • 7,767
  • 1
  • 25
  • 31
  • @tishon thanks for helping out. I'll give a try at page blobs – jlezard Sep 27 '10 at 21:38
  • The choice between page and block blobs is really more about optimizing for large chunks & sequential reading (block blobs) or small chunks and random access reading and writing (page blobs). You can indeed append blocks to a block blob, and re-commit the updated block list (you can also remove, insert, and replace). – Paul Wheeler Feb 08 '13 at 03:54
  • 1
    This answer is outdated with the introduction of Append Blobs. Use Append Blobs instead. – Tobias Feb 21 '18 at 19:02
1

You can try getting the list of committed blocks via the CloudBlockBlob.DownloadBlockList() method and then append the new content via CloudBlockBlob.PutBlock().

tishon
  • 650
  • 6
  • 14