Create and send url of media created in client

I am new to Anvil and I must be missing something fundamental. After almost 2 days on this, I don’t know what else to try.

I need to send a URL of an audio file to an external endpoint. I can use a URLs of a public file of different formats (wav, mp4) and thy work properly. So, everything is working except for being able to create a URL of a file I create client side and sending it to the endpoint. I am getting a response of 400, malformed / bad request.

On the client side I have two variables returned, one for audio content and one for uri.

audio = anvil.js.to_media(self.recording,content_type='audio/wav')
self.uri = js.window.URL.createObjectURL(self.recording)

self.uri address is different than creating a Blob from the audio. In the same pass I get:
self.uri:
blob:https://yks3o2kwribzm6cr.anvil.app/9e8152be-d815-4f0c-aa50-77b5902d9148

for the audio blob, using:

temp_url = anvil.media.TempUrl(audio)
blob_url = temp_url.url

blob:https://yks3o2kwribzm6cr.anvil.app/596c424d-b838-4714-8c62-3fb0465d9809

When I send either of these links to the server code to call the endpoint, with or without “blob:” I get response of 400.
On the server side I use the url that I pass from the client side. I read on a forum post that you need to open the file. If I try to open the link, then I get “No such file or directory.”

What I am missing?

Media urls are only good for the session in which they’re created, as a security measure. That prevents people from linking to your media objects on their own sites.

To create an independent URL that can serve media, you need to use HTTP endpoints. Doing some forum searching should show sample code for how to return a media object from an HTTP endpoint. It’s been quite a while, but I remember seeing someone post their results.

If you’re not using HTTP endpoints already, the docs have examples of creating them.

1 Like

@jshaffstall I am not serving media. I need to create a temporary URL of a media file created during the user’s session that gets sent to an endpoint via the server. How do I make that happen?

That is what I answered. You are serving a media file. You can only use the url of the media file in the session in which it is created, you cannot pass it to some third-party service.

To get a url that you can pass to a third-party service, you must use HTTP endpoints to serve the file. That means the file must be stored somewhere, probably in a data table.

1 Like

Okay, that is the point I am missing. Thank you.

May I suggest a rewording of this statement?: “You can only use the url of the media file in the session in which it is created, you cannot pass it to some third-party service.”
To:
“You can only use the url of the media file in the session in which it is created, but it will not work for a third-party service. For a third-party service, you need to create a HTTP endpoint to make the media accessible.”

So, what are TempURLs for? It states, " You can also create temporary client-side URLs for Media Objects, even if the media has no permanent URL, using anvil.media.TempURL(media)."
That suggests to me that I can use it with a third-party service.

The docs cover that.

Look at Anvil Docs | Files, Media Objects and Binary Data in the section on the url property. That states when you can get a permanent URL versus temporary ones.

On that same page when it talks about TempURL, it states the situation under which you’d use that function (e.g. what they are for).

It doesn’t talk about whether a TempURL is accessible from outside the session or not. From your attempt with your code, it looks like they aren’t.

2 Likes

I am unable to find TempUrl method in the anvil.media module. Has it been removed?

Error I am receiving: AttributeError: module 'anvil.media' has no attribute 'TempUrl'

I used it earlier and it was definitely still there!

I figured it out. It seems it is only available on the client side, not on the server side. I think that should be clarified somewhere in the docs.

1 Like