Ah, yes - the url
property of Media objects is only valid:
- If the Media object is permanent (eg a Data Table)
- If you’re requesting it from the same browser session as the one that accessed the
url
property (if you’re requesting from outside the browser, each API request executes in a fresh session)
For more information, see https://anvil.works/docs/media#using-media-objects-directly
For what you’re doing, I would recommend creating an API endpoint that serves one image, and one that provides the JSON index. Something like this (I’m using row IDs to construct the URL for each image; you can use something else as long as it’s enough information to find the right image):
@anvil.server.http_endpoint('/all_images')
def get_images(**p):
return [{'url': anvil.server.get_api_origin()+"/image/"+anvil.http.url_encode(row.get_id())}
for row in app_tables.images.search()]
@anvil.server.http_endpoint('/image/:row_id')
def get_image(row_id, **p):
row = app_tables.images.get_by_id(row_id)
if row is not None:
# Returning a Media object serves it as the HTTP response
return row['image_column']
else:
return anvil.server.HttpResponse(status='404')
(Obviously, if you want those endpoints to be only accessible by certain users, you’ll want to do a bit of checking around “is this user allowed to access this row?”)
This kind of thing (making URLs that refer to particular database tables) is described in more detail in the guide @alcampopiano posted earlier: