Beispiel #1
0
 public JsonResult Create(int assetCount, string assetType, bool assetPublish)
 {
     try
     {
         string authToken = HomeController.GetAuthToken(Request, Response);
         using (MediaClient mediaClient = new MediaClient(authToken))
         {
             StorageBlobClient sourceBlobClient = new StorageBlobClient();
             StorageBlobClient assetBlobClient  = new StorageBlobClient(mediaClient.MediaAccount, mediaClient.PrimaryStorageAccount);
             for (int i = 1; i <= assetCount; i++)
             {
                 List <Task> uploadTasks   = new List <Task>();
                 string      containerName = Constant.Storage.BlobContainer.MediaServices;
                 string      directoryPath = assetType;
                 string      assetName     = MediaAsset.GetAssetName(sourceBlobClient, containerName, directoryPath, out MediaFile[] sourceFiles);
                 assetName = string.Concat(i.ToString(), Constant.Media.Asset.NameDelimiter, assetName);
                 Asset asset = mediaClient.CreateAsset(mediaClient.PrimaryStorageAccount, assetName);
                 foreach (MediaFile sourceFile in sourceFiles)
                 {
                     CloudBlockBlob sourceBlob   = sourceBlobClient.GetBlockBlob(containerName, directoryPath, sourceFile.Name);
                     CloudBlockBlob assetBlob    = assetBlobClient.GetBlockBlob(asset.Container, null, sourceFile.Name);
                     Stream         sourceStream = sourceBlob.OpenReadAsync().Result;
                     Task           uploadTask   = assetBlob.UploadFromStreamAsync(sourceStream);
                     uploadTasks.Add(uploadTask);
                 }
                 if (assetPublish)
                 {
                     Task.WaitAll(uploadTasks.ToArray());
                     string streamingPolicyName = assetType == Constant.Media.Asset.SingleBitrate ? PredefinedStreamingPolicy.DownloadOnly : PredefinedStreamingPolicy.ClearStreamingOnly;
                     mediaClient.CreateLocator(asset.Name, asset.Name, streamingPolicyName, null);
                 }
             }
         }
         return(Json(assetCount));
     }
     catch (ApiErrorException ex)
     {
         return(new JsonResult(ex.Response.Content)
         {
             StatusCode = (int)ex.Response.StatusCode
         });
     }
 }