xartal
xartal

Reputation: 457

CloudBlockBlob: DownloadTextAsync and UploadTextAsync and Metadata/Properties

As in the Documentation azure doc or msdn you have to call FetchAttributesAsync() to populate a CloudBlockBlob's metadata.

I've noticed that the Metadata also gets populated when I call DownloadTextAsync() and is written while I call UploadTextAsync(). Is this safe to use as that it won't change in future versions of the storage sdk? I can't really find any documentation that confirms that behaviour.

Sample:

        var blob = container.GetBlockBlobReference("testblob");

        blob.Metadata["testdata"] = "set";

        await blob.UploadTextAsync("content");

        blob.Metadata["notset"] = "set";

        blob = container.GetBlockBlobReference("testblob");

        var content = await blob.DownloadTextAsync();
        // here content["testdata"] is set and content["notset"] is null

Thanks for information!

Upvotes: 1

Views: 2847

Answers (1)

Gaurav Mantri
Gaurav Mantri

Reputation: 136196

Let me try to explain what's happening with your code. See if that clarifies your doubts.

When you execute following code:

await blob.UploadTextAsync("content");

Essentially it calls Put Blob REST API. As you can see from the REST API documentation, any metadata elements you specify by using blob.Metadata["testdata"] = "set"; code gets passed on to the REST API in x-ms-meta-testdata header.

So when this code executes, the metadata is saved along with the blob.

When you execute the following line of code:

blob.Metadata["notset"] = "set";

Basically, the blob object's (which is of type CloudBlockBlob) Metadata property gets updated. This update happens only on the client side and nothing gets saved in the blob. In order to save it, you must call SetMetadataAsync.

Coming to the last part, when you execute following line of code:

var content = await blob.DownloadTextAsync();

Essentially it calls Get Blob REST API. As you can see from the REST API documentation, this operation also returns the metadata for the blob in response headers. Azure Storage Client library then updates the Metadata property of your blob using the data it received as response to the REST API call.

Upvotes: 7

Related Questions