I want to generating PNG images in a Python transform and save them as a Media item. The inputs to the transform are datasets, and there are then media outputs and datasets outputs. No media input.
It’s not clear to me how I should save the media reference (from the media item I just generated) to a dataset. This documentation explains how to do it given a media input to the transform, but doesn’t work in my case.
As a workaround, I’m passing the media item RID and media set RID to this API endpoint, which is documented properly. (Is there a better way?)
The problem is that this doesn’t return a proper media reference, because it returns the media reference in single quotes. From what I can tell, there are no examples in documentation of the proper media reference format except here; e.g. this documentation is pretty unhelpful!
The “media reference” returned by the API is in single quotes (row 1 in the image below) and hence an invalid media reference. If I replace single quotes with double quotes, it’s now a valid media reference.
Here’s my code (with the hacky workaround, including use of a bearer token):
# Upload to media set
upload_response = output_media_set.put_media_item(img_buffer, filename)
uploaded_media_item_rid = upload_response.media_item_rid
logger.info(f"Successfully uploaded media item RID: {uploaded_media_item_rid}")
try:
headers = {"authorization": f"Bearer {TOKEN}"}
url = (
f"https://<hostname>/api/v2/mediasets/ri.mio.main.media-set.577f7d2e-f7f0-4dc7-ba0a-5f7262d45631/items/{uploaded_media_item_rid}/reference?preview=true"
)
logger.info(url)
response = requests.get(url, headers=headers)
# Raise an HTTPError if the response was 4xx/5xx
response.raise_for_status()
# If you want the JSON payload:
media_reference = response.json()
logger.info(f"Retrieved proper media reference: {media_reference}")
The output is an invalid media reference, as we can see in the logs for logger.info(f"Retrieved proper media reference: {media_reference}")
.
While my build is successful (and the mediaset builds fine), when I inspect the output dataset, I get an error in the console
SyntaxError: Expected property name or ‘}’ in JSON at position 1 (line 1 column 2)
and the following error in the table
This is due to the malformed media reference.