简体   繁体   中英

Referencing services from CloudStorageAccount when migrating from WindowsAzure.Storage to Azure.Storage packages

I have a code-base that makes extensive use of the WindowsAzure.Storage nuget package for access to Queues, Tables and Blobs . The package is now flagged as deprecated with the indication that the functionality has been broken up into individual components under the Azure.Storage package-set.

This StackOverflow question and answer provides some description of the replacement packages but it's unclear how much of the restructuring is complete and what combination of legacy and new packages are required to migrate at this time.

I've been unable to find any up-to-date migration guide and example code/documentation for the new packages tends to focus on basic operations.

Specifically, I'm having difficulty accessing the new services from a top-level storage account.

The current code uses a pattern like this...

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

What would be the equivalent using the new packages and what combination of packages would I need?

You will need 3 separate Nuget packages:

  1. Azure.Storage.Blobs : For managing blobs
  2. Azure.Storage.Queues : For managing queues and
  3. Microsoft.Azure.Cosmos.Table : For managing tables.

As far as creating an instance of CloudStorageAccount is concerned, it is not available in Azure.Storage.Blobs and Azure.Storage.Queues. You will have to deal with it in a different manner. For Tables, CloudStorageAccount is available in Microsoft.Azure.Cosmos.Table namespace.

For example, the following code in old SDK

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

Need to be changed to something like:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM