使用SasLocator上传到Azure媒体服务/ blob存储未显示上传的文件

时间:2018-05-31 14:02:39

标签: c# azure azure-storage azure-mobile-services

所以我想将视频从客户端桌面应用程序上传到Azure媒体服务(当然使用Azure存储)。

我正在尝试组合:

第一个展示了我的方案的完美示例,但第二个展示了如何使用BlobTransferClient上传多个文件并拥有“进度”指示器。

问题:它似乎上传,上传后我没有收到任何错误,但Azure门户/存储帐户中没有显示任何内容。 它似乎上传,因为任务需要很长时间,任务管理器显示wifi上传进度,Azure存储显示正在(成功)请求。

所以,在服务器端,我暂时创建一个SasLocator:

public async Task<VideoUploadModel> GetSasLocator(string filename)
{

    var assetName = filename + DateTime.UtcNow;

    IAsset asset = await _context.Assets.CreateAsync(assetName, AssetCreationOptions.None, CancellationToken.None);

    IAccessPolicy accessPolicy = _context.AccessPolicies.Create(assetName, TimeSpan.FromMinutes(10),
        AccessPermissions.Write);

    var locator = _context.Locators.CreateLocator(LocatorType.Sas, asset, accessPolicy);

    var blobUri = new UriBuilder(locator.Path);
    blobUri.Path += "/" + filename;

    var model = new VideoUploadModel()
    {
        Filename = filename,
        AssetName = assetName,
        SasLocator = blobUri.Uri.AbsoluteUri,
        AssetId = asset.Id
    };
    return model;
}

在客户端,我尝试上传:

public async Task UploadVideoFileToBlobStorage(string[] files, string sasLocator, CancellationToken cancellationToken)
{
    var blobUri = new Uri(sasLocator);
    var sasCredentials = new StorageCredentials(blobUri.Query);

    //var blob = new CloudBlockBlob(new Uri(blobUri.GetComponents(UriComponents.SchemeAndServer | UriComponents.Path, UriFormat.UriEscaped)), sasCredentials);
    var blobClient = new CloudBlobClient(new Uri(blobUri.GetComponents(UriComponents.SchemeAndServer | UriComponents.Path, UriFormat.UriEscaped)), sasCredentials);
    var blobTransferClient = new BlobTransferClient(TimeSpan.FromMinutes(1))
    {
        NumberOfConcurrentTransfers = 2,
        ParallelTransferThreadCount = 2
    };
    //register events
    blobTransferClient.TransferProgressChanged += BlobTransferClient_TransferProgressChanged;
    //files
    var uploadTasks = new List<Task>();
    foreach (var filePath in files)
    {
        await blobTransferClient.UploadBlob(blobUri, filePath, new FileEncryption(), cancellationToken, blobClient, new NoRetry());
    }



    //StorageFile storageFile = null;

    //if (string.IsNullOrEmpty(file.FutureAccessToken))
    //{
    //    storageFile = await StorageFile.GetFileFromPathAsync(file.Path).AsTask(cancellationToken);
    //}
    //else
    //{
    //    storageFile = await StorageApplicationPermissions.FutureAccessList.GetFileAsync(file.FutureAccessToken).AsTask(cancellationToken);
    //}
    //cancellationToken.ThrowIfCancellationRequested();
    //await blob.UploadFromFileAsync(storageFile);
}

我知道我可能没有正确地命名资产和使用进度指示器而不是等待,但当然我首先要在完成它之前先使用它。

我将Azure媒体服务配置为“使用服务主体连接到媒体服务API”,我在其中创建了一个新的Azure AD应用并为其生成了密钥,如this documentation page。我不确定这是如何工作的,在Azure AD和Azure AD应用程序中没什么经验(指导?)。

上传:

uploading of asset

已创建资产但没有文件:

media services

存储也不会显示任何文件:

blob containers

存储确实显示成功上传:

statistics

我无法完全遵循Upload multiple files with Media Services .NET SDK文档的原因是因为它使用_contextMicrosoft.WindowsAzure.MediaServices.Client.CloudMediaContext),_context我可以使用服务器端而不是客户端,因为它需要TentantDomain,RESTAPI Endpoint,ClientId和Client Secret。

我猜通过SaSLocator上传是正确的方法(?)。

更新1

使用CloudBlockBlob上传时,它会再次上传并显示在资产中的我的存储帐户中,但当我在azure中查看媒体服务并点击特定资产时,它不会显示任何文件

所以代码:

var blob = new CloudBlockBlob(new Uri(blobUri.GetComponents(UriComponents.SchemeAndServer | UriComponents.Path, UriFormat.UriEscaped)), sasCredentials);
//files
var uploadTasks = new List<Task>();
foreach (var filePath in files)
{
    await blob.UploadFromFileAsync(filePath, CancellationToken.None);
}

我还尝试在Azure中手动上传资源。因此,单击“资源”菜单中的“上载”,然后对其进行编码。一切正常。

更新2:

深入挖掘我想出了以下目标,但尚未提供生产证明,以使其目前正常运行:

