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

回眸只為那壹抹淺笑 提交于 2019-12-01 15:25:29

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/6915424/azure-storage-blob-types-cloudblobcontainer-cloudblobclient-etc-and-thread

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!