Eksportuj (0) Drukuj
Rozwiń wszystko
EN
Ta zawartość nie jest dostępna w wymaganym języku. Wersja w języku angielskim znajduje się tutaj.

Ingesting Assets with the Media Services SDK for .NET

Updated: February 13, 2014

This topic shows how to ingest (upload) content into Media Services. In a Media Services application, ingesting tasks is the process of uploading content (assets, media files, and so on) into the system. The most fundamental content object in Media Services is an IAsset, which is a collection of metadata about a set of media files. Each IAsset contains one or more IAssetFile objects. For more detailed explanation of what an Asset in Media Services is, see Asset.

noteNote
Media Services uses the value of the IAssetFile.Name property when building URLs for the streaming content (for example, http://{WAMSAccount}.origin.mediaservices.windows.net/{GUID}/{IAssetFile.Name}/streamingParameters.) For this reason, percent-encoding is not allowed. The value of the Name property cannot have any of the following percent-encoding-reserved characters: !*'();:@&=+$,/?%#[]". Also, there can only be one ‘.’ for the file name extension.

There are a few different approaches to ingesting assets into Media Services:

  • Create an Asset, create one or more AssetFiles that are associated with the Asset, and then upload the AssetFiles to Microsoft Azure Media Services. Use this approach when working with smaller sets of media files, and when not using a separate upload solution. All examples in this topic are of this type.

  • Bulk ingest a set of files by preparing a manifest and upload them to Microsoft Azure Media Services using a separate upload solution. For more information on this approach, see Ingesting Assets in Bulk with the Media Services SDK for .NET.

TipTip
When uploading a large number of assets, consider the following.

The code in this topic shows how to perform the following common tasks:

noteNote
To create any asset, first you must have a reference to the Media Services server context as described in the topic Connecting to Media Services with the Media Services SDK. The context object is represented by the variable named _context in the following code examples.

Creating an empty asset

Before you can upload a file to the Media Services server, you must create an asset using the Create or CreateAsync method. Both methods take the asset name and encryption option as parameters.

When you create assets, you can specify three different options for encryption:

  • AssetCreationOptions.None: no encryption.

  • AssetCreationOptions.CommonEncryptionProtected: for Common Encryption Protected (CENC) files. An example is a set of files that are already PlayReady encrypted.

  • AssetCreationOptions.StorageEncrypted: storage encryption. Encrypts a clear input file before it is uploaded to Azure storage.

    noteNote
    Note that Media Services only provides on-disk storage encryption, not over the wire like a Digital Rights Management solution (DRM).

The CreateEmptyAsset method shows how to create an empty asset.

static private IAsset CreateEmptyAsset(string assetName, AssetCreationOptions assetCreationOptions)
{
    var asset = _context.Assets.Create(assetName, assetCreationOptions);

    Console.WriteLine("Asset name: " + asset.Name);
    Console.WriteLine("Time created: " + asset.Created.Date.ToString());

    return asset;
}

Uploading a single file

The code in this section does the following:

  1. Creates an empty Asset using the CreateEmptyAsset method defined in the previous step.

  2. Creates an AssetFile instance associated with the Asset.

  3. Uploads the media file associated with the AssetFile into Media Services.

noteNote
The AssetFile instance and the actual media file are two distinct objects. The AssetFile instance contains metadata about the media file, while the media file contains the actual media content.

static public IAsset CreateAssetAndUploadSingleFile( AssetCreationOptions assetCreationOptions, string singleFilePath)
{   
    var assetName = "UploadSingleFile_" + DateTime.UtcNow.ToString();
    var asset = CreateEmptyAsset(assetName, assetCreationOptions);
    var fileName = Path.GetFileName(singleFilePath);
    var assetFile = asset.AssetFiles.Create(fileName);

    Console.WriteLine("Created assetFile {0}", assetFile.Name);
    Console.WriteLine("Upload {0}", assetFile.Name);

    assetFile.Upload(singleFilePath);
    Console.WriteLine("Done uploading of {0}", assetFile.Name);

    return asset;

}

Upload multiple files from a local directory and display upload progress

The code in this section does the following:

  1. Creates an empty asset using the CreateEmptyAsset method defined in the previous step.

  2. Creates an AccessPolicy instance that defines the permissions and duration of access to the asset.

  3. Creates a Locator instance that provides access to the asset.

  4. Creates a BlobTransferClient instance. This type represents a client that operates on the Azure blobs. In this example we use the client to monitor the upload progress.

  5. Enumerates through files in the specified directory and creates an AssetFile instance for each file.

  6. Uploads the files into Media Services using the UploadAsync method.

    TipTip
    Use the UploadAsync method to ensure that the calls are not blocking and the files are uploaded in parallel.

static public IAsset CreateAssetAndUploadMultipleFiles(AssetCreationOptions assetCreationOptions, string folderPath)
{
      var assetName = "UploadMultipleFiles_" + DateTime.UtcNow.ToString();
      var asset = CreateEmptyAsset(assetName, assetCreationOptions);
      var accessPolicy = _context.AccessPolicies.Create(assetName, TimeSpan.FromDays(30), AccessPermissions.Write | AccessPermissions.List);
      var locator = _context.Locators.CreateLocator(LocatorType.Sas, asset, accessPolicy);

      var blobTransferClient = new BlobTransferClient();
      blobTransferClient.NumberOfConcurrentTransfers = 20;
      blobTransferClient.ParallelTransferThreadCount = 20;
      blobTransferClient.TransferProgressChanged += blobTransferClient_TransferProgressChanged;

       var filePaths = Directory.EnumerateFiles(folderPath);

       Console.WriteLine("There are {0} files in {1}", filePaths.Count(), folderPath);

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

       var uploadTasks = new List<Task>();
       foreach (var filePath in filePaths)
       {
            var assetFile = asset.AssetFiles.Create(Path.GetFileName(filePath));

            Console.WriteLine("Start uploading of {0}", assetFile.Name);
            uploadTasks.Add(assetFile.UploadAsync(filePath, blobTransferClient, locator, CancellationToken.None));
       }

      Task.WaitAll(uploadTasks.ToArray());
      Console.WriteLine("Done uploading the files");

      blobTransferClient.TransferProgressChanged -= blobTransferClient_TransferProgressChanged;
      locator.Delete();
      accessPolicy.Delete();

      return asset;

}

static void  blobTransferClient_TransferProgressChanged(object sender, BlobTransferProgressChangedEventArgs e)
{
    Console.WriteLine("{0}% upload competed for {1}.", e.ProgressPercentage, e.LocalFile);
}

You are ready to proceed to the next topic: Ingesting Assets in Bulk with the Media Services SDK for .NET.

See Also


Build Date:

2014-04-24

Zawartość społeczności

Dodaj
Pokaż:
© 2014 Microsoft