Microsoft Azure storage connector example¶
Given below is a sample scenario that demonstrates how to work with container and blob operations using the WSO2 Microsoft Azure Storage Connector.
What you'll build¶
This example demonstrates how to use Microsoft Azure Storage connector to:
- Create a container (a location for storing employee details) in a Microsoft Azure Storage account.
- Upload JSON employee details (blob) into the container.
- Download an employee details (blob).
- Remove uploaded employee details (blob).
- Retrieve the metadata from a specific file (blob).
- Remove created container.
For more information about these operations, please refer to the Microsoft Azure Storage connector reference guide.
Note: Before invoking the API, you need to create a Storage Account in Microsoft Azure Storage account. See Azure Storage Configuration documentation for more information.
Set up the integration project¶
Follow the steps in the create integration project guide to set up the Integration Project.
Create the integration logic¶
-
Click
+
on the Extension panel APIs to create the REST API. Specify the API name asMSAzureStorageTestAPI
and the API context as/azure
. Then, delete the default/resource
endpoint. -
First, we will create the
/createcontainer
resource. This API resource will retrieve the container name from the incoming HTTP POST request and create a container in Microsoft Azure Storage. Click on the+ Resource
and fill in the details. Use a URL template called/createcontainer
and the POST HTTP method. -
Click the created resource. Next, click the
+
arrow below the Start node to open the side panel. Select Externals and click Add new Connection. Searchmsazurestorage
and click. -
Create a connection as shown below.
In the window, the following parameters must be provided:
Note
You can either define the Account Access key or Client Credentials for authentication. For more information, please refer to Initialize the connector guide.
-
After the connection is successfully created, select the created connection in Externals. In the dropdown menu, click
CreateContainer
. -
Next, configure the following parameters in the properties window,
- Container Name - json-eval($.containerName)
-
Click
+
below theCreateContainer
node to add the Respond Mediator to send back the response from creating the container as shown below. -
Follow the same steps to create the next API resource,
/addblob
. This API resource will retrieve information about the blob from the incoming HTTP POST request, such as the container name, blob name, and the file content, and upload it to Microsoft Azure Storage. -
Next, add the
uploadBlob
operation from the Externals tab using the created connection. In the properties view, provide the following expressions for the below properties:- Container Name - json-eval($.containerName)
- Blob name - json-eval($.fileName)
- Content Type - json-eval($.contentType)
- Text Content - json-eval($.textContent)
- Metadata - json-eval($.metadata)
-
Add the Respond Mediator to send back the response from uploading the blob.
-
Follow the same steps to create the next API resource,
/downloadblob
. This API resource will retrieve information from the incoming HTTP POST request, such as the container name and blob name, and download from Microsoft Azure Storage. -
Next, add the
downloadBlob
operation from the Externals tab using the created connection. In the properties view, provide the following expressions for the below properties:- Container Name - json-eval($.containerName)
- Blob name - json-eval($.fileName)
-
Finally, add the Respond Mediator to send back the response from the
downloadBlob
operation. -
Follow the same steps to create the next API resource,
/deleteblob
. This API resource will retrieve information from the incoming HTTP POST request, such as the container name and blob name, and delete the blob from Microsoft Azure Storage. -
Next, add the
deleteBlob
operation from the Externals tab using the created connection. In the properties view, provide the following expressions for the below properties:- Container Name - json-eval($.containerName)
- Blob name - json-eval($.fileName)
-
Finally, add the Respond Mediator to send back the response from the
deleteBlob
operation. -
Follow the same steps to create the next API resource,
/listmetadata
. This API resource will retrieve information from the incoming HTTP POST request, such as the container name and blob name, and retrieve the metadata of the blob from Microsoft Azure Storage. -
Next, add the
listMetadata
operation from the Externals tab using the created connection. In the properties view, provide the following expressions for the below properties:- Container Name - json-eval($.containerName)
- Blob name - json-eval($.fileName)
-
Finally, add the Respond Mediator to send back the response from the
listMetadata
operation. -
Follow the same steps to create the next API resource,
/deletecontainer
. This API resource will retrieve information from the incoming HTTP POST request, such as the container name, and delete the container from Microsoft Azure Storage. -
Next, add the
deleteContainer
operation from the Externals tab using the created connection. In the properties view, provide the following expressions for the below properties:- Container Name - json-eval($.containerName)
-
Finally, add the Respond Mediator to send back the response from the
deleteContainer
operation. -
You can find the complete API XML configuration below. You can go to the source view (by clicking the
</>
icon on the top right corner) and copy-paste the following configuration.
<?xml version="1.0" encoding="UTF-8"?>
<api context="/azure" name="MSAzureStorageTestAPI" xmlns="http://ws.apache.org/ns/synapse">
<resource methods="POST" uri-template="/createcontainer">
<inSequence>
<msazurestorage.createContainer configKey="AZURE_CONNECTION">
<containerName>{json-eval($.containerName)}</containerName>
</msazurestorage.createContainer>
<respond/>
</inSequence>
<faultSequence/>
</resource>
<resource methods="POST" uri-template="/addblob">
<inSequence>
<msazurestorage.uploadBlob configKey="AZURE_CONNECTION">
<containerName>{json-eval($.containerName)}</containerName>
<textContent>{json-eval($.textContent)}</textContent>
<fileName>{json-eval($.fileName)}</fileName>
<blobContentType>{json-eval($.contentType)}</blobContentType>
<metadata>{json-eval($.metadata)}</metadata>
</msazurestorage.uploadBlob>
<respond/>
</inSequence>
<faultSequence/>
</resource>
<resource methods="POST" uri-template="/downloadblob">
<inSequence>
<msazurestorage.downloadBlob configKey="AZURE_CONNECTION">
<containerName>{json-eval($.containerName)}</containerName>
<fileName>{json-eval($.fileName)}</fileName>
</msazurestorage.downloadBlob>
<respond/>
</inSequence>
<faultSequence/>
</resource>
<resource methods="POST" uri-template="/deleteblob">
<inSequence>
<msazurestorage.deleteBlob configKey="AZURE_CONNECTION">
<containerName>{json-eval($.containerName)}</containerName>
<fileName>{json-eval($.fileName)}</fileName>
</msazurestorage.deleteBlob>
<respond/>
</inSequence>
<faultSequence/>
</resource>
<resource methods="POST" uri-template="/listmetadata">
<inSequence>
<msazurestorage.listMetadata configKey="AZURE_CONNECTION">
<containerName>{json-eval($.containerName)}</containerName>
<fileName>{json-eval($.fileName)}</fileName>
</msazurestorage.listMetadata>
<respond/>
</inSequence>
<faultSequence/>
</resource>
<resource methods="POST" uri-template="/deletecontainer">
<inSequence>
<msazurestorage.deleteContainer configKey="AZURE_CONNECTION">
<containerName>{json-eval($.containerName)}</containerName>
</msazurestorage.deleteContainer>
<respond/>
</inSequence>
<faultSequence/>
</resource>
</api>
Get the project¶
You can download the ZIP file and extract the contents to get the project code.
Tip
You may need to update the value of the credentials and make other such changes before deploying and running this project.
Deployment¶
Follow these steps to deploy the exported CApp in the integration runtime.
Deploying on Micro Integrator To deploy and run the project, refer to the Build and Run guide.
Test¶
Invoke the API as shown below using the curl command. Curl Application can be downloaded from here.
-
Creating a new container in Microsoft Azure Storage for store employee details.
Sample request
curl -v POST -d {"containerName":"employeedetails"} "http://localhost:8290/azure/createcontainer" -H "Content-Type:application/json"
Expected Response
{ "result": { "success": true } }
-
Upload JSON employee details.
Sample request
curl -v POST 'http://localhost:8290/azure/addblob' --header 'Content-Type: application/json' -d '{"containerName": "employeedetails", "fileName": "employee1.json", "textContent": "{\"name\":\"John\", \"salary\": 1000, \"age\": 44}", "contentType": "application/json", "metadata": {"key1": "value1"}}'
Expected Response
{ "result": { "success": true } }
-
Download JSON employee details.
Sample request
curl -v POST 'http://localhost:8290/azure/downloadblob' --header 'Content-Type: application/json' -d '{"containerName": "employeedetails", "fileName": "employee1.json"}'
Expected Response
It will retrieve the content text or binary name and the file path.
{ "name": "John", "salary": 1000, "age": 44 }
-
Retrieve blob metadata.
Sample request
curl -v POST 'http://localhost:8290/azure/listmetadata' --header 'Content-Type: application/json' -d '{"containerName": "employeedetails", "fileName": "employee1.json"}'
Expected Response
{ "result": { "metadata": { "key1": "value1" } } }
-
Remove uploaded employee details (blob).
Sample request
curl -v POST 'http://localhost:8290/azure/deleteblob' --header 'Content-Type: application/json' -d '{"containerName": "employeedetails", "fileName": "employee1.json"}'
Expected Response
{ "result": { "success": true } }
-
Remove created container.
Sample request
curl -v POST -d {"containerName":"employeedetails"} "http://localhost:8290/azure/deletecontainer" -H "Content-Type:application/json"
Expected Response
{ "result": { "success": true } }
What's next¶
- You can deploy and run your project on Docker or Kubernetes. See the instructions in Running the Micro Integrator on Containers.