Skip to content

attaching binary file to an event in cumulocity

I have a scenario where in my code creates a file and an event in cumulocity as well. I need to attach the binary file to the event, however none of the API’s provide me the functionality, any guidance as to how to go ahead please?


On having a look at the documentation , the binaryElement has a following structure BinaryElement

I have modified my JAVA code in the following manner:

String pathEvent = platform.getUrl().get()+"/event/events/{{eventId}}/binaries";
    String pathEventAttachment = pathEvent.replace("{{eventId}}", event.getId().getValue());
    HttpHeaders headers = RequestAuthenticationEncoder.encode(getCredentials());
    MultiValueMap<String, Object> map = new LinkedMultiValueMap<>();
    map.add("file",new ByteArrayResource(FileUtils.readFileToByteArray(file)));//anychanges here fails the code
    map.add("filename", file.getName().getBytes());

    HttpEntity<MultiValueMap<String, Object>> request = new HttpEntity<>(map,headers);
    restTemplate.postForObject(pathEventAttachment, request, String.class);"Binary file added to the event {}", event.getId().getValue());

Binary is created for the event, however the filename is incorrect: Output structure

Please let me know some insights as to why the name is not changing?



You need to create a POST on /event/events/{{eventId}}/binaries with the appropriate Content-Type and body.

Here is a code snippet in python where a binary (log file) is attached to an event. It just POSTs the file content (bytes) as application/octet-stream

# Create event
logfileEvent = {
    'type': 'c8y_Logfile',
    'text': 'See attached logfile',
    'time': datetime.utcnow().strftime('%Y-%m-%dT%H:%M:%S.%f')[:-3] + 'Z',
    'source': {
        'id': DEVICE_ID
res = + '/event/events/', data=json.dumps(logfileEvent))
eventId = res.json()['id']

# Attach Binary
res = + '/event/events/' + eventId + '/binaries', data=bytes(FILE_CONTENT, 'utf-8'), headers={'Content-Type': 'application/octet-stream'})
binaryRef = res.json()['self']

# Update operation
logfileFragment = logfileOperation['c8y_LogfileRequest']
logfileFragment['file'] = binaryRef
updatedOperation = {
    'status': 'SUCCESSFUL',
    'c8y_LogfileRequest': logfileFragment
client.put(C8Y_BASE + '/devicecontrol/operations/' + logfileOperation['id'], data=json.dumps(updatedOperation))
2 People found this is helpful