如何将流媒体内容上传到Azure?

时间:2019-04-04 09:32:45

标签: c# azure

我在我的项目中将dotnet core与c#和angular js UI一起使用。我正在尝试将流媒体内容上传到Azure,但是每次失败都可以。 我尝试了太多方法,但没有一个起作用。 请帮助我,我被卡住了。

1 个答案:

答案 0 :(得分:1)

此链接可能会帮助您:

https://askgif.com/blog/314/uploading-streaming-media-content-to-azure/

我一直在Swank Motion Pictures的SmartTV应用程序上工作,以提供基于云的点播流电影服务。为此,我们将内容上载到Azure媒体服务资产,并使用OnDemand和SAS定位器在电视上流式传输内容。我已经将几个小方法提取到一个简单的类中,以帮助希望入门的任何人上载到Azure媒体服务。虽然此示例未提供PlayReady DRM或其他加密,但这可能是一个有用的开始。

/// <summary>
/// This class uploads files to Azure.
/// </summary>
public class SimpleAzureUploader
{
private readonly CloudMediaContext _context;

/// <summary>
/// Initializes a new instance of the <see cref="SimpleAzureUploader"/> class.
/// </summary>
/// <param name="accountName">Name of the Azure Media Services account.</param>
/// <param name="accountKey">The Account key.</param>
public SimpleAzureUploader(string accountName, string accountKey)
{
    _context = new CloudMediaContext(accountName, accountKey);
}

/// <summary>
/// Uplodas a local folder to a non-encrypted asset in Azure.
/// </summary>
/// <param name="assetName">Name of the asset.</param>
/// <param name="path">The directory or file path.</param>
/// <param name="concurrentTransfers">The max number of concurrent transfers.</param>
/// <param name="parallelTransferThreads">The max number parallel transfer threads.</param>
/// <returns></returns>
public IAsset UploadAsset(string assetName, string path, int concurrentTransfers = 10, int parallelTransferThreads = 10)
{
    var blobTransferClient = new BlobTransferClient
    {
        NumberOfConcurrentTransfers = concurrentTransfers,
        ParallelTransferThreadCount = parallelTransferThreads
    };

    var asset = _context.Assets.Create(assetName, AssetCreationOptions.None);

    var uploadingAccessPolicy = _context.AccessPolicies.Create("Upload Policy", new TimeSpan(1, 0, 0, 0), AccessPermissions.Write | AccessPermissions.List);

    var uploadingLocator = _context.Locators.CreateLocator(LocatorType.Sas, asset, uploadingAccessPolicy);

    if (Directory.Exists(path) && new FileInfo(path).Attributes.HasFlag(FileAttributes.Directory))
    {
        UploadFolder(asset, uploadingLocator, path, blobTransferClient);
    }
    else
    {
        UploadFile(asset, uploadingLocator, path, blobTransferClient);
    }

    uploadingLocator.Delete();
    uploadingAccessPolicy.Delete();

    return asset;
}

/// <summary>
/// Uploads the folder to the existing asset.
/// </summary>
/// <param name="asset">The existing asset.</param>
/// <param name="locator">The uploading locator.</param>
/// <param name="folderPath">The folder path.</param>
/// <param name="blobTransferClient">The blob transfer client.</param>
/// <exception cref="System.IO.FileNotFoundException"></exception>
private void UploadFolder(IAsset asset, ILocator locator, string folderPath, BlobTransferClient blobTransferClient)
{
    var filePaths = Directory.GetFiles(folderPath);

    if (!filePaths.Any())
    {
        throw new FileNotFoundException(String.Format("No files in directory, check folderPath: {0}", folderPath));
    }

    Task.WaitAll((from filePath
                  in filePaths
                  let assetFile = asset.AssetFiles.Create(Path.GetFileName(filePath))
                  select assetFile.UploadAsync(filePath, blobTransferClient, locator, CancellationToken.None)).ToArray());
}

/// <summary>
/// Uploads the file to the existing asset.
/// </summary>
/// <param name="asset">The existing asset.</param>
/// <param name="locator">The uploading locator.</param>
/// <param name="filePath">The file path.</param>
/// <param name="blobTransferClient">The blob transfer client.</param>
private void UploadFile(IAsset asset, ILocator locator, string filePath, BlobTransferClient blobTransferClient)
{
    var assetFile = asset.AssetFiles.Create(Path.GetFileName(filePath));
    assetFile.UploadAsync(filePath, blobTransferClient, locator, CancellationToken.None).Wait();
}
}

希望这会有所帮助。