Compute Modules - Writing to Media Sets

Hey there,

Does anyone have experience writing to a media set from a compute module in pipeline mode. I’m trying to create a compute Module that writes to a Mediaset. I’m currently attempting to write to it like this


        # Upload to Foundry media set
        headers = {
            "content-type": "application/octet-stream",
            "authorization": f"Bearer {bearer_token}"
        }
        
        media_url = f"https://{FOUNDRY_URL}/api/v2/mediasets/{media_set_rid}/items?mediaItemPath={filename}&preview=true"
        
        print(f"Uploading to: {media_url}")
        print(f"File: {file_path}")
        print(f"Filename: {filename}")
        print(f"File size: {len(file_content)} bytes")
        
        upload_resp = requests.post(media_url, headers=headers, data=file_content)

but I’m getting the error:

{\"errorCode\":\"PERMISSION_DENIED\",\"errorName\":\"ApiUsageDenied\",\"errorInstanceId\":\"d483478a-03de-4c82-ab14-80937318a24a\",\"parameters\":{\"missingScope\":\"api:usage:mediasets-write\"}}"}

The media set I’m writing too is in the outputs of the compute module so i’m surprised I’m not able to write here? Is there another way to write to the media set? I feel like I may have to do this via stream proxy or some other end point but I can’t find where?

Hi! You should be able to do this, and setting the mediaset as an output of the CM should give your token access to write to the mediaset.

How are you getting the bearer token before the request? It should look something like this:

with open(os.environ['BUILD2_TOKEN']) as f:
    bearer_token = f.read()

Hmmm, Thats exactly how I’m getting the token, the token even works to write to a stream within the same module, I just can’t seem to write too the media set. I have the media set added as an output to my compute module. Anything else I could be missing here?

Actually I think the issue might be since in pipelines mode the inputs/outputs go through the build system you aren’t able to create a new transaction to write to the output mediaset. One option you could try is to run the CM in functions mode but just have similar code to pipelines mode running in a while loop. This will allow it to create new transactions, such as for writing to the media set.

The only thing you should have to change in your code is instead of using the build2 token for auth, you can get a bearer token from the client id/secret like this:

token_response = requests.post("https://stack.palantirfoundry.com/multipass/api/oauth2/token",
    data={
        "grant_type": "client_credentials",
        "client_id": os.environ["CLIENT_ID"],
        "client_secret": os.environ["CLIENT_SECRET"],
        "scope": "compass:edit"
    },
    headers={
        "Content-Type": "application/x-www-form-urlencoded",
    }
)
access_token = token_response.json()["access_token"]

I’ve tried this with the service user but I’m actually still seeing an identical error. It still claims I don’t have the permissions to write to that media sets. The service user has both been granted owner on the resource and i’ve given it the scope for media set write. Very strange

Hi, thank you for your patience on this issue. I realized that my previous response was missing a key part. The service user needs to be given write access to the dataset, and the access token used in code should be scoped based on the permissions needed.

To give the service user write access open the output Mediaset, then click the “Share” button in the top right of your screen. Then under access there’s a section for “Add a user or group”, input the Client ID for the service user here then give it either the Editor or Owner role.

As far as the scoped access token, below is an example of how you can get this in Python.

Apologies for the error earlier, this should resolve the issue you’re seeing. Let me know if you run into anymore problems!

from compute_modules.auth import oauth

FOUNDRY_URL = "stack.palantirfoundry.com"

access_token = oauth(FOUNDRY_URL, ["foundry:read-data",
    "api:mediasets-write",
    "mio:read-media-set",
    "mio:write-media-set",
    "api:ontologies-read",
    "api:ontologies-write",
    "offline_access"])