Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Niels Lynggaard 192 posts 550 karma points
    Mar 02, 2020 @ 11:48
    Niels Lynggaard
    0

    Umbraco 7 Programatically set umbracoFile on MediaItem doesn't work with Azure Blob Storage...

    Hi All!

    I've recently switched to Azure Blob storage for media items on a Umbraco 7 Cloud project. Works well.

    Except, I have an integration to some imageserver, that we're getting images from, so that upon pull from a specific service, we download images and add them to Umbraco Media Library (through an eventhandler).

    This part worked like a charm, untill I switched to Azure Blob storage. Now, the media items are getting created, the 'umbracoFile'-property is being set, but the actual image isn't there, thus all images goes 404 on me...

    The error log tells me that "Blob not found", for all the imported items..

    Is there something i need to adjust on this bit of code (using memorystream to set umbracoFile)?

    // Download file from url
                    using (var stream = _httpClient.GetStreamAsync(fullImageUrl).ConfigureAwait(false).GetAwaiter().GetResult())
                    {
                        using (var memoryStream = new MemoryStream())
                        {
                            stream.CopyTo(memoryStream);
    
                            string fileName = new Uri(imageRef.BaseImageUrl).Segments.Last();
    
                            // Set umbracoFile property
                            item.SetValue("umbracoFile", fileName, memoryStream);
    
                            // Set metadata properties
                            TrySetPropertyValue(item, pt => pt.AltText, metadata.Caption);
                            TrySetPropertyValue(item, pt => pt.PublicationStatus, metadata.PublicationStatus);
                            TrySetPropertyValue(item, pt => pt.DisplayCredit, metadata.DisplayCredit);
                            TrySetPropertyValue(item, pt => pt.MetadataUpdated, DateTime.Now);
                        }
                    }
    

    Bonus info; Creating media items and uploading files through media-section works like a charm...

  • Niels Lynggaard 192 posts 550 karma points
    Mar 04, 2020 @ 22:18
    Niels Lynggaard
    100

    Nevermind, I figured it out... the stream.CopyTo moves the position of the stream to the end.

    The solution was to do a memoryStream.Position = 0 before moving on.

Please Sign in or register to post replies

Write your reply to:

Draft