Associating Binary file with managedObject

Hello,

In REST API documentation I am missing on how can someone associate the uploaded binary file with existing managedObject.

We want to let user upload their profile pictures to the already created managedObjects and also to let them update once in a while.
We want to once uploaded profile picture

We want to retrieve those pictures only in some scenarios.

REST API Documentation on Binaries does not contain such example.

An example would be very much appreciated.

Hi,

once a binary is uploaded and created it also has its own ManagedObject. This ManagedObject is being used to store meta-information about the binary, for example:

{
    "additionParents": {
        "self": "https://<tenant>.cumulocity.com/inventory/managedObjects/184888/additionParents",
        "references": []
    },
    "owner": "john.doe",
    "childDevices": {
        "self": "https://<tenant>.cumulocity.com/inventory/managedObjects/184888/childDevices",
        "references": []
    },
    "childAssets": {
        "self": "https://<tenant>.cumulocity.com/inventory/managedObjects/184888/childAssets",
        "references": []
    },
    "type": "image/png",
    "lastUpdated": "2021-03-03T09:12:03.212Z",
    "childAdditions": {
        "self": "https://<tenant>.cumulocity.com/inventory/managedObjects/184888/childAdditions",
        "references": []
    },
    "name": "logo.png",
    "assetParents": {
        "self": "https://<tenant>.cumulocity.com/inventory/managedObjects/184888/assetParents",
        "references": []
    },
    "deviceParents": {
        "self": "https://<tenant>.cumulocity.com/inventory/managedObjects/184888/deviceParents",
        "references": []
    },
    "self": "https://<tenant>.cumulocity.com/inventory/managedObjects/184888",
    "id": "184888",
    "c8y_IsBinary": "",
    "length": 165262,
    "contentType": "image/png"
}

The id of this ManagedObject can be used to either download the binary by using the Binary API {{url}}/inventory/binaries/<id> or you can query meta-information (as shown above) by using the Inventory API {{url}}/inventory/managedObjects/<id>.

If you want relate the binary to another ManagedObject there are different approaches on how can achieve this. The first solution would be to create a custom fragment on your ManagedObject to which you want to relate the Binary, which stores the id for the binary, e.g.

{
  // general user information ..., 
  "profilePictureId" : "184888"
}

or you could store the Binary ManagedObject as a child asset to the corresponding ManagedObject.

POST {{url}}/inventory/managedObjects/{{managedObjectId}}/childAssets

{
  "managedObject": {"id":"<id>"}
}

To create this kind of relationship would be a custom implementation in your application.

In addition, you can also update existing Binary by performing a PUT on {{url}}/inventory/binaries/{{binaryId}} and provide the new picture. Similar to the initial upload using the POST.

Hope this helps.

Best regards
Christian

1 Like