【问题标题】:Referencing services from CloudStorageAccount when migrating from WindowsAzure.Storage to Azure.Storage packages从 WindowsAzure.Storage 迁移到 Azure.Storage 包时从 CloudStorageAccount 引用服务
【发布时间】:2020-06-25 10:52:38
【问题描述】:

我有一个代码库,它广泛使用 WindowsAzure.Storage nuget 包来访问队列、表和 Blob。该包现在被标记为已弃用,并表明该功能已分解为 Azure.Storage 包集下的各个组件。

This StackOverflow question and answer 提供了一些替换包的描述,但目前尚不清楚重组完成了多少,以及目前需要迁移旧包和新包的哪种组合。

我一直找不到任何最新的迁移指南,新包的示例代码/文档往往侧重于基本操作。

具体来说,我在从顶级存储帐户访问新服务时遇到了困难。

当前代码使用这样的模式...

    var accountName = "...";
    var accountKey ="..............";
    var credentials = new StorageCredentials(accountName, accountKey);
    var account = new CloudStorageAccount(credentials,true);

    //for table access...
    var client = account.CreateCloudTableClient();
    var table = client.GetTableReference(tableName);
    
    //for queue access
    var client = account.CreateCloudQueueClient();
    var queue = client.GetQueueReference(queueName);
    
    //for blob access 
    var client = account.CreateCloudBlobClient();
    var container = client.GetContainerReference(containerName);
    var blob =   container.GetBlockBlobReference(path);
        

使用新软件包的等效方法是什么?我需要哪种软件包组合?

【问题讨论】:

    标签: c# azure


    【解决方案1】:

    您将需要 3 个单独的 Nuget 包:

    1. Azure.Storage.Blobs: 用于管理 blob
    2. Azure.Storage.Queues:用于管理队列和
    3. Microsoft.Azure.Cosmos.Table:用于管理表格。

    就创建CloudStorageAccount 的实例而言,它在 Azure.Storage.Blobs 和 Azure.Storage.Queues 中不可用。您将不得不以不同的方式处理它。对于表,CloudStorageAccount 在 Microsoft.Azure.Cosmos.Table 命名空间中可用。

    例如旧SDK中的如下代码

    var container = client.GetContainerReference(containerName);
    var blob =   container.GetBlockBlobReference(path);
    

    需要改成这样的:

    var blobContainerClient = new BlobContainerClient(connectionString, containerName);//Use this client to perform operations on blob container.
    var blockBlobClient = blobContainerClient.GetBlockBlobClient(blobName);//Use this client to perform operations on block blob.
    

    【讨论】:

    • 感谢 Gaurav,这非常有用。您的回答意味着不再可能从存储帐户“导航”到其中的 blob/队列。您知道这是新 API 中经过深思熟虑的设计决定,还是只是尚未移植此功能?
    • ...还有一点可以帮助澄清答案。新 API 中预期的“连接字符串”是存储帐户的连接字符串(来自门户)还是帐户的 blob/队列部分的某些连接字符串?
    • 关于您的第一条评论,我不是 Storage SDK 团队的一员,所以我会猜测,但我相信这是一个深思熟虑的设计决定,我认为我们不能指望存储帐户对象出现开发工具包。 FWIW,我个人认为新设计很糟糕。这是相当复杂和复杂的(等到你进入共享访问签名:))。
    • Is the "connection string" expected in the new API the connection string of the Storage Account (from the Portal) or some connection string for the blob/queue portion of the account? - 是的。您可以使用完整的连接字符串。如果在连接字符串中定义,每个 SDK 都会选择与其服务相关的相关端点。
    • 太好了 - 谢谢!是的,我们使用 SAS,所以听起来很有趣……;-)
    猜你喜欢
    • 2021-10-01
    • 1970-01-01
    • 2016-07-26
    • 1970-01-01
    • 1970-01-01
    • 2019-11-06
    • 2020-09-30
    • 2021-08-27
    • 2021-08-16
    相关资源
    最近更新 更多