How to access table storage from Managed API, both are under same subscription.

Dhaval Amin 20 Reputation points
2023-07-27T01:22:31.4466667+00:00

I am working on Managed API and need to access storage table from endpoint. My API and storage table both under same subscription.

Azure Table Storage
Azure Table Storage
An Azure service that stores structured NoSQL data in the cloud.
170 questions
Azure Storage Accounts
Azure Storage Accounts
Globally unique resources that provide access to data management services and serve as the parent namespace for the services.
3,149 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Will Chen 145 Reputation points
    2023-07-27T02:24:12.6666667+00:00

    To access Azure Table Storage from a Managed API (like Azure Functions or Azure App Service) under the same subscription, you can use Azure Managed Identities to secure the access. Here's an outline of how to do it:

    Step 1. Enable and Obtain the Managed Identity

    1. In the Azure portal, go to your API's resource blade (for example, the Azure Functions or App Service blade).
    2. Under the settings section, click on "Identity."
    3. Under the "System assigned" tab, switch the Status to "On" and save your changes.
    4. After the identity has been created, note down the "Object ID" as you will need it in the next step.

    Step 2. Grant Access to the Table Storage

    1. In the Azure portal, navigate to your Table Storage account.
    2. Under "Access control (IAM)", click "+ Add" and select "Add role assignment."
    3. In the "Add role assignment" panel, select the "Storage Blob Data Contributor" role. This role allows for read, write, and delete access to blob data (including tables).
    4. In the "Select" field, paste the "Object ID" you obtained in the previous step. It should resolve to the name of your API.
    5. Click "Save" to add the role assignment.

    Step 3. Access the Table Storage from the Managed API

    In your API, you can now use the Azure SDK to access your Table Storage without explicitly providing any connection strings. Azure will automatically use the Managed Identity to authenticate your requests.

    If you're using C# and the Azure.Storage.Tables library, it could look something like this:

    csharpCopy code
    var client = new TableServiceClient(new Uri("<your-table-storage-url>"), new DefaultAzureCredential());
    

    The DefaultAzureCredential() class automatically handles the authentication using Managed Identities if they are available.

    Please replace "<your-table-storage-url>" with the URL of your Azure Table Storage service. You can find this in the Azure portal under the "Properties" section of your Table Storage account.

    Please note that this approach requires that your API is running in Azure, as Managed Identities are not available when running locally. For local development, you can use your own credentials or a connection string, but make sure not to commit these to your code repository.

    Thanks!

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.