Search code examples
c#azurehtml5-videoasp.net-core-webapiazure-blob-storage

Asynchronously Streaming Video with .Net Core API from Azure Blob Storage


I have found a bunch of examples that use objects not available to me within my application and don't seem to match up to my version of .NET Core web API. In essence, I am working on a project that will have tags on a web page and want to load the videos using a stream from the server rather than directly serving the files via a path. One reason is the source of the files may change and serving them via path isn't what my customer wants. So I need to be able to open a stream and async write the video file.

This for some reason produces JSON data so that's wrong. I am downloading the video file from Azure Blob storage and returning as a stream, but I just don't understand what I need to do to send a streamed video file to a tag in HTML.

My API Controller,

[AllowAnonymous]
    [HttpGet("getintroductoryvideos")]
    public async Task<Stream> GetIntroductoryVideos()
    {
        try
        {
            return  _documentsService.WriteContentToStream().Result;
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

My Service class,

 public async Task<Stream> WriteContentToStream()
    {
        var cloudBlob = await _blobService.GetBlobAsync(PlatformServiceConstants._blobIntroductoryVideoContainerPath + PlatformServiceConstants.IntroductoryVideo1, introductoryvideocontainerName);
        await cloudBlob.FetchAttributesAsync();

        var fileStream = new MemoryStream();
        await cloudBlob.DownloadToStreamAsync(fileStream);
        return fileStream;
    }

Solution

  • You can try the code below:

    API Controller:

    [AllowAnonymous]
    [HttpGet("getintroductoryvideos")]
    public async Task<FileContentResult> GetIntroductoryVideos(string videoname)
    {        
       return  await _documentsService.WriteContentToStream();        
    }
    

    Service class:

    public async Task<FileContentResult> WriteContentToStream()
    {
        var cloudBlob = await _blobService.GetBlobAsync(PlatformServiceConstants._blobIntroductoryVideoContainerPath + PlatformServiceConstants.IntroductoryVideo1, introductoryvideocontainerName);
    
        MemoryStream fileStream = new MemoryStream();
        await cloudBlob.DownloadToStreamAsync(fileStream);
        return new FileContentResult (fileStream.ToArray(), "application/octet-stream");
    
    }
    

    Html:

    <div className="xxx">
      <video height="auto">
          <source src="xx/getintroductoryvideos?videoname=xxx" type="video/mp4" />
      </video>
    </div>