Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Azure Storage Blob types (CloudBlobContainer, CloudBlobClient, etc.) and thread safety

I am developing an azure application which needs at some point to upload(download) a large amount of small blobs to a single container (more than 1k blobs, less than 1 Mb each). In order to speed up this process I'd like to use multiple threads for uploading(downloading) blobs.

This is routine I use for uploading single blob:

CloudStorageAccount storageAccount = CloudStorageAccount.Parse(ConnectionString);
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
CloudBlobContainer blobContainer = 
    blobClient.GetContainerReference(ContainerName);
blobContainer.CreateIfNotExist();

CloudBlob blob = blobContainer.GetBlobReference(Id);
blob.UploadByteArray(Data);

For each type used in the code above MSDN says following:

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Does it mean that I need to execute following code in every thread? Or maybe I can execute it only once and share single instance of CloudBlobContainer among different threads?

CloudStorageAccount storageAccount = CloudStorageAccount.Parse(ConnectionString);
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
CloudBlobContainer blobContainer = 
    blobClient.GetContainerReference(ContainerName);

I would be really happy to use single instance of CloudBlobContainer in different threads otherwise it seriously slows down the whole uploading(downloading) process.

like image 523
monofilm Avatar asked Aug 02 '11 16:08

monofilm


1 Answers

You should be fine sharing a single blob container reference as long as you are not trying to perform an update on the container itself (even then, I think it would still be fine in most scenarios like List). In fact, you don't really even need the container reference if you are sure it exists:

client.GetContainerReference("foo").GetBlobReference("bar");
client.GetBlobReference("foo/bar");  //same

As you can see, the only reason to get a container reference is if you want to perform an operation on the container itself (list, delete, etc.). If you keep the blob references in separate threads, you will be fine.

like image 128
dunnry Avatar answered Oct 03 '22 20:10

dunnry