How to get Object Store content via API

Posted by

How to get Object Store content via API

How to get the Object Store content via API easily? In this article, you’ll discover how to get it thanks to an official Mulesoft connector!

In several Mulesoft applications, Object Store is the suitable component to keep data persistently, although the Cloudhub Platform interface is not as user-friendly as it should sometimes be.

What I mean is, that as you want to understand what is really kept inside of the API you might face this scenario:

Sounds familiar? Here I am to help you!

My name is Lorenzo Neri, a Cap4 Lab integration engineer. Several times during my working time, I met many different problems I tried to solve, then afterwards, a question came to my mind: “What if I bring help to other people to solve my same problem?”.

Let’s get started.

How to get the object store content via API

Even if you are aware of the fact that the Cloudhub interface could be confusing sometimes, the solution I’m introducing is based on the Object Store v2 API offered by Mulesoft itself.

According to your user’s privileges, it allows you to view the object store content in a complete and sharp way.

The first step you need to perform is obtaining the organization ID and the environment ID of your VPC. To do so, I’ll provide you with the links to get

As soon as you got them… Get back here, please! We need to go further :wink:

Object Store v2 API: additional steps to use it properly

As soon as you get your organization ID and your Cloudhub environment ID, you need the client ID and client secret related to the application you’re working with:

Now that you have all of the required pieces, we could start obtaining the access token necessary to execute HTTP requests via Object Store v2 API.

To do so, execute a POST request at the following URL with a properly formatted and filled JSON payload, like so:

https://anypoint.mulesoft.com/accounts/oauth2/token

{    "client_id" : "app_client_ID",
    "client_secret": "app_client_secret",
    "grant_type" : "client_credentials"
 }

“app_clientID” and “app_client_secret” are related to your Mulesoft deployed application.

Once you get your access token, you can start to use the Object Store v2 API and we’ll see together the four steps needed to obtain stored values!

Object Store v2 API: get the content stored in your Mulesoft Application

Do you remember that I asked you to get your organization ID and environment ID related to your VPC? Now it’s time to grab them!

The first step is an HTTP request in order to get all of the applications into the given environment using object store at the following URL: https://object-store-your-mulesoft-world-zone.anypoint.mulesoft.com/api/v1/organizations/{ORGID}/environments/{ENV_ID}/stores/

 

To give you a complete context, the URL part of “your-mulesoft-world-zone” is the data center where your VPC is deployed: for instance eu-central-1. Meantime, URI parameters like “ORGID” and “ENV_ID” are pretty easy to be gotten :wink:

This endpoint will respond as follows:

{
    "values": [
        {
            "storeId": "APP_yourAPPNAME1__defaultPersistentObjectStore",
            "encrypted": true,
            "permanentOsFlag": false,
            "persistent": true,
            "defaultTtlSeconds": 2592000,
            "defaultConfirmationTtlSeconds": 600,
            "isFixedTtl": false
        },
        {
            "storeId": "APP_yourAPPNAME2__defaultPersistentObjectStore",
            "encrypted": true,
            "permanentOsFlag": false,
            "persistent": true,
            "defaultTtlSeconds": 2592000,
            "defaultConfirmationTtlSeconds": 600,
            "isFixedTtl": false
        }
    ],
    "nextPageToken": null
}

As soon as you identify your application, you’ll need to see all of the partitions related to its own object-store.

This next step could always be performed with an HTTP request at this URL:

https://object-store-your-mulesoft-world-zone.anypoint.mulesoft.com/api/v1/organizations/{ORGID}/environments/{ENV_ID}/stores/APP_yourAPPNAME__defaultPersistentObjectStore/partitions/

Pssst! There’s an additional URI parameter combined with the keyword: “APP_yourAPPNAME” combined with “__defaultPersistentObjectStore” is your application name!

And it gives you back the partitions:

{
    "values": [
        "TransactionsObjectStore",
        "_defaultPartition"
    ],
    "nextPageToken": null
}

After partitions, we could finally land on keys contained in the given partition as URI parameters!

… To another endpoint of course:

https://object-store-your-mulesoft-world-zone.anypoint.mulesoft.com/api/v1/organizations/{ORGID}/environments/{ENV_ID}/stores/APP_yourAPPNAME1__defaultPersistentObjectStore/partitions/NAMEOFPARTITION

Receiving back this outcome:

{
    "values": [
        {
            "keyId": "041190ac-1f2b-4dbf-8463-7995a5ef6eff"
        },
        {
            "keyId": "28f439d5-dc4d-4456-a6fb-159d32517470"
        }
    ]
}

I know, it’s a frustrating and repeating process, but we reached the final stage giving us back with no “mask” the TRUE content of the object store related to the previous keys you got back!

To obtain FINALLY the object store content related to a given key, the following URL is what you need to get it:

https://object-store-your-mulesoft-world-zone.anypoint.mulesoft.com/api/v1/organizations/{ORGID}/environments/{ENV_ID}/stores/APP_yourAPPNAME1__defaultPersistentObjectStore/partitions/NAMEOFPARTITIONS/keys/YOUR_KEY_ID_VALUE

Gives you the object store content related to the given key:

{
  "binaryValue": "++jksdfjlsod97CiAgImh0dHBUcmFuc2FjdGlvbklkIjogIjA0MTE5MGFjLTFmMmItNGRiZi04NDYzLTc5OTVhNWVmNmVmZiIsCiAgIm9yZGVySUQiOiAiSFlCNjI1ODI4MmY1Njk2ZDAwMDExOGVjNGQ5IiwKICAidHlwZU9wIjogImFjayIKffys2aEBAQEBamF2YS5pby5TZXJpYWxpemFibOWAYXBwbGljYXRpb24vanNvbjsgY2hhcnNldD1VVEYtuAAAAAAAAACA",
  "keyId": "041190ac-1f2b-4dbf-8463-7995a5ef6eff",
  "valueType": "BINARY"
}

Moreover, the keyId itself gives you back the true content of the object store like you were after at the beginning of the article.