上传到 Azure Blob 存储的 MP4 视频文件无法播放

问题描述 投票:0回答:1

我使用下面的代码将 MP4 视频文件上传到 azure blob 存储。文件上传成功,但当我在浏览器中输入文件的 URL 时,它永远不会播放。但是,如果我从 Azure 门户将相同的文件上传到 blob 存储,它就会播放。不确定我的 C# 代码有什么问题。

public async Task<string> UploadVideoFileAsync(IFormFile file, string fileName, string containerName, IDictionary<string, string>? metadata = null)
{
    var containerClient = _blobServiceClient.GetBlobContainerClient(containerName);
    try
    {
        await containerClient.CreateIfNotExistsAsync();
        var blobClient = containerClient.GetBlobClient(fileName);
        using (Stream stream = file.OpenReadStream())
        {
            stream.Seek(0, SeekOrigin.Begin);                
            await blobClient.UploadAsync(stream, new BlobUploadOptions
            {
                HttpHeaders = new BlobHttpHeaders { ContentType = "video/mp4" }
            });
        }

        if (metadata != null)
        {
            await blobClient.SetMetadataAsync(metadata);
        }

        return blobClient.Uri.ToString();
    }
    catch (RequestFailedException ex)
    {
        // Handle the exception here, e.g., log or return an error message.
        return $"Error uploading the file: {ex.Message}";
    }
}  

使用上述代码上传的文件未在浏览器中播放:

c# azure video-streaming azure-blob-storage azure-sdk
1个回答
0
投票

上传到 Azure Blob 存储的 MP4 视频文件无法播放

您可以使用以下代码使用C#上传视频mp4文件。

代码:

namespace BlobStorageDemo
{
    class Program
    {
        static void Main(string[] args)
        {

            string connectionString = "xxxx";
            string containerName = "test";
            string fileName = "sample7.mp4";
            string filePath = "<path of file>";
            string contentType = "video/mp4";

            BlobServiceClient blobServiceClient = new BlobServiceClient(connectionString);
            BlobContainerClient containerClient = blobServiceClient.GetBlobContainerClient(containerName);
            BlobClient blobClient = containerClient.GetBlobClient(fileName);

            FileStream fileStream = null;
            try
            {
                fileStream = new FileStream(filePath, FileMode.Open);
                blobClient.Upload(fileStream, new BlobUploadOptions
                {
                    HttpHeaders = new BlobHttpHeaders { ContentType = contentType }
                });
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Error uploading the file: {ex.Message}");
            }
            finally
            {
                if (fileStream != null)
                {
                    fileStream.Dispose();
                }
            }
            Console.WriteLine($"File uploaded to {blobClient.Uri.ToString()}");
        }
    }
}

以上代码已执行并成功上传到azure blob存储。

我复制了输出 URL 并将其粘贴到浏览器中,它显示了视频。

输出:

© www.soinside.com 2019 - 2024. All rights reserved.