Opening FileStream from Umbraco media item whilst ...
# help-with-umbraco
l
Hello, I am trying to open a file from the Umbraco Media library as a stream so that I can add it to a Zip archive to return to the user. However I'm using the Azure Blob Storage for media storage so it's not on the filesystem itself. I keep getting errors that the file does not exist or other null references. Am I trying to get it via an incorrect way or is this something that is not possible? I am using the
MediaFileManager.FileSystem
to get to the filesystem to use FileExists and OpenFile etc.
j
I want to preface this with saying I am not an expert on blob storage file streams at all, but Microsoft seems to have some documentation about getting items from blob storage into a stream (which I assume you could then move into your .zip as desired). https://learn.microsoft.com/en-us/azure/storage/blobs/storage-blob-download#download-to-a-stream
a
There is also an Umbraco specific approach, that may be a bit easier to work with. You can inject
MediaFileManager
. It has a
FileSystem
property, which again has a
OpenFile
method. Eg.:
Copy code
using Stream stream = _mediaFileManager.FileSystem.Open("/media/egpmoga4/froe.jpg");
j
Does that work with Azure storage?
a
Yup
j
Sweet, I learned something today 😄
a
The whole point is that Umbraco provides an abstraction for the file system, so you don't have to worry about whether media fiels are stored on disk, Azure blob storage. I haven't worked with Amazon, but I think that should work the same way.
j
Thanks, Anders! I've started working with some clients with Azure storage recently but many of our clients are self-hosted (government likes that for some reason), so it's something I'm still digging into.
l
I am working with the
MediaFileManager
as mentioned in the post but for some reason it doesn't want to open a stream to the file
Using the BlobClient would mean I would have the stream open already as well if Im reading the microsoft page correctly. I'm afraid that won't work either then
I was also using
var fullOrgPath = _mediaFileManager.FileSystem.GetFullPath(_mediaFileManager.FileSystem.GetRelativePath(imagePath));
which returned a path without the / at the beginning, this turned out to be the issue. If I use the
imagePath
variable which was just
media.Url()
then I get a path with a leading / which does work!
a
Yes, the path for the
OpenFile
method shouldn't be a full/absolute path, but the one you get from the
umbracoFile
property or
media.Url()
.
l
My mistake, thanks to both of you for helping!