简体   繁体   English

Stream 视频来自 Azure blob 存储和 ASP.NET 核心 3

[英]Stream videos from Azure blob storage and ASP.NET Core 3

I'm using latest and recommended Azure.Storage.Blobs package.我正在使用最新和推荐Azure.Storage.Blobs package。 I'm uploading the video file as chunks, which works fine.我将视频文件作为块上传,效果很好。 The problem is now returning back the video to the web client, which is videojs .现在的问题是将视频返回给 web 客户端,即videojs The player is using Range request.播放器正在使用Range请求。

My endpoint:我的端点:

[HttpGet]
[Route("video/{id}")]
[AllowAnonymous]
public async Task<IActionResult> GetVideoStreamAsync(string id)
{
   var stream = await GetVideoFile(id);

   return File(stream, "video/mp4", true); // true is for enableRangeProcessing
}

And my GetVideoFile method还有我的GetVideoFile方法

var ms = new MemoryStream();
await blobClient.DownloadToAsync(ms, null, new StorageTransferOptions
{
    InitialTransferLength = 1024 * 1024,
    MaximumConcurrency = 20,
    MaximumTransferLength = 4 * 1024 * 1024
});

ms.Position = 0;

return ms;

The video gets downloaded and streamed just fine.视频被下载并流式传输就好了。 But it downloads the whole video and not respecting Range at all.但它下载了整个视频,根本不尊重Range I've also tried with DownloadTo(HttpRange)我也试过DownloadTo(HttpRange)

var ms = new MemoryStream();

// parse range header... 
var range = new HttpRange(from, to);
BlobDownloadInfo info = await blobClient.DownloadAsync(range);
await info.Content.CopyToAsync(ms);
return ms;

But nothing gets displayed in the browser.但是浏览器中没有显示任何内容。 What is the best way to achieve that?实现这一目标的最佳方法是什么?

Answering my own question if someone comes across.如果有人遇到,请回答我自己的问题。

CloudBlockBlob (version I'm using: 11.2.2) now has OpenReadAsync() method which returns stream. CloudBlockBlob (我正在使用的版本:11.2.2)现在具有OpenReadAsync()方法,该方法返回 stream。 In my case I'm returning this stream to videojs which handles the Range header on its own.在我的情况下,我将这个 stream 返回给videojs ,它自己处理Range header。

Please try by resetting the memory stream's position to 0 before returning:请尝试在返回之前将 memory 流的 position 重置为0

var ms = new MemoryStream();

// parse range header... 
var range = new HttpRange(from, to);
BlobDownloadInfo info = await blobClient.DownloadAsync(range);
await info.Content.CopyToAsync(ms);
ms.Position = 0;//ms is positioned at the end of the stream so we need to reset that.
return ms;

I believe it's not possible to achieve it only using Azure Blob.我相信仅使用 Azure Blob 是不可能实现的。 More info in here: https://stackoverflow.com/a/26053910/1384539更多信息在这里: https://stackoverflow.com/a/26053910/1384539

but in summary, you can use a CDN that offers the Seek Start / End position: https://docs.vdms.com/cdn/re3/Content/Streaming/HPD/Seeking_Within_a_Video.htm但总而言之,您可以使用提供搜索开始/结束 position 的 CDN: https://docs.vdms.com/cdn/re3/Content/Streaming/HPD/Seeking_Within_a_Video.htm

Another possibility is to use Azure Media Services that supports streamming.另一种可能性是使用支持流媒体的 Azure 媒体服务。 Your approach is actually a progressive download which is not exactly the same idea, and you'd probably spend a lot with network out.您的方法实际上是一种渐进式下载,这不是完全相同的想法,您可能会在网络外花费很多。 (assuming you have many access to the same file) (假设您对同一个文件有很多访问权限)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Azure blob存储下载流返回“”asp.net - Azure blob storage download to stream returning “” asp.net 如何将图像从 Asp.net Core IFormFile 上传到 Azure Blob 存储? - How to Upload Image From Asp.net Core IFormFile to Azure Blob Storage? 如何在 ASP.NET Core 应用程序中显示来自 Azure Blob 存储帐户的图像? - How to I display an image from my Azure Blob Storage Account in my ASP.NET Core Application? 如何从 Asp.Net 核心中的 azure blob 下载文件? - How to download a file from azure blob in Asp.Net core? 如何从 ASP.NET 核心应用程序将图像上传到 Azure blob 存储中的特定目录? - How do I upload an image to a specific directory in my Azure blob storage from my ASP.NET Core application? 使用ASP.NET在Windows Azure blob存储上设置CORS - Set CORS on Windows Azure blob storage using ASP.NET 如何使用ASP.NET MVC3在表中显示Azure Blob存储的所有图像? - How to display all the image from Azure Blob Storage in table using ASP.NET MVC3? 从 ASP.Net MVC 将 3+ GB 文件上传到 Azure Blob 存储的最佳方法是什么? - What is the best way to upload 3+ GB files to Azure Blob Storage from ASP.Net MVC? 从ASP.NET C#与MS Azure上的Blob存储交互 - interacting with blob storage on MS Azure from ASP.NET C# 如何在Asp.Net Core中将文件上传到Azure Blob? - How to upload a file in to azure blob in Asp.Net Core?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM