Hi there,
I've been searching documentation and the web, while trying quite a few ways to simply lookup a media file by a name ("file.pdf"), by using the Umbraco API for the Media Service and/or Media File Manager, and I can't seem to accomplish it by using file name (versus path, GUID, etc).
I am refactoring older code that used SQL to directly check for the file name in the database, but I want to have it updated.
Is there no other way to check for existing media file?
I actually had a pretty elaborate set of methods that would look locally, and then if no, would switch to look in the blob service. But this seemed like stepping outside the context of Umbraco's capabilities.
It will if you have the full path (i.e. /media/1024/file.pdf) but not sure it will work with just the file name?
In an ideal world it's best to avoid hitting the DB via the MediaService if you can access the cache for looking things up, but depends if you want to then update it once you've got it!
To add a solution to the "get media" question of this post. What is working pretty well is using Examine to search for the file by name, returning the top matching result, and then setting the new file from the stream to the existing media.
I am trying to complete this project with being a batch process, but I'm having difficulties with it for some reason not always grabbing the new file from the stream, to upload in the existing media item to the media folder, but I am hopeful I'll get that part figured out.
Get Media File by Name in Umbraco 13
Hi there, I've been searching documentation and the web, while trying quite a few ways to simply lookup a media file by a name ("file.pdf"), by using the Umbraco API for the Media Service and/or Media File Manager, and I can't seem to accomplish it by using file name (versus path, GUID, etc).
I am refactoring older code that used SQL to directly check for the file name in the database, but I want to have it updated.
Is there no other way to check for existing media file?
I actually had a pretty elaborate set of methods that would look locally, and then if no, would switch to look in the blob service. But this seemed like stepping outside the context of Umbraco's capabilities.
If you've got access to the published cache, you could try something like this (I'm writing this off the top of my head so might not work!)
Something like that?
will generate direct SQL request to DB
It will if you have the full path (i.e. /media/1024/file.pdf) but not sure it will work with just the file name?
In an ideal world it's best to avoid hitting the DB via the MediaService if you can access the cache for looking things up, but depends if you want to then update it once you've got it!
yep, it will for relative path i.e. /media/1024/file.pdf,
using publishing api need to also think about folder, for big website can be slow
for big site examine query will be better solution
Thank you guys! You gave me some things to try.
For context, I don't know the media folder ("media/0csnrul/"), or if my startup.cs is using local or azure media.
Examine might be the best, however my goal is to get existing media file if exists and replace File, but keep path intact.
To add a solution to the "get media" question of this post. What is working pretty well is using Examine to search for the file by name, returning the top matching result, and then setting the new file from the stream to the existing media.
I am trying to complete this project with being a batch process, but I'm having difficulties with it for some reason not always grabbing the new file from the stream, to upload in the existing media item to the media folder, but I am hopeful I'll get that part figured out.
is working on a reply...