【问题标题】:Upload Video (mp4) to Azure Blob Storage将视频 (mp4) 上传到 Azure Blob 存储
【发布时间】:2020-04-09 23:43:36
【问题描述】:

我正在关注 Transfer data with the Data Movement library 将视频 (mp4) 从我的本地计算机上传到 Azure Blob 存储。

这里是代码。

using System;
using System.Threading;
using System.Threading.Tasks;
using System.Diagnostics;
using Microsoft.Azure.Storage;
using Microsoft.Azure.Storage.Blob;
using Microsoft.Azure.Storage.DataMovement;
using System.Net;
using System.Text;
using System.IO;
using System.Collections.Generic;
using Newtonsoft.Json;

namespace AzureUpload
{
    class Program
    {

        static void Main(string[] args)
        {
            try
            {
                CloudStorageAccount account = CloudStorageAccount.Parse("<<ConnectionString>>");
                ExecuteChoice(account);
            }
            catch (Exception e)
            {
                Console.ReadLine();
            }
            Console.ReadLine();
        }

        public static void ExecuteChoice(CloudStorageAccount account)
        {
            TransferLocalFileToAzureBlob(account).Wait();
        }

        public static string GetSourcePath()
        {
            return "E:\\SampleVideo.mp4";
        }

        public static CloudBlockBlob GetBlob(CloudStorageAccount account)
        {
            CloudBlobClient blobClient = account.CreateCloudBlobClient();
            CloudBlobContainer container = blobClient.GetContainerReference("<<Container>>");
            container.CreateIfNotExistsAsync().Wait();
            CloudBlockBlob blob = container.GetBlockBlobReference("MoD0604");
            blob.Properties.ContentType = "video/mp4"; // -- added this line to set ContentType as my video file is mp4
            return blob;
        }

        public static async Task TransferLocalFileToAzureBlob(CloudStorageAccount account)
        {
            string localFilePath = GetSourcePath();
            CloudBlockBlob blob = GetBlob(account);
            Console.WriteLine("\nTransfer started...");
            await TransferManager.UploadAsync(localFilePath, blob);
            Console.WriteLine("\nTransfer operation complete.");
        }
    }
}

链接中提供的示例在 GetBlob 方法中没有以下行

blob.Properties.ContentType = "video/mp4"; // -- added this line to set ContentType as my video file is mp4.

我尝试了使用 ContentType 而没有使用 ContentType。在这两种情况下,看起来文件都已上传到 Blob。唯一的区别似乎是 ContentType 如下图所示。

问题是在尝试下载任何文件时,它导致了一个警告错误,后来文件都没有播放,并显示以下错误。

请让我知道我在这里缺少什么。

【问题讨论】:

    标签: c# azure file-upload azure-blob-storage


    【解决方案1】:

    虽然标题设置正确,但 Windows 不知道如何处理没有扩展名的文件,这就是您无法打开它的原因。如果您在下载 blob 时附加了正确的扩展名,它将起作用,因为您(很可能)有一个与该特定文件类型相关联的应用程序。

    【讨论】:

    • 是的,问题出在扩展名上。在创建 CloudBlockBlob 时,我也应该传递扩展名。即 CloudBlockBlob blob = container.GetBlockBlobReference("MoD0604"); 应该是 CloudBlockBlob blob = container.GetBlockBlobReference("MoD0604.mp4"); .mp4 添加到名称中。
    【解决方案2】:

    考虑使用 Microsoft 的 the new SDK 来处理 Azure Blob 存储。
    安装 Azure.Storage.Blobs NuGet 包。

            string connString = "DefaultEndpointsProtocol=https;AccountName=yourAccountName;AccountKey=yourAccountKey;EndpointSuffix=core.windows.net";
    
            BlobHttpHeaders blobHttpHeaders = new BlobHttpHeaders()
            {
                ContentType = "video/mp4"
            };
    
            BlobClient blobClient = new BlobClient(connString, "container1", "SampleVideo.mp4");
            blobClient.Upload("E:\\SampleVideo.mp4", blobHttpHeaders);
    

    【讨论】:

      猜你喜欢
      • 2020-11-07
      • 2020-08-12
      • 1970-01-01
      • 2015-11-30
      • 2015-05-20
      • 2021-02-28
      • 2018-09-15
      • 2023-03-28
      • 2019-05-26
      相关资源
      最近更新 更多