匯出 (0) 列印
全部展開

將現有的大型物件複製到Media Services 資產

更新日期: 2014年8月

本主題顯示從儲存體帳戶複製 blob 至新 Microsoft Azure Media Services 資產的方法。

您的 blob 可存在於與 Media Services 帳戶相關聯的儲存體帳戶,或未與 Media Services 帳戶相關聯的儲存體帳戶。此主題示範如何將二進位大型物件從儲存體帳戶複製到 Media Services 資產。請注意,您也可以跨「資料中心」進行複製。但是,這麼做可能會產生某些費用。如需有關定價的詳細資訊,請參閱資料傳輸

Important重要事項
您不應該嘗試變更 Blob 容器的內容,此內容由 Media Services 在不使用 Media Service API 的情況下產生。

  • 在新的或現有的 Azure 訂閱中的兩個 Media Services 帳戶。請參閱「如何建立 Media Services 帳戶」主題。

  • 作業系統:Windows 7、Windows 2008 R2 或 Windows 8。

  • .NET Framework 4.5 或 .NET Framework 4。

  • Visual Studio 2012 或 Visual Studio 2010 SP1 (Professional、Premium、Ultimate 或 Express)。

在本節中,您會建立並設定 C# [主控台應用程式] 專案。

  1. 使用 Visual Studio 2012 或 Visual Studio 2010 SP1 來建立新的解決方案,其包含 C# 主控台應用程式專案。在 [名稱] 中輸入 CopyExistingBlobsIntoAsset,然後按一下 [確定]。

  2. 使用 Nuget 來新增參照到 Media Services 相關的 DLL。在 Visual Studio 主功能表中,選取 [工具] -> [程式庫套件管理員] -> [Package Manager Console]。在此主控台視窗中,輸入 Install-Package windowsazure.mediaservices,然後按 ENTER 鍵。

  3. 新增此專案所需的其他參照:System.Configuration.

  4. 使用下列陳述式取代預設新增至 Programs.cs 檔案的陳述式:

    using System;
    using System.Linq;
    using System.Configuration;
    using System.IO;
    using System.Text;
    using System.Threading;
    using System.Threading.Tasks;
    using System.Collections.Generic;
    using Microsoft.WindowsAzure.Storage;
    using Microsoft.WindowsAzure.MediaServices.Client;
    using Microsoft.WindowsAzure;
    using System.Web;
    using Microsoft.WindowsAzure.Storage.Blob;
    using Microsoft.WindowsAzure.Storage.Auth;
    
  5. 新增 appSettings 區段至 .config 檔案,並依您的 Media Services 和儲存體金鑰和名稱值來更新值。

    <appSettings>
      <add key="MediaServicesAccountName" value="Media-Services-Account-Name"/>
      <add key="MediaServicesAccountKey" value="Media-Services-Account-Key"/>
      <add key="MediaServicesStorageAccountName" value="Media-Services-Storage-Account-Name"/>
      <add key="MediaServicesStorageAccountKey" value="Media-Services-Storage-Account-Key"/>
      <add key="ExternalStorageAccountName" value="External-Storage-Account-Name"/>
      <add key="ExternalStorageAccountKey" value="External-Storage-Account-Key"/>
    </appSettings>
    

以下的程式碼範例執行下列任務:

  1. 建立 CloudMediaContext 執行個體。

  2. 建立 CloudStorageAccount 執行個體:_sourceStorageAccount 和 _destinationStorageAccount。

  3. 將 Smooth Streaming 檔案從本機目錄上傳至位於 _sourceStorageAccount 的二進位大型物件容器中。

  4. 建立新資產。為此資產建立的二進位大型物件容器位於 _destinationStorageAccount 中。

  5. 使用 Azure 儲存體 SDK 將指定的二進位大型物件複製到與資產相關聯的容器。

    Warning警告
    如果定位器已過期,則複製作業不會擲回例外狀況。

  6. 設定 .ism 檔案為主要檔案。

  7. 建立與資產相關聯的 OnDemandOrigin 定位器的 Smooth Streaming URL。

