box / box-python-sdk

Box SDK for Python
http://opensource.box.com/box-python-sdk/
Apache License 2.0
413 stars 214 forks source link

Monitor events in subfolders recursively #828

Closed annie0467 closed 10 months ago

annie0467 commented 1 year ago

Given a single folder, Is there any way to keep monitoring all of its subfolders for events(ex: ITEM_UPLOAD, ITEM_DOWNLOAD, etc) and associate it with the specific folder name?

For ex: hierarchy is as follows / Main_Folder -> A -> B -> C

There might be more folders in A, B, C I need to track events in each subfolder folder

I wish to use generate_events_with_long_polling()

I have tried creating a monitoring thread on each single folder, but I run into unexpected issues after sometime.

lukaszsocha2 commented 1 year ago

Hi @annie0467, by default generate_events_with_long_polling() provides ALL events for the authenticated user. So if an action takes place in any directory, you will get an event for it. Event has field source, which allow to get more details about event source. It could be one of: File / Folder / User / Event source object. See Event object documentation here. If the event source is File, Folder or Event source, it will have parent field, so you can filter out events happening in a specific folder (or a set of folders). You can use this code snippet to print name of the folder, in which the event takes place:

events = client.events().generate_events_with_long_polling()
for event in events:
    event_source = event.source
    if event_source.parent:
        print(f'Got {event.event_type} event in folder: {event_source.parent.name}')

Best, @lukaszsocha2

annie0467 commented 12 months ago

Thanks for your reply @lukaszsocha2 I was using the exact same code with tweaks of my folder id matching but I now have figured how to track events in all subfolders using this same code.

My only concern is that even after exception handling, the monitoring code get errors unexpectedly say after a few minutes or so

    raise BoxAPIException(
boxsdk.exception.BoxAPIException: Message: Authorization Failed
Status: 404
Code: not_found

And I confirmed that there is not authorization problem in the folder path I give any suggestions?

mhagmajer commented 11 months ago

Hi @annie0467, can you share your monitoring code that errors after a few minutes? We'll try to reproduce on our side

stale[bot] commented 10 months ago

This issue has been automatically marked as stale because it has not been updated in the last 30 days. It will be closed if no further activity occurs within the next 7 days. Feel free to reach out or mention Box SDK team member for further help and resources if they are needed.

stale[bot] commented 10 months ago

This issue has been automatically closed due to maximum period of being stale. Thank you for your contribution to Box Python SDK and feel free to open another PR/issue at any time.