Azure存储Blob类型(CloudBlobContainer,CloudBlobClient等)和线程安全 [英] Azure Storage Blob types (CloudBlobContainer, CloudBlobClient, etc.) and thread safety

查看:453
本文介绍了Azure存储Blob类型(CloudBlobContainer,CloudBlobClient等)和线程安全的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个Azure应用程序,该应用程序有时需要将大量小Blob上传(下载)到单个容器(超过1k Blob,每个Blob小于1 Mb).为了加快此过程,我想使用多个线程来上传(下载)blob.

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.

这是我用于上传单个Blob的例程:

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);

对于MSDN上面的代码中使用的每种类型,请说以下:

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

此类型的任何公共静态(在Visual Basic中为Shared)成员都是 线程安全.不保证任何实例成员都是线程 安全.

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

这是否意味着我需要在每个线程中执行以下代码?或者也许我只能执行一次,并在不同线程之间共享一个CloudBlobContainer实例?

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);

我真的很高兴在不同线程中使用CloudBlobContainer的单个实例,否则会严重减慢整个上传(下载)过程.

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

推荐答案

只要您不尝试对容器本身执行更新,就应该很好地共享一个Blob容器引用(即使这样,我也认为这样做会在大多数情况下(如列表)仍然可以).实际上,如果确定容器引用存在,您甚至根本不需要容器引用:

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

如您所见,获取容器引用的唯一原因是是否要对容器本身执行操作(列表,删除等).如果将Blob引用保留在单独的线程中,就可以了.

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.

这篇关于Azure存储Blob类型(CloudBlobContainer,CloudBlobClient等)和线程安全的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