Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Azure blob storage - auto generate unique blob name

I am writing a small web application for Windows Azure, which should use the blob storage for, obviously, storing blobs.

Is there a function or a way to automatically generate a unique name for a blob on insert?

like image 378
Emiswelt Avatar asked Jan 14 '13 13:01

Emiswelt


3 Answers

You can use a Guid for that:

string blobName = Guid.NewGuid().ToString();
like image 66
Sandrino Di Mattia Avatar answered Oct 07 '22 14:10

Sandrino Di Mattia


There is nothing that generates a unique name "on insert"; you need to come up with the name ahead of time.

When choosing the name of your blob, be careful when using any algorithm that generates a sequential number of some kind (either at the beginning or the end of the name of a blob). Azure Storage relies of the name for load balancing; using sequential values can create contention in accessing/writing to Azure Blobs because it can prevent Azure from properly load-balancing its storage. You get 60MB/Sec on each node (i.e. server). So to ensure proper load-balancing and to leverage 60MB/Sec on multiple storage nodes you need to use random names for your blobs. I typically use Guids to avoid this problem, just as Sandrino is recommending.

like image 34
Herve Roggero Avatar answered Oct 07 '22 14:10

Herve Roggero


In addition to what Sandrino said (using GUID which have very low probability of being duplicated) you can consider some third-party libraries which generate conflict-free identifiers example: Flake ID Generator

EDIT

Herve has pointed out very valid Azure Blob feature which should be considered with any blob names, namely, Azure Storage load balancing and blobs partitioning.

Azure keeps all blobs in partition servers. Which partition server should be used to store particular blob is decided on the blob container and the blob file name. Unfortunately I was not able to find and documentation describing algorithm used for blobs partitioning.

More on Azure Blob architecture can be found on Windows Azure Storage Architecture Overview article.

like image 4
Tom Avatar answered Oct 07 '22 13:10

Tom