class Program
{
    // Read values from the App.config file. 
    static string _accountName = ConfigurationManager.AppSettings["MediaServicesAccountName"];
    static string _accountKey = ConfigurationManager.AppSettings["MediaServicesAccountKey"];
    static string _storageAccountName = ConfigurationManager.AppSettings["MediaServicesStorageAccountName"];
    static string _storageAccountKey = ConfigurationManager.AppSettings["MediaServicesStorageAccountKey"];
    static string _externalStorageAccountName = ConfigurationManager.AppSettings["ExternalStorageAccountName"];
    static string _externalStorageAccountKey = ConfigurationManager.AppSettings["ExternalStorageAccountKey"];

    private static MediaServicesCredentials _cachedCredentials = null;
    private static CloudMediaContext _context = null;

    private static CloudStorageAccount _sourceStorageAccount = null;
    private static CloudStorageAccount _destinationStorageAccount = null;

    static void Main(string[] args)
    {
        _cachedCredentials = new MediaServicesCredentials(
                        _accountName,
                        _accountKey);
        // Use the cached credentials to create CloudMediaContext.
        _context = new CloudMediaContext(_cachedCredentials);

        // In this example the storage account from which we copy blobs is not 
        // associated with the Media Services account into which we copy blobs.
        // But the same code will work for coping blobs from a storage account that is 
        // associated with the Media Services account.
        //
        // Get a reference to a storage account that is not associated with a Media Services account
        // (an external account).  
        StorageCredentials externalStorageCredentials =
            new StorageCredentials(_externalStorageAccountName, _externalStorageAccountKey);
        _sourceStorageAccount = new CloudStorageAccount(externalStorageCredentials, true);

        //Get a reference to the storage account that is associated with a Media Services account. 
        StorageCredentials mediaServicesStorageCredentials =
            new StorageCredentials(_storageAccountName, _storageAccountKey);
        _destinationStorageAccount = new CloudStorageAccount(mediaServicesStorageCredentials, false);

        // Upload Smooth Streaming files into a storage account.
        string localMediaDir = @"C:\supportFiles\streamingfiles";
        CloudBlobContainer blobContainer =
            UploadContentToStorageAccount(localMediaDir);

        // Create a new asset and copy the smooth streaming files into 
        // the container that is associated with the asset.
        IAsset asset = CreateAssetFromExistingBlobs(blobContainer);

        // Get the streaming URL for the smooth streaming files 
        // that were copied into the asset.   
        string urlForClientStreaming = CreateStreamingLocator(asset);
        Console.WriteLine("Smooth Streaming URL: " + urlForClientStreaming);

        Console.ReadLine();
    }

    /// <summary>
    /// Uploads content from a local directory into the specified storage account.
    /// In this example the storage account is not associated with the Media Services account.
    /// </summary>
    /// <param name="localPath">The path from which to upload the files.</param>
    /// <returns>The container that contains the uploaded files.</returns>
    static public CloudBlobContainer UploadContentToStorageAccount(string localPath)
    {
        CloudBlobClient externalCloudBlobClient = _sourceStorageAccount.CreateCloudBlobClient();
        CloudBlobContainer externalMediaBlobContainer =
            externalCloudBlobClient.GetContainerReference(externalCloudBlobClient.BaseUri + "streamingfiles");

        externalMediaBlobContainer.CreateIfNotExists();

        // Upload files to the blob container.  
        DirectoryInfo uploadDirectory = new DirectoryInfo(localPath);
        foreach (var file in uploadDirectory.EnumerateFiles())
        {
            CloudBlockBlob blob = externalMediaBlobContainer.GetBlockBlobReference(file.Name);

            blob.UploadFromFile(file.FullName, FileMode.Open);
        }

        return externalMediaBlobContainer;
    }

