Loading...

How To: Send requests to Azure Storage from Azure API Management

How To: Send requests to Azure Storage from Azure API Management

Overview

In this How To, I will show a simple mechanism for writing a payload to Azure Blob Storage from Azure API Management (APIM). Some examples where this approach is useful:

  • Claim-Check
    Claim-Check pattern is used to split a large message into a claim check and a payload. The claim check is sent to a downstream system which has a reference to the location of the payload. The downstream system then retrieves the payload for handling. You might want to do this when working with technology that are not optimised suited for very large messages (Service Bus) either by a size limitation or associated cost, or when you do not need to process all messages. 
  • Message Logging
    Application Insights is awesome, and though you can send payloads as part of your telemetry, this can result in a surprisingly high usage charge. As a way to address this, you might want to store the payloads in Azure Storage instead.

There are more scenarios of course, but those are the two top ones that jump out at me.

 

Example Use Case

In this example, I will amend the policy of an API to write the payload received to a storage account blob container. This example will show how to modify the payload as well as how to establish the required permission using service identity and RBAC permissions. This is my preferred way of handling authentication and authorisation when working with Azure Storage. API Keys and SAS tokens are possible but I find them challenging for most teams to manage and code.

 

Setup

This How To starts after an Azure Storage service and a APIM service have been provisioned. Thanks to the APIM team, the service provides a sample API, Echo API, that is suitable for a our purposes.

JeffreyChilberto_0-1663034183943.png

This example will update the POST operation Create resource. 

 

Permissions

The next step is to enable the APIM to access blob storage. To do this, navigate to the Managed identities blade:

JeffreyChilberto_1-1663034928154.png

You will want the System assigned on:

JeffreyChilberto_2-1663035127321.png

Using Azure role assignments, create a assignment for Storage Blob Data Contributor over your storage account. 

JeffreyChilberto_3-1663035264717.png

The entry above shows access for my APIM, tmp-apim-ase, over the sttempase Azure Storage account.

 

Policy

Now back on the Echo API, select the Create resource operation and click the Policy code editor:

JeffreyChilberto_4-1663035399536.png

 

This will provide an xml editor. Insert the following xml in the inboud element.

 

 

<set-variable name="BlobFileName" value="@{ var id = context.Api.Id; var operation = context.Operation.Id; var requestId = context.RequestId; return $"https://[yourstorage].blob.core.windows.net/apimlog/{id}/{operation}/{requestId}.txt"; }" /> <send-request mode="new" timeout="20" response-variable-name="blobdata" ignore-error="true"> <set-url>@((string)context.Variables["BlobFileName"])</set-url> <set-method>PUT</set-method> <set-header name="x-ms-version" exists-action="override"> <value>2019-07-07</value> </set-header> <set-header name="x-ms-blob-type" exists-action="override"> <value>BlockBlob</value> </set-header> <set-body>@( context.Request.Body.As<string>(preserveContent: true) )</set-body> <authentication-managed-identity resource="https://storage.azure.com" /> </send-request>

 

 

You will need to update the value for [yourstorage] to your Azure Blob Storage account. The result should look something like this:

JeffreyChilberto_5-1663035589740.png

A couple things to note. I set the file name to use the unique RequestId generated by APIM, and I created a folder structure to reflect the api and operation called. Have a look at the supported Context Variables to suit your purpose. 

Also note the ignore-error attribute is set to true. In my case, I do not want to fail the message but this might not be suitable if implementing the Claim-Check pattern. Also, when reading the request body, be sure to preserveContent. 

 

Test

Testing the API is nice and simple using the Test tab.

JeffreyChilberto_6-1663035940723.png

If the Azure gods are with you, you should not see an error and instead see a new file created in your storage account.

JeffreyChilberto_7-1663036010904.png

Now, what if something goes wrong, or if you are not familiar with the Trace feature, read on.

 

Troubleshooting

The best way to troubleshoot is using Trace. This is located next to the send button.

JeffreyChilberto_8-1663036207787.png

This provides more detail as to the steps involved. For example, you can investigate the step where the file is created:

JeffreyChilberto_9-1663036314861.png

Trace is your friend :smile:

 

Summary

This How To illustrated a simple way to publish to Azure Storage. There are other ways I explored including publishing the claim to Event Hub and the payload to the backend, but I thought this approach was simplest and made for a good How To. Let me know if this saves you some time!

 

Cheers

 

Published on:

Learn more
Azure Developer Community Blog articles
Azure Developer Community Blog articles

Azure Developer Community Blog articles

Share post:

Related posts

This Month in Azure Static Web Apps | 09/2024

    We are back with another edition of the Azure Static Web Apps Community! :party_popper:   September was yet another month ...

1 year ago

GitHub Copilot for Azure: 6 Must-Try Features

As developers, we are constantly seeking tools that streamline our workflows and boost productivity. … Enter GitHub Copilot for Azure, now in ...

1 year ago

Responsible AI Mitigation Layers

Generative AI is increasingly being used in various kinds of systems to augment humans and infuse intelligent behavior into existing and new a...

1 year ago

Streamline Your Azure Workflow: Introducing GitHub Copilot for Azure in VS Code

I'm excited to announce the public preview of GitHub Copilot for Azure - a new addition to your toolkit that seamlessly integrates with G...

1 year ago

Build Intelligent Apps Code-First with Prompty and Azure AI

      Building Generative AI applications can feel daunting for traditional app developers. What does the end-to-end applicati...

1 year ago

Certificación AI-900 (Fundamentos de IA) con Chicas en IA

La inteligencia artificial ha llegado para quedarse, ¡y más aún con la revolucionaria IA generativa! Para ayudar a los profesionales a especia...

1 year ago

Get certified with Learn Live GitHub series!

GitHub Universe is coming, and Microsoft and GitHub are partnering to offer a new special Learn Live series in Brazilian Portuguese, English a...

1 year ago

Certifícate con Learn Live GitHub en Español

Microsoft y GitHub se han unido para ofrecer una nueva serie especial de Learn Live en inglés y español: GitHub 2024. Del 10 al 24 de Octubre,...

1 year ago

Evaluating generative AI: Best practices for developers

As a developer working with generative AI, you've likely marveled at the impressive outputs your models can produce. But how do you ensure the...

1 year ago

Introducing Azure Product Retirement Livestreams

The Azure Retirements team, in collaboration with key partner groups, is excited t...

1 year ago

Newsletter

Get the latest Dynamics 365 and Power Platform content in your inbox

A curated digest of community blogs, product news, videos, and podcasts — delivered without the noise.

Weekly updates Unsubscribe anytime Fresh community picks
We use your email only for the newsletter and you can unsubscribe at any time.
By subscribing, you agree to the privacy policy.