gpt4 book ai didi

c# - 让 CloudBlobContainer 对象成为 transient 可以吗?

转载 作者:行者123 更新时间:2023-12-02 08:08:43 26 4
gpt4 key购买 nike

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

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

现在,每次需要执行类似操作时,服务方法都会调用此方法,

    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 暂时使用?

最佳答案

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

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

关于c# - 让 CloudBlobContainer 对象成为 transient 可以吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26095591/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com