Five New Optimizely Certifications are Here! Validate your expertise and advance your career with our latest certification exams. Click here to find out more

Get URL path for image from episerver blob file

Vote:
 

I'm creating a blob file using the following method in my code

Adding to Blob

 Thumbnail = MediaToBlob(thumbMediaUrl, accessToken, ".jpg", blobFactory, qbankMedia, "text/html");
                                    blobCache.Add(videoMediaUrl, qbankMedia.BinaryData);

MediaToBlob Method

public Blob MediaToBlob(string mediaUrl, string accessToken, string extension, IBlobFactory blobFactory, IQBankEpiMedia qbankMedia, string mimeType, int maxLength = int.MaxValue)
        {
            var mediaStream = GetMediaStream(mediaUrl, accessToken, mimeType, maxLength);

            Blob blob = null;

            if (mediaStream != null)
            {
                blob = blobFactory.CreateBlob(qbankMedia.BinaryDataContainer, extension);

                using (var blobStream = blob.OpenWrite())
                {
                    byte[] buffer = new byte[64 * 1024];
                    int read;
                    while ((read = mediaStream.Read(buffer, 0, buffer.Length)) > 0)
                        blobStream.Write(buffer, 0, read);
                    mediaStream.Flush();
                    mediaStream.Close();
                }
            }

            return blob;
        }

I Need to get a relative path to this Thumbnail object so that I can use it to render the image in site. I can use Thumbnail.ID.AbsoluteUri to get this :

epi.fx.blob://default/f791355aa6334c92b5f370c5cfafd971/adc882777a04431baae83a2e05f3f02d.jpg

instead of that, I need something like this:

http://localhost:8000/episerver/f791355aa6334c92b5f370c5cfafd971/adc882777a04431baae83a2e05f3f02d.jpg

Can anyone guide me to have something that I can use as normal Url for images?

#278715
Apr 19, 2022 16:43
Vote:
 

Hi, can't you use the public URL to the thumbnail? Not sure what format this is "http://localhost:8000/episerver/f791355aa6334c92b5f370c5cfafd971/adc882777a04431baae83a2e05f3f02d.jpg", it looks like nothing our APIs would produce. You can access thumbnail properties by appending the property name to the URL:

private readonly IUrlResolver _urlResolver;

var thumbnailUrl = _urlResolver.GetUrl(imageContentReference) + "/thumbnail";

This works for any thumbnail property your content type might have. 'Thumbnail' is the default thumbnail property available on ImageData.

#278789
Edited, Apr 20, 2022 9:38
Vote:
 

If you have a content reference to the media, then it's easier to follow Johans suggestion. 

However, if you only have access to the blob instance - have you tried using the blob.ID property? Can be used in one of the following ways:

Blob blob = ...;

var url  = UrlResolver.Current.GetUrl(new UrlBuilder(blob.ID), ContextMode.Default);

// ... or in a Razor file ...

@Url.ContentUrl(new Url(blob.ID))

// ... and also explore ...

var absolutePath = blob.ID.AbsolutePath;
#278790
Edited, Apr 20, 2022 9:47
Johan Petersson - Apr 20, 2022 9:53
I don't think this code will work if e.g. the blobs are stored in Azure Blob Storage.
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.