Access logs for media downloads

I want to know how many times a media url is accessed and the ip address used, in order that I can disable the link if it is being used too many times from too many locations.

I have an app where a user can log in and get a unique download link for a media object/file that they will use in another third party desktop app. The third party app will download the file on a set schedule.

If I can see the access logs for the url I can identify where users are sharing the file with other people in other locations which is against the licence.

Is the data I require available anywhere within Anvil?

You can implement your own system for downloading using HTTP Endpoints and storing logs in a data table.

It was recently discussed on the Forum - Anvil Media URL - not permanent - #2 by jshaffstall

An example in your case can be

@anvil.server.http_endpoint('/media/:id')
def download_media(id):
     ip_address=anvil.server.request.remote_address 
     media_row=app_tables.media.get_by_id(id)
     app_tables.media_logs.add_row(ip=ip,media=media_row)
     return media_row['Media_Column']

Relevant Docs - Anvil Docs | Creating HTTP APIs

1 Like

You can also store your URL in a table, with a timestamp and a simple object column.

Access this table through a server module and you can use anvil.server.context.client to gather data about the requester. If it were me I would mash a bunch of that data together in a hash function and add it as a string to a set in the simple object column on every request.

If either the timestamp becomes too old, or the len() of your simple object column for that URL row grows too large, the URL can be deleted from the table and the client can be prevented from getting the download link.

See the following examples of the use of context.client:

1 Like

You can create your own log on a table as suggested by @ianb, or you can just print('something') and you will see it on the app log. You can add whatever you want to the app log.

Unfortunately the text search on the logs had been improved and was working very very well, but now it has been broken for a while.

Thanks all - thinking creating an API will be the correct solution for me.