简体   繁体   中英

Azure : Get classic storage account key using StorageManagementClient

I am trying to automatically retrieve the key of a classic Storage Account using StorageManagementClient.

For new ones, this worked fine client.storage_accounts.list_keys(resource_group_name, storage_account_name) but for classic ones I end up with the following error :

msrestazure.azure_exceptions.CloudError: Azure Error: ResourceNotFound
Message: The Resource 'Microsoft.Storage/storageAccounts/<storage_account_name>' under resource group '<resource_group_name>' was not found.

I did not find a way to also include classic resources in the documentation (see https://github.com/Azure/azure-sdk-for-python/tree/master/azure-mgmt-storage/azure/mgmt/storage ).

Do I simply have to use another API / client ?

You can't with this package, you need to use the ASM (Azure Service Management) package called azure-servicemanagement-legacy : https://pypi.org/project/azure-servicemanagement-legacy/

See this unittest from this testfile: https://github.com/Azure/azure-sdk-for-python/blob/master/azure-servicemanagement-legacy/tests/test_legacy_mgmt_storage.py

def test_get_storage_account_keys(self):
    # Arrange
    self._create_storage_account(self.storage_account_name)

    # Act
    result = self.sms.get_storage_account_keys(self.storage_account_name)

    # Assert
    self.assertIsNotNone(result)
    self.assertIsNotNone(result.url)
    self.assertIsNotNone(result.service_name)
    self.assertIsNotNone(result.storage_service_keys.primary)
    self.assertIsNotNone(result.storage_service_keys.secondary)
    self.assertIsNone(result.storage_service_properties)

Edit : You might want to try a ARM resource call using azure-mgmt-resource (see https://stackoverflow.com/a/45814624/4074838 for details) to this URL: https://management.azure.com/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.ClassicStorage/storageAccounts/{accountName}/listKeys?api-version=2015-06-01

You can create a ResourceManagementClient client client , and call client.resources.get_by_id(subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.ClassicStorage/storageAccounts/{accountName}/listKeys, '2015-06-01') As described in the answer I mention however, there is no guarantee this call will continue to work in the future (if it's even still working now)

Ref: https://docs.microsoft.com/en-us/python/api/azure.mgmt.resource.resources.v2017_05_10.operations.resourcesoperations?view=azure-python#get-by-id

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