    /// <summary>
    /// Creates a new asset and copies blobs from the specifed storage account.
    /// </summary>
    /// <param name="mediaBlobContainer">The specified blob container.</param>
    /// <returns>The new asset.</returns>
    static public IAsset CreateAssetFromExistingBlobs(CloudBlobContainer mediaBlobContainer)
    {
        // Create a new asset. 
        IAsset asset = _context.Assets.Create("NewAsset_" + Guid.NewGuid(), AssetCreationOptions.None);

        IAccessPolicy writePolicy = _context.AccessPolicies.Create("writePolicy",
            TimeSpan.FromHours(24), AccessPermissions.Write);
        ILocator destinationLocator = _context.Locators.CreateLocator(LocatorType.Sas, asset, writePolicy);

        CloudBlobClient destBlobStorage = _destinationStorageAccount.CreateCloudBlobClient();

        // Get the asset container URI and Blob copy from mediaContainer to assetContainer. 
        string destinationContainerName = (new Uri(destinationLocator.Path)).Segments[1];

        CloudBlobContainer assetContainer =
            destBlobStorage.GetContainerReference(destinationContainerName);

        if (assetContainer.CreateIfNotExists())
        {
            assetContainer.SetPermissions(new BlobContainerPermissions
            {
                PublicAccess = BlobContainerPublicAccessType.Blob
            });
        }

        var blobList = mediaBlobContainer.ListBlobs();
        foreach (var sourceBlob in blobList)
        {
            var assetFile = asset.AssetFiles.Create((sourceBlob as ICloudBlob).Name);
            CopyBlob(sourceBlob as ICloudBlob, assetContainer);
        }

        destinationLocator.Delete();
        writePolicy.Delete();

        // Since we copied a set of Smooth Streaming files, 
        // set the .ism file to be the primary file. 
        SetISMFileAsPrimary(asset);

        return asset;
    }

    /// <summary>
    /// Creates the OnDemandOrigin locator in order to get the streaming URL.
    /// </summary>
    /// <param name="asset">The asset that contains the smooth streaming files.</param>
    /// <returns>The streaming URL.</returns>
    static public string CreateStreamingLocator(IAsset asset)
    {
        var ismAssetFile = asset.AssetFiles.ToList().
            Where(f => f.Name.EndsWith(".ism", StringComparison.OrdinalIgnoreCase)).First();

        // Create a 30-day readonly access policy. 
        IAccessPolicy policy = _context.AccessPolicies.Create("Streaming policy",
            TimeSpan.FromDays(30),
            AccessPermissions.Read);

        // Create a locator to the streaming content on an origin. 
        ILocator originLocator = _context.Locators.CreateLocator(LocatorType.OnDemandOrigin, asset,
            policy,
            DateTime.UtcNow.AddMinutes(-5));

        return originLocator.Path + ismAssetFile.Name + "/manifest";
    }

    /// <summary>
    /// Copies the specified blob into the specified container.
    /// </summary>
    /// <param name="sourceBlob">The source container.</param>
    /// <param name="destinationContainer">The destination container.</param>
    static private void CopyBlob(ICloudBlob sourceBlob, CloudBlobContainer destinationContainer)
    {
        var signature = sourceBlob.GetSharedAccessSignature(new SharedAccessBlobPolicy
        {
            Permissions = SharedAccessBlobPermissions.Read,
            SharedAccessExpiryTime = DateTime.UtcNow.AddHours(24)
        });

        ICloudBlob destinationBlob = destinationContainer.GetBlockBlobReference(sourceBlob.Name);

        if (destinationBlob.Exists())
        {
            Console.WriteLine(string.Format("Destination blob '{0}' already exists. Skipping.", destinationBlob.Uri));
        }
        else
        {
            try
            {
                Console.WriteLine(string.Format("Copy blob '{0}' to '{1}'", sourceBlob.Uri, destinationBlob.Uri));
                destinationBlob.StartCopyFromBlob(new Uri(sourceBlob.Uri.AbsoluteUri + signature));
            }
            catch (Exception ex)
            {
                Console.WriteLine(string.Format("Error copying blob '{0}': {1}", sourceBlob.Name, ex.Message));
            }
        }
    }

    /// <summary>
    /// Sets a file with the .ism extension as a primary file.
    /// </summary>
    /// <param name="asset">The asset that contains the smooth streaming files.</param>
    static private void SetISMFileAsPrimary(IAsset asset)
    {
        var ismAssetFiles = asset.AssetFiles.ToList().
            Where(f => f.Name.EndsWith(".ism", StringComparison.OrdinalIgnoreCase)).ToArray();

        if (ismAssetFiles.Count() != 1)
            throw new ArgumentException("The asset should have only one, .ism file");

        ismAssetFiles.First().IsPrimary = true;
        ismAssetFiles.First().Update();
    }
}

另請參閱

Microsoft 正展開一份線上問卷調查,了解您對於 MSDN 網站的看法。 如果您選擇參加,您離開 MSDN 網站時即會顯示線上問卷調查。

您是否想要參加?
顯示:
© 2014 Microsoft