使CloudBlobContainer对象瞬态变好了吗?

时间:2014-09-29 08:30:50

标签: c# azure azure-storage-blobs

我有一个服务类,其中包含一个名为GetContainer的方法,

    private CloudBlobContainer GetContainer(string blobConnectionString, string blobContainerName)
    {
        var storageAccount = CloudStorageAccount.Parse(blobConnectionString);
        var blobClient = storageAccount.CreateCloudBlobClient();
        return blobClient.GetContainerReference(blobContainerName);
    }

现在从service方法每次需要执行像

这样的操作时都会调用此方法
    public Task CopyAsync(string blobConnectionString, string blobContainerName, string sourceFile, string destinationFile)
    {
        var container = GetContainer(blobConnectionString, blobContainerName);
        return CopyInternalAsync(container, sourceFile, destinationFile);
    }

    private Task CopyInternalAsync(CloudBlobContainer container, string sourceFile, string destinationFile)
    {
        var sourceBlob = container.GetBlockBlobReference(sourceFile);
        var destinationBlob = container.GetBlockBlobReference(destinationFile);
        return destinationBlob.StartCopyFromBlobAsync(sourceBlob);
    }

我可以像使用web-api示例一样使CloudBlobContainer单例执行here。但问题是我的blob连接字符串和容器名称没有修复。我应该使用ConcurrentDictionary还是将CloudBlobContainer瞬态化?

1 个答案:

答案 0 :(得分:3)

GetContainerReferenceGetBlockBlobReference都是非常轻量级的操作 - 在您实际执行上述StartCopyFromBlobAsync之类的操作之前,它们都不会与Azure进行对话。

请参阅https://github.com/Azure/azure-storage-net/blob/master/Lib/Common/Blob/CloudBlobClient.Common.cs#L288

public CloudBlobContainer GetContainerReference(string containerName)
{
    CommonUtility.AssertNotNullOrEmpty("containerName", containerName);
    return new CloudBlobContainer(containerName, this);
}

https://github.com/Azure/azure-storage-net/blob/master/Lib/Common/Blob/CloudBlobContainer.Common.cs#L82

internal CloudBlobContainer(string containerName, CloudBlobClient serviceClient) : this(new BlobContainerProperties(), new Dictionary<string, string>(), containerName, serviceClient)
{
}

internal CloudBlobContainer(BlobContainerProperties properties, IDictionary<string, string> metadata, string containerName, CloudBlobClient serviceClient)
{
    this.StorageUri = NavigationHelper.AppendPathToUri(serviceClient.StorageUri, containerName);
    this.ServiceClient = serviceClient;
    this.Name = containerName;
    this.Metadata = metadata;
    this.Properties = properties;
}

它只是创建一个对象并将其返回给您 - 将其视为瞬态不应该是一个问题。