How to query a binary based on name?

Product/components used and version/fix level:

Cumulocity Production

Detailed explanation of the problem:

Hi, I have deployed one image on cumulocity binary. I want to get that image based on name. What I tried, I used custom query filter and tried to filter my image with this url but it is still returning all the binaries.

<tenant_url>/inventory/binaries?withTotalPages=true&pageSize=2000&query=$filter=name eq 'Aggregation service flow.png'

How can I achieve this scenario.

Thanks,
Samanyu

Looking at the Cumulocity Open API specification, the binaries resource does not support the inventory query language.

However the meta information for the binaries are stored as inventory managed objects (marked with the c8y_IsBinary fragment) so you can do an inventory query first to find the correct binary id, then retrieve the binary using the id.

The two-step process is detailed as follows:

  1. Look for the id of the binary using the inventory query language

    For example, filter for managed objects with the c8y_IsBinary fragment, and the name matching Aggregation service flow.png.

    GET /inventory/managedObjects?query=$filter=has(c8y_IsBinary) and name eq 'Aggregation service flow.png'
    

    The REST API above will return all the matching binary meta information managed objects, so select the appropriate .id property.

  2. Using the binary id found in the previous step, get the binary file using the binary API:

    GET /inventory/binaries/{id}
    

If you’re a go-c8y-cli user, you could do the same lookup using the following chained commands (which does exactly the same as above, but using shell pipeline):

c8y inventory find --query "has(c8y_IsBinary) and name eq 'Aggregation service flow.png'" | c8y binaries get
2 Likes

Hi Reuben,

Thank you for the response. It is working as expected, but I have one doubt. How Can I fetch this image or the doc whatever I have stored in binary in my code through rest api. I will get the response in postman but how can i fetch it in my code.

Lets say If I have stored 1 image and 1 doc, and I get the ids and i am trying to fetch it from binary, what object or what type will be returned by cumulocity in this case?

Thanks,
Samanyu

Hi @Sam123

it will be returned as the content-type which has been provided while posting the binaries.

Example

        {
            "owner": "service_report-agent",
            "type": "text/csv",
            "lastUpdated": "2020-11-19T12:00:00.146Z",
            "name": "Test_managedObjects_2020-11-19T12:00:00.086Z.csv",
            "self": "https://t14368213.eu-latest.cumulocity.com/inventory/binaries/240399",
            "id": "240399",
            "c8y_IsBinary": "",
            "length": 57,
            "contentType": "text/csv"
        }

When queried:
GET /inventory/binaries/240399
The response will have the content-type text/csv which needs to be handled in your code accordingly.

2 Likes

Understood, Thank you :heart:

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.