1。直接从存储中获取共享访问签名并将其上传到该位置:

public static async Task<string> GetMediaSasLocator(string filename)
{
    CloudBlobContainer cont = await GetMediaContainerAsync();
    SharedAccessBlobPolicy policy = new SharedAccessBlobPolicy()
    {
        SharedAccessExpiryTime = DateTimeOffset.UtcNow.AddMinutes(60),
        Permissions = SharedAccessBlobPermissions.Write,
        SharedAccessStartTime = DateTimeOffset.UtcNow.AddMinutes(-5)
    };
    await cont.FetchAttributesAsync();

    return cont.Uri.AbsoluteUri + "/" + filename + cont.GetSharedAccessSignature(policy);
}

有了这个SaS,我可以上传就像我在UPDATE 1中显示的那样,没有任何改变。

2. :创建Azure功能(已计划执行),用于处理资产创建,文件上传到资产,编码和发布。 这是通过遵循本教程完成的:Azure Functions Tools for Visual Studio然后实现Upload multiple files with Media Services .NET SDK中说明的代码。

所以这“工作”但还不完美,我仍然没有在我的客户端WPF应用程序中使用我的进度指示器,Azure功能需要很长时间才能完成,因为我们基本上“再次”将文件“上传”到资产已存在于Azure存储中。我宁愿使用方法从一个容器复制到资产容器。

我之前提到过,因为Azure功能需要一个固定的给定容器名称,因为资产在存储帐户中创建自己的容器,您无法在这些容器上触发Azure功能。因此,要使用Azure功能,我似乎必须将其上传到固定的容器名称,然后完成剩下的工作。

问题仍然存在:为什么通过BlobTransferClient将视频文件上传到Azure存储无效?如果它可以工作,我如何基于多个容器触发Azure功能。像资产一样的'路径' - {name} / {name} .avi将是首选。

1 个答案:

答案 0 :(得分:1)

最终我发现我需要在UploadBlob方法中指定基本网址,因此没有SasLocator网址中的文件名本身,只有容器名称。< / p>

一旦我修复了,我还注意到它没有上传到我在SasLocator中提供的文件名我生成的服务器端(它包含一个customerID前缀)。我不得不使用其他方法重载之一来获取正确的文件名。

public async Task UploadVideoFilesToBlobStorage(List<VideoUploadModel> videos, CancellationToken cancellationToken)
{
    var blobTransferClient = new BlobTransferClient();
    //register events
    blobTransferClient.TransferProgressChanged += BlobTransferClient_TransferProgressChanged;
    //files
    _videoCount = _videoCountLeft = videos.Count;
    foreach (var video in videos)
    {
        var blobUri = new Uri(video.SasLocator);
        //create the sasCredentials
        var sasCredentials = new StorageCredentials(blobUri.Query);
        //get the URL without sasCredentials, so only path and filename.
        var blobUriBaseFile = new Uri(blobUri.GetComponents(UriComponents.SchemeAndServer | UriComponents.Path,
            UriFormat.UriEscaped));
        //get the URL without filename (needed for BlobTransferClient (seems to me like a issue)
        var blobUriBase = new Uri(blobUriBaseFile.AbsoluteUri.Replace("/"+video.Filename, ""));

        var blobClient = new CloudBlobClient(blobUriBaseFile, sasCredentials);
        //upload using stream, other overload of UploadBlob forces to put online filename of local filename
        using (FileStream fs = new FileStream(video.FilePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
        {
            await blobTransferClient.UploadBlob(blobUriBase, video.Filename, fs, null, cancellationToken, blobClient, 
                new NoRetry(), "video/x-msvideo");
        }
        _videoCountLeft -= 1;
    }

    blobTransferClient.TransferProgressChanged -= BlobTransferClient_TransferProgressChanged;
}

private void BlobTransferClient_TransferProgressChanged(object sender, BlobTransferProgressChangedEventArgs e)
{
    Console.WriteLine("progress, seconds remaining:" + e.TimeRemaining.Seconds);
    double bytesTransfered = e.BytesTransferred;
    double bytesTotal = e.TotalBytesToTransfer;
    double thisProcent = bytesTransfered / bytesTotal;
    double procent = thisProcent;
    //devide by video amount
    int videosUploaded = _videoCount - _videoCountLeft;
    if (_videoCountLeft > 0)
    {
        procent = (thisProcent + videosUploaded) / _videoCount;
    }

    procent = procent * 100;//to real %
    UploadProgressChangedEvent?.Invoke((int)procent, videosUploaded, _videoCount);
}

实际上Microsoft.WindowsAzure.MediaServices.Client.BlobTransferClient应该可以进行并发上传,但没有上传多个方法,但它有NumberOfConcurrentTransfersParallelTransferThreadCount的属性,不知道如何使用它。

我没有检查它现在是否也在使用Assets,因为我现在为每个文件上传到1个单独的容器,然后使用Azure Function处理到Asset,主要是因为我无法触发Azure功能在动态容器名称上(每个资产都创建自己的容器)。