【发布时间】: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