public void LocatorCRUD() { ILocator stubed = _mediaContext.Locators.FirstOrDefault(); Assert.IsNotNull(stubed); Assert.IsNotNull(stubed.Asset); Assert.IsNotNull(stubed.AccessPolicy); IAsset asset = _mediaContext.Assets.Create("Test", AssetCreationOptions.None); IAccessPolicy policy = _mediaContext.AccessPolicies.Create("Test", TimeSpan.FromDays(1), AccessPermissions.Read); ILocator locator = _mediaContext.Locators.CreateLocator(LocatorType.OnDemandOrigin, asset, policy); Assert.IsNotNull(locator.AccessPolicy); Assert.IsNotNull(locator.Asset); locator.Update(DateTime.UtcNow.AddDays(5)); locator.Update(DateTime.UtcNow.AddDays(1), DateTime.UtcNow.AddDays(5)); locator.UpdateAsync(DateTime.UtcNow.AddDays(5)); locator.Delete(); Assert.IsNull(_mediaContext.Locators.Where(c => c.Id == locator.Id).FirstOrDefault()); ILocator sas = _mediaContext.Locators.CreateSasLocator(asset, policy); sas.Delete(); sas = _mediaContext.Locators.CreateSasLocatorAsync(asset, policy).Result; sas.Delete(); ILocator sasAsync = _mediaContext.Locators.CreateSasLocatorAsync(asset, policy, DateTime.UtcNow, "Name").Result; sasAsync.DeleteAsync().Wait(); }
public void ShouldDownloadSameAssetFile2TimesIdenticallyAsStorageSDK() { IAsset asset = _mediaContext.Assets.Create("Test", AssetCreationOptions.None); VerifyAsset(asset); IAccessPolicy policy = _mediaContext.AccessPolicies.Create("temp", TimeSpan.FromMinutes(10), AccessPermissions.Write); ILocator locator = _mediaContext.Locators.CreateSasLocator(asset, policy); UploadFile(locator, asset, _smallWmv, _mediaContext); UploadFile(locator, asset, WindowsAzureMediaServicesTestConfiguration.SmallWmv2, _mediaContext); IAssetFile assetFile = asset.AssetFiles.FirstOrDefault(); Assert.IsNotNull(assetFile); assetFile.IsPrimary = true; assetFile.Update(); locator.Delete(); policy.Delete(); IAsset refreshedAsset = RefreshedAsset(asset); Assert.AreEqual(2, refreshedAsset.AssetFiles.Count(), "file count wrong"); for (int i = 0; i < 2; i++) { VerifyAndDownloadAsset(refreshedAsset, 2); } }
private async Task <WamsLocatorInfo> CreateLocatorAsync(IAsset asset, string accessPolicyName, LocatorType type, TimeSpan duration) { IAccessPolicy accessPolicy = null; ILocator locator = null; try { accessPolicy = await Context.AccessPolicies.CreateAsync(accessPolicyName, duration, AccessPermissions.Read | AccessPermissions.List).ConfigureAwait(continueOnCapturedContext: false); locator = await Context.Locators.CreateLocatorAsync(type, asset, accessPolicy).ConfigureAwait(continueOnCapturedContext: false); Logger.Information("New {0} locator with duration {1} was created for asset '{2}'", type, duration, asset.Name); return(new WamsLocatorInfo(locator.Id, locator.Path)); } catch (Exception ex) { Logger.Error(ex, "Error while creating locator for asset '{0}'. Cleaning up any created locator and access policy.", asset.Name); try { if (locator != null) { locator.Delete(); } if (accessPolicy != null) { accessPolicy.Delete(); } } catch (Exception iex) { Logger.Warning(iex, "Error while cleaning up created locator and access policy."); } throw; } }
public void ShouldCreateEmptyAssetUploadTwoFilesSetPrimaryAndDownloadFile() { IAsset asset = _mediaContext.Assets.Create("Test", AssetCreationOptions.None); VerifyAsset(asset); IAccessPolicy policy = _mediaContext.AccessPolicies.Create("temp", TimeSpan.FromMinutes(10), AccessPermissions.Write); ILocator locator = _mediaContext.Locators.CreateSasLocator(asset, policy); UploadFile(locator, asset, _smallWmv, _mediaContext); //asset = _dataContext.Assets.Where(c => c.Id == asset.Id).FirstOrDefault(); UploadFile(locator, asset, WindowsAzureMediaServicesTestConfiguration.SmallWmv2, _mediaContext); Assert.AreEqual(2, asset.AssetFiles.Count()); IAssetFile assetFile = asset.AssetFiles.ToList()[1]; assetFile.IsPrimary = true; assetFile.Update(); locator.Delete(); policy.Delete(); IAsset refreshedAsset = RefreshedAsset(asset); Assert.AreEqual(2, refreshedAsset.AssetFiles.Count(), "file count wrong"); VerifyAndDownloadAsset(refreshedAsset, 2); }
private IAsset CreateMediaAsset(CloudFile model) { CloudMediaContext context = new CloudMediaContext(mediaAccountName, mediaAccountKey); CloudStorageAccount storageAccount = CloudStorageAccount.Parse(storageConnectionString); CloudBlobClient cloudBlobClient = storageAccount.CreateCloudBlobClient(); CloudBlobContainer mediaBlobContainer = cloudBlobClient.GetContainerReference(storageContainerReference); mediaBlobContainer.CreateIfNotExists(); // Create a new asset. IAsset asset = context.Assets.Create("UploadedVideo-" + Guid.NewGuid(), AssetCreationOptions.None); IAccessPolicy writePolicy = context.AccessPolicies.Create("writePolicy", TimeSpan.FromMinutes(120), AccessPermissions.Write); ILocator destinationLocator = context.Locators.CreateLocator(LocatorType.Sas, asset, writePolicy); // Get the asset container URI and copy blobs from mediaContainer to assetContainer. Uri uploadUri = new Uri(destinationLocator.Path); string assetContainerName = uploadUri.Segments[1]; CloudBlobContainer assetContainer = cloudBlobClient.GetContainerReference(assetContainerName); string fileName = HttpUtility.UrlDecode(Path.GetFileName(model.BlockBlob.Uri.AbsoluteUri)); var sourceCloudBlob = mediaBlobContainer.GetBlockBlobReference(fileName); sourceCloudBlob.FetchAttributes(); if (sourceCloudBlob.Properties.Length > 0) { IAssetFile assetFile = asset.AssetFiles.Create(fileName); var destinationBlob = assetContainer.GetBlockBlobReference(fileName); destinationBlob.DeleteIfExists(); destinationBlob.StartCopyFromBlob(sourceCloudBlob); destinationBlob.FetchAttributes(); if (sourceCloudBlob.Properties.Length != destinationBlob.Properties.Length) { model.UploadStatusMessage += "Failed to copy as Media Asset!"; } } destinationLocator.Delete(); writePolicy.Delete(); sourceCloudBlob.Delete(); //delete temp blob // Refresh the asset. asset = context.Assets.Where(a => a.Id == asset.Id).FirstOrDefault(); var ismAssetFiles = asset.AssetFiles.ToList().Where(f => f.Name.EndsWith(".mp4", 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(); model.UploadStatusMessage += " Media file uploaded successfully by id: " + asset.Id; model.AssetId = asset.Id; return(asset); }
/// <summary> /// Downloads the represented file to the specified destination path. /// </summary> /// <param name="destinationPath">The path to download the file to.</param> public void Download(string destinationPath) { IAccessPolicy accessPolicy = null; ILocator locator = null; try { accessPolicy = this.GetMediaContext().AccessPolicies.Create("SdkDownload", TimeSpan.FromHours(12), AccessPermissions.Read); locator = this.GetMediaContext().Locators.CreateSasLocator(this.Asset, accessPolicy); BlobTransferClient blobTransfer = this.GetMediaContext().MediaServicesClassFactory.GetBlobTransferClient(); blobTransfer.NumberOfConcurrentTransfers = this.GetMediaContext().NumberOfConcurrentTransfers; blobTransfer.ParallelTransferThreadCount = this.GetMediaContext().ParallelTransferThreadCount; this.DownloadAsync(destinationPath, blobTransfer, locator, CancellationToken.None).Wait(); } catch (AggregateException exception) { throw exception.Flatten(); } finally { if (locator != null) { locator.Delete(); } if (accessPolicy != null) { accessPolicy.Delete(); } } }
/// <summary> /// Removes locators, delivery policies and keys associated with an asset /// </summary> /// <param name="asset"></param> private static void CleanUpAsset(IAsset asset) { foreach (var locator in asset.Locators) { ILocator locatorRefreshed = _context.Locators.Where(p => p.Id == locator.Id).FirstOrDefault(); if (locatorRefreshed != null) { locatorRefreshed.Delete(); } } var deliveryPolicies = asset.DeliveryPolicies.ToList(); foreach (var deliveryPolicy in deliveryPolicies) { asset.DeliveryPolicies.Remove(deliveryPolicy); var deliveryPolicyRefreshed = _context.AssetDeliveryPolicies.Where(p => p.Id == deliveryPolicy.Id).FirstOrDefault(); if (deliveryPolicyRefreshed != null) { deliveryPolicyRefreshed.Delete(); } } var keys = asset.ContentKeys.ToList(); foreach (var key in keys) { asset.ContentKeys.Remove(key); IContentKey keyRefreshed = _context.ContentKeys.Where(p => p.Id == key.Id).FirstOrDefault(); if (keyRefreshed != null) { keyRefreshed.Delete(); } } }
private string GetSasUrl(IAsset asset) { // Create an 1-day readonly access policy. IAccessPolicy streamingPolicy = mediaContext.AccessPolicies.Create("Full Access Policy", TimeSpan.FromMinutes(20), AccessPermissions.List | AccessPermissions.Write | AccessPermissions.Read); // Create the origin locator. Set the start time as 5 minutes // before the present so that the locator can be accessed immediately // if there is clock skew between the client and server. ILocator sasLocator = (from l in mediaContext.Locators where l.Type == LocatorType.Sas && l.AssetId == asset.Id select l).FirstOrDefault(); if (sasLocator != null && sasLocator.ExpirationDateTime < DateTime.UtcNow) { sasLocator.Delete(); sasLocator = null; } if (sasLocator == null) { sasLocator = mediaContext .Locators.CreateSasLocator(asset, streamingPolicy, DateTime.UtcNow.AddMinutes(-5)); } // Create a full URL to the manifest file. Use this for playback // in streaming media clients. string sasUrl = sasLocator.Path; return(sasUrl); }
public void ShouldCreateEncryptedInitilizedAsset() { IAsset asset = _mediaContext.Assets.Create("Test", AssetCreationOptions.StorageEncrypted); Assert.IsNotNull(asset, "Asset should be non null"); Assert.AreNotEqual(Guid.Empty, asset.Id, "Asset ID shuold not be null"); Assert.AreEqual(0, asset.AssetFiles.Count(), "Asset has files"); Assert.AreEqual(AssetState.Initialized, asset.State, "Expecting initilized state"); IAccessPolicy policy = _mediaContext.AccessPolicies.Create("temp", TimeSpan.FromMinutes(10), AccessPermissions.Write); ILocator locator = _mediaContext.Locators.CreateSasLocator(asset, policy); IAssetFile file = asset.AssetFiles.Create(Path.GetFileName(_smallWmv)); Task task = file.UploadAsync(_smallWmv, _mediaContext.MediaServicesClassFactory.GetBlobTransferClient(), locator, CancellationToken.None); task.Wait(); Assert.IsTrue(task.IsCompleted); Assert.IsTrue(!task.IsFaulted); locator.Delete(); policy.Delete(); IAsset refreshedAsset = _mediaContext.Assets.Where(c => c.Id == asset.Id).FirstOrDefault(); Assert.AreEqual(asset.Name, refreshedAsset.Name); Assert.AreEqual(AssetState.Initialized, refreshedAsset.State); Assert.AreEqual(1, refreshedAsset.AssetFiles.Count(), "file count wrong"); VerifyAndDownloadAsset(refreshedAsset, 1, false); ContentKeyTests.VerifyFileAndContentKeyMetadataForStorageEncryption(refreshedAsset, _mediaContext); }
public static async Task <IAsset> CreateAsset(CloudBlockBlob blob, string assetName, TraceWriter log) { // Create a new asset. var asset = _mediaServiceContext.Assets.Create(assetName, AssetCreationOptions.None); log.Info($"Created new asset {asset.Name}"); IAccessPolicy writePolicy = _mediaServiceContext.AccessPolicies.Create("writePolicy", TimeSpan.FromHours(4), AccessPermissions.Write); ILocator destinationLocator = _mediaServiceContext.Locators.CreateLocator(LocatorType.Sas, asset, writePolicy); _storageAccount = CloudStorageAccount.Parse(_connectionString); CloudBlobClient destBlobStorage = _storageAccount.CreateCloudBlobClient(); // Get the destination asset container reference string destinationContainerName = (new Uri(destinationLocator.Path)).Segments[1]; CloudBlobContainer assetContainer = destBlobStorage.GetContainerReference(destinationContainerName); try { assetContainer.CreateIfNotExists(); log.Info($"Created new container {destinationContainerName}"); } catch (Exception ex) { log.Error($"!!!ERROR!!!: {ex.Message}"); } //// Get hold of the destination blob CloudBlockBlob destinationBlob = assetContainer.GetBlockBlobReference(assetName); // Copy Blob try { using (var stream = await blob.OpenReadAsync()) { await destinationBlob.UploadFromStreamAsync(stream); } log.Info("Copy Complete."); var assetFile = asset.AssetFiles.Create(assetName); assetFile.ContentFileSize = blob.Properties.Length; assetFile.IsPrimary = true; assetFile.Update(); asset.Update(); } catch (Exception ex) { log.Error(ex.Message); log.Info(ex.StackTrace); log.Info("Copy Failed."); throw; } destinationLocator.Delete(); writePolicy.Delete(); return(asset); }
/// <summary> /// Copies the files in the <paramref name="sourceAsset"/> into into the <paramref name="destinationAsset"/> instance. /// </summary> /// <param name="sourceAsset">The <see cref="IAsset"/> instance that contains the asset files to copy.</param> /// <param name="destinationAsset">The <see cref="IAsset"/> instance that receives asset files.</param> /// <param name="destinationStorageCredentials">The <see cref="Microsoft.WindowsAzure.Storage.Auth.StorageCredentials"/> instance for the <paramref name="destinationAsset"/> Storage Account.</param> /// <param name="cancellationToken">The <see cref="System.Threading.CancellationToken"/> instance used for cancellation.</param> /// <returns>A <see cref="System.Threading.Tasks.Task"/> instance to copy the files in the <paramref name="sourceAsset"/> into into the <paramref name="destinationAsset"/> instance.</returns> public static async Task CopyAsync(this IAsset sourceAsset, IAsset destinationAsset, StorageCredentials destinationStorageCredentials, CancellationToken cancellationToken) { if (sourceAsset == null) { throw new ArgumentNullException("sourceAsset", "The source asset cannot be null."); } if (destinationAsset == null) { throw new ArgumentNullException("destinationAsset", "The destination asset cannot be null."); } if (destinationStorageCredentials == null) { throw new ArgumentNullException("destinationStorageCredentials", "The destination storage credentials cannot be null."); } if (destinationStorageCredentials.IsAnonymous || destinationStorageCredentials.IsSAS) { throw new ArgumentException("The destination storage credentials must contain the account key credentials.", "destinationStorageCredentials"); } if (!string.IsNullOrWhiteSpace(destinationStorageCredentials.AccountName) && !destinationStorageCredentials.AccountName.Equals(destinationAsset.StorageAccountName, StringComparison.OrdinalIgnoreCase)) { throw new ArgumentException("The destination storage credentials does not belong to the destination asset storage account.", "destinationStorageCredentials"); } MediaContextBase sourceContext = sourceAsset.GetMediaContext(); ILocator sourceLocator = null; try { sourceLocator = await sourceContext.Locators.CreateAsync(LocatorType.Sas, sourceAsset, AccessPermissions.Read | AccessPermissions.List, AssetBaseCollectionExtensions.DefaultAccessPolicyDuration).ConfigureAwait(false); cancellationToken.ThrowIfCancellationRequested(); IRetryPolicy retryPolicy = sourceContext.MediaServicesClassFactory.GetBlobStorageClientRetryPolicy().AsAzureStorageClientRetryPolicy(); BlobRequestOptions options = new BlobRequestOptions { RetryPolicy = retryPolicy }; CloudBlobContainer sourceContainer = new CloudBlobContainer(sourceAsset.Uri, new StorageCredentials(sourceLocator.ContentAccessComponent)); CloudBlobContainer destinationContainer = new CloudBlobContainer(destinationAsset.Uri, destinationStorageCredentials); await CopyBlobHelpers.CopyBlobsAsync(sourceContainer, destinationContainer, options, cancellationToken).ConfigureAwait(false); cancellationToken.ThrowIfCancellationRequested(); await CopyAssetFilesAsync(sourceAsset, destinationAsset, cancellationToken).ConfigureAwait(false); } finally { if (sourceLocator != null) { sourceLocator.Delete(); } } }
/// <summary> /// Uploads a stream asynchronously /// </summary> /// <param name="name">Name for the stream</param> /// <param name="stream">Stream to be uploaded</param> /// <param name="cancellationToken">A <see cref="CancellationToken"/> to use for canceling upload operation.</param> /// <returns>A function delegate that returns the future result to be available through the Task.</returns> public Task UploadAsync(Stream stream, CancellationToken cancellationToken) { if (stream == null) { throw new ArgumentNullException("stream"); } if (this.IsFragmented()) { throw new NotSupportedException(StringTable.NotSupportedFragblobUpload); } IAccessPolicy accessPolicy = null; ILocator locator = null; var policyName = "SdkUpload" + Guid.NewGuid().ToString(); return(GetMediaContext().AccessPolicies .CreateAsync(policyName, TimeSpan.FromHours(12), AccessPermissions.Write) .ContinueWith <ILocator>( t => { accessPolicy = t.Result; t.ThrowIfFaulted(() => this.Cleanup(null, null, locator, accessPolicy)); cancellationToken.ThrowIfCancellationRequested(() => this.Cleanup(null, null, locator, accessPolicy)); locator = this.GetMediaContext().Locators.CreateSasLocator(this.Asset, accessPolicy); cancellationToken.ThrowIfCancellationRequested(() => this.Cleanup(null, null, locator, accessPolicy)); return locator; }, cancellationToken). ContinueWith( t => { locator = t.Result; t.ThrowIfFaulted(() => this.Cleanup(null, null, locator, accessPolicy)); cancellationToken.ThrowIfCancellationRequested(() => this.Cleanup(null, null, locator, accessPolicy)); var blobTransfer = GetMediaContext().MediaServicesClassFactory.GetBlobTransferClient(); blobTransfer.NumberOfConcurrentTransfers = this.GetMediaContext().NumberOfConcurrentTransfers; blobTransfer.ParallelTransferThreadCount = this.GetMediaContext().ParallelTransferThreadCount; UploadAsync(stream, blobTransfer, locator, cancellationToken).Wait(); locator.Delete(); cancellationToken.ThrowIfCancellationRequested(() => this.Cleanup(null, null, null, accessPolicy)); accessPolicy.Delete(); }, cancellationToken)); }
private void Subclipping_FormClosed(object sender, FormClosedEventArgs e) { if (_tempLocator != null) { try { _tempLocator.Delete(); } catch { } } }
public string GetMp4StreamingOriginLocator(Models.Asset assetToStream) { // Get a reference to the manifest file from the collection // of streaming files in the asset. var theManifest = from f in assetToStream.MediaAsset.AssetFiles where f.Name.EndsWith(".mp4") select f; // Cast the reference to a true IFileInfo type. IAssetFile manifestFile = theManifest.First(); // Create an 1-day readonly access policy. IAccessPolicy streamingPolicy = this.MediaService.MediaContext.AccessPolicies.Create("Readonly 1 hour policy", TimeSpan.FromHours(1), AccessPermissions.Read); // Create the origin locator. Set the start time as 5 minutes // before the present so that the locator can be accessed immediately // if there is clock skew between the client and server. ILocator originLocator = (from l in this.MediaService.MediaContext.Locators where l.AssetId.Equals(assetToStream.MediaAsset.Id) && l.Type == LocatorType.Sas select l).FirstOrDefault(); if (originLocator != null && originLocator.ExpirationDateTime <= DateTime.UtcNow) { originLocator.Delete(); originLocator = null; } if (originLocator == null) { originLocator = this.MediaService.MediaContext .Locators.CreateSasLocator(assetToStream.MediaAsset, streamingPolicy, DateTime.UtcNow.AddMinutes(-5)); } // Create a full URL to the manifest file. Use this for playback // in streaming media clients. UriBuilder bldr = new UriBuilder(originLocator.Path); bldr.Path += "/" + manifestFile.Name; string urlForClientStreaming = bldr.ToString(); // Display the full URL to the streaming manifest file. Console.WriteLine("URL to for progressive download: "); Console.WriteLine(urlForClientStreaming); return(urlForClientStreaming); }
public string GetSmoothStreamingAzureCDNLocator(Models.Asset assetToStream) { // Get a reference to the manifest file from the collection // of streaming files in the asset. var manifestFile = assetToStream.MediaAsset.AssetFiles.Where(x => x.Name.EndsWith(".ism")).FirstOrDefault(); // Cast the reference to a true IFileInfo type. if (null == manifestFile) { return(null); } // Create an 1-day readonly access policy. IAccessPolicy streamingPolicy = this.MediaService.MediaContext.AccessPolicies.Create("CDN Streaming policy", TimeSpan.FromHours(1), AccessPermissions.Read); // Create the origin locator. Set the start time as 5 minutes // before the present so that the locator can be accessed immediately // if there is clock skew between the client and server. ILocator originLocator = (from l in this.MediaService.MediaContext.Locators where l.AssetId.Equals(assetToStream.MediaAsset.Id) && l.Type == LocatorType.None select l).FirstOrDefault(); if (originLocator != null && originLocator.ExpirationDateTime <= DateTime.UtcNow) { originLocator.Delete(); originLocator = null; } if (originLocator == null) { originLocator = this.MediaService.MediaContext .Locators.CreateLocator(LocatorType.None, assetToStream.MediaAsset, streamingPolicy, DateTime.UtcNow.AddMinutes(-5)); } // Create a full URL to the manifest file. Use this for playback // in streaming media clients. string urlForClientStreaming = originLocator.Path + manifestFile.Name + "/manifest"; // Display the full URL to the streaming manifest file. Console.WriteLine("URL to manifest for client streaming: "); Console.WriteLine(urlForClientStreaming); return(urlForClientStreaming); }
/// <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); assetFile.ContentFileSize = (sourceBlob as ICloudBlob).Properties.Length; assetFile.Update(); } asset.Update(); destinationLocator.Delete(); writePolicy.Delete(); // Since we copied a set of Smooth Streaming files, // set the .ism file to be the primary file. // If we, for example, copied an .mp4, then the mp4 would be the primary file. SetISMFileAsPrimary(asset); return(asset); }
private void DeleteSASLocator(ILocator locator) { if (locator != null) { try { var locatorTask = Task.Factory.StartNew(() => { locator.Delete(); }); locatorTask.Wait(); } catch { } } }
public void ShouldRecreateLocatorWithSameLocatorId() { IAsset asset = AssetTests.CreateAsset(_mediaContext, _smallWmv, AssetCreationOptions.None); IAccessPolicy accessPolicy = _mediaContext.AccessPolicies.Create("Read", TimeSpan.FromMinutes(5), AccessPermissions.Read); ILocator locator = _mediaContext.Locators.CreateLocator(LocatorType.OnDemandOrigin, asset, accessPolicy); Assert.IsNotNull(locator); string id = locator.Id; locator.Delete(); Assert.IsNull(_mediaContext.Locators.Where(c => c.Id == id).FirstOrDefault()); locator = _mediaContext.Locators.CreateLocator(id, LocatorType.OnDemandOrigin, asset, accessPolicy, null); Assert.IsNotNull(locator); }
private void Subclipping_FormClosed(object sender, FormClosedEventArgs e) { if (_tempLocator != null) { try { _tempLocator.Delete(); } catch { } } // let's sure we dispose the webbrowser control webBrowserPreview.Url = null; webBrowserPreview.Dispose(); }
/// <summary> /// Uploads the file with given path asynchronously /// </summary> /// <param name="path">The path of a file to upload</param> /// <param name="cancellationToken">A <see cref="CancellationToken"/> to use for canceling upload operation.</param> /// <returns>A function delegate that returns the future result to be available through the Task.</returns> internal Task UploadAsync(string path, CancellationToken cancellationToken) { ValidateFileName(path); IAccessPolicy accessPolicy = null; ILocator locator = null; var policyName = "SdkUpload" + Guid.NewGuid().ToString(); return(GetMediaContext().AccessPolicies .CreateAsync(policyName, TimeSpan.FromHours(12), AccessPermissions.Write) .ContinueWith <ILocator>( t => { accessPolicy = t.Result; t.ThrowIfFaulted(() => this.Cleanup(null, null, locator, accessPolicy)); cancellationToken.ThrowIfCancellationRequested(() => this.Cleanup(null, null, locator, accessPolicy)); locator = this.GetMediaContext().Locators.CreateSasLocator(this.Asset, accessPolicy); cancellationToken.ThrowIfCancellationRequested(() => this.Cleanup(null, null, locator, accessPolicy)); return locator; }, cancellationToken). ContinueWith( t => { locator = t.Result; t.ThrowIfFaulted(() => this.Cleanup(null, null, locator, accessPolicy)); cancellationToken.ThrowIfCancellationRequested(() => this.Cleanup(null, null, locator, accessPolicy)); var blobTransfer = GetMediaContext().MediaServicesClassFactory.GetBlobTransferClient(); blobTransfer.NumberOfConcurrentTransfers = this.GetMediaContext().NumberOfConcurrentTransfers; blobTransfer.ParallelTransferThreadCount = this.GetMediaContext().ParallelTransferThreadCount; UploadAsync(path, blobTransfer, locator, cancellationToken).Wait(); locator.Delete(); cancellationToken.ThrowIfCancellationRequested(() => this.Cleanup(null, null, null, accessPolicy)); accessPolicy.Delete(); }, cancellationToken)); }
//TODO: move to workerEncoder private IAsset CreateAssetFromBlob(CloudBlobContainer externalMediaBlobContainer, string ExternalBlobName, CloudBlobClient assetBlobClient, string MediaServicesBlobName, string myProcessId) { // Create a new asset. //myProcessId = Guid.NewGuid().ToString(); CloudMediaContext MediaContext = ObtainContext(_accountMediaName, _accountMediaKey); string assetName = string.Format("{0}_{1}_Butler_{2}", externalMediaBlobContainer.Name, ExternalBlobName, myProcessId); IAsset asset = MediaContext.Assets.Create(assetName, AssetCreationOptions.None); IAccessPolicy writePolicy = MediaContext.AccessPolicies.Create("writePolicy_" + assetName, TimeSpan.FromMinutes(120), AccessPermissions.Write); ILocator destinationLocator = MediaContext.Locators.CreateLocator(LocatorType.Sas, asset, writePolicy); string assetContainerName = (new Uri(destinationLocator.Path)).Segments[1]; CloudBlobContainer assetContainer = assetBlobClient.GetContainerReference(assetContainerName); CloudBlockBlob ExternalBlob = externalMediaBlobContainer.GetBlockBlobReference(ExternalBlobName); CloudBlockBlob assetBlob = assetContainer.GetBlockBlobReference(MediaServicesBlobName); var sas = externalMediaBlobContainer.GetSharedAccessSignature(new SharedAccessBlobPolicy() { SharedAccessStartTime = DateTime.UtcNow.AddMinutes(-15), SharedAccessExpiryTime = DateTime.UtcNow.AddDays(7), Permissions = SharedAccessBlobPermissions.Read, }); var srcBlockBlobSasUri = string.Format("{0}{1}", ExternalBlob.Uri, sas); assetBlob.StartCopyFromBlob(new Uri(srcBlockBlobSasUri)); CloudBlockBlob blobStatusCheck; blobStatusCheck = (CloudBlockBlob)assetContainer.GetBlobReferenceFromServer(MediaServicesBlobName); while (blobStatusCheck.CopyState.Status == CopyStatus.Pending) { Task.Delay(TimeSpan.FromSeconds(10d)).Wait(); Trace.TraceInformation("Waiting copy of " + blobStatusCheck.Name); blobStatusCheck = (CloudBlockBlob)assetContainer.GetBlobReferenceFromServer(MediaServicesBlobName); } assetBlob.FetchAttributes(); var assetFile = asset.AssetFiles.Create(MediaServicesBlobName); destinationLocator.Delete(); writePolicy.Delete(); //// Refresh the asset. asset = MediaContext.Assets.Where(a => a.Id == asset.Id).FirstOrDefault(); return(asset); }
public bool GenerateTranscript(IAsset asset) { try { var outputFolder = this._amsConfigurations.FfmpegFramesOutputPath; IAsset outputAsset = asset; IAccessPolicy policy = null; ILocator locator = null; policy = _mediaContext.AccessPolicies.Create("My 30 days readonly policy", TimeSpan.FromDays(360), AccessPermissions.Read); locator = _mediaContext.Locators.CreateLocator(LocatorType.Sas, outputAsset, policy, DateTime.UtcNow.AddMinutes(-5)); DownloadAssetToLocal(outputAsset, outputFolder); locator.Delete(); return(true); } catch { //TODO: Logging Console.WriteLine("Exception occured while generating index for video."); throw; } }
private IAsset ProcessUploadFile(string SafeFileName, string FileName, string storageaccount = null) { if (storageaccount == null) { storageaccount = MyContext.DefaultStorageAccount.Name; // no storage account or null, then let's take the default one } IAsset asset = null; IAccessPolicy policy = null; ILocator locator = null; try { asset = MyContext.Assets.Create(SafeFileName as string, storageaccount, AssetCreationOptions.None); IAssetFile file = asset.AssetFiles.Create(SafeFileName); policy = MyContext.AccessPolicies.Create( SafeFileName, TimeSpan.FromDays(30), AccessPermissions.Write | AccessPermissions.List); locator = MyContext.Locators.CreateLocator(LocatorType.Sas, asset, policy); file.UploadProgressChanged += file_UploadProgressChanged; file.Upload(FileName); AssetInfo.SetFileAsPrimary(asset, SafeFileName); } catch { asset = null; } finally { if (locator != null) { locator.Delete(); } if (policy != null) { policy.Delete(); } } return(asset); }
public IAsset CreateAsset(string authToken, string assetName, string storageAccount, bool storageEncryption, string[] fileNames) { AssetCreationOptions assetEncryption = storageEncryption ? AssetCreationOptions.StorageEncrypted : AssetCreationOptions.None; IAsset asset = _media.Assets.Create(assetName, storageAccount, assetEncryption); BlobClient blobClient = new BlobClient(authToken, storageAccount); string sourceContainer = Constant.Storage.Blob.Container.FileUpload; if (fileNames.Length == 1) { string fileName = fileNames[0]; IAssetFile assetFile = asset.AssetFiles.Create(fileName); CloudBlockBlob sourceBlob = blobClient.GetBlob(sourceContainer, null, fileName, true); Stream sourceStream = sourceBlob.OpenRead(); assetFile.Upload(sourceStream); foreach (ILocator locator in asset.Locators) { locator.Delete(); } } else { BlobTransferClient transferClient = new BlobTransferClient(); ILocator sasLocator = CreateLocator(LocatorType.Sas, asset, true); List <Task> uploadTasks = new List <Task>(); foreach (string fileName in fileNames) { IAssetFile assetFile = asset.AssetFiles.Create(fileName); CloudBlockBlob sourceBlob = blobClient.GetBlob(sourceContainer, null, fileName, true); Stream sourceStream = sourceBlob.OpenRead(); Task uploadTask = assetFile.UploadAsync(sourceStream, transferClient, sasLocator, CancellationToken.None); uploadTasks.Add(uploadTask); } Task.WaitAll(uploadTasks.ToArray()); sasLocator.Delete(); } SetPrimaryFile(asset); return(asset); }
public void ShouldCreateEncryptedInitilizedAsset() { IAsset asset = _dataContext.Assets.Create("Test", AssetCreationOptions.StorageEncrypted); Assert.IsNotNull(asset, "Asset should be non null"); Assert.AreNotEqual(Guid.Empty, asset.Id, "Asset ID shuold not be null"); Assert.AreEqual(0, asset.AssetFiles.Count(), "Asset has files"); Assert.AreEqual(AssetState.Initialized, asset.State, "Expecting initilized state"); IAccessPolicy policy = _dataContext.AccessPolicies.Create("temp", TimeSpan.FromMinutes(10), AccessPermissions.Write); ILocator locator = _dataContext.Locators.CreateSasLocator(asset, policy); IAssetFile file = asset.AssetFiles.Create(Path.GetFileName(_smallWmv)); Task task = file.UploadAsync(_smallWmv, new BlobTransferClient { NumberOfConcurrentTransfers = 10, ParallelTransferThreadCount = 10 }, locator, CancellationToken.None); task.Wait(); Assert.IsTrue(task.IsCompleted); Assert.IsTrue(!task.IsFaulted); locator.Delete(); policy.Delete(); IAsset refreshedAsset = _dataContext.Assets.Where(c => c.Id == asset.Id).FirstOrDefault(); Assert.AreEqual(asset.Name, refreshedAsset.Name); Assert.AreEqual(AssetState.Initialized, refreshedAsset.State); Assert.AreEqual(1, refreshedAsset.AssetFiles.Count(), "file count wrong"); // TODO: Task 27827: Design and implement a KeyOracle Role to hold the Private key or keys used to protect content keys //VerifyAndDownloadAsset(refreshedAsset); ContentKeyTests.VerifyFileAndContentKeyMetadataForStorageEncryption(refreshedAsset, _dataContext); }
public static async Task <IAsset> UploadVideo(CloudMediaContext context, string name, string address) { IAsset uploadAsset = await context.Assets.CreateAsync(Path.GetFileNameWithoutExtension(name), AssetCreationOptions.None, CancellationToken.None); IAssetFile assetFile = await uploadAsset.AssetFiles.CreateAsync(name, CancellationToken.None); IAccessPolicy accessPolicy = context.AccessPolicies.Create(uploadAsset.Name, TimeSpan.FromMinutes(5), AccessPermissions.Write); ILocator locator = context.Locators.CreateSasLocator(uploadAsset, accessPolicy); BlobTransferClient client = new BlobTransferClient() { NumberOfConcurrentTransfers = 5, ParallelTransferThreadCount = 5 }; await assetFile.UploadAsync(address, client, locator, CancellationToken.None); locator.Delete(); accessPolicy.Delete(); return(uploadAsset); }
private static ILocator CreateLocator(IAsset asset) { ILocator streamingLocator = _mediaContext .Locators .Where(l => l.AssetId == asset.Id) .AsEnumerable() .FirstOrDefault(l => l.Type == LocatorType.OnDemandOrigin); if (streamingLocator != null && streamingLocator.ExpirationDateTime <= DateTime.UtcNow + TimeSpan.FromHours(1)) { streamingLocator.Delete(); streamingLocator = null; } if (streamingLocator == null) { IAccessPolicy accessPolicy = _mediaContext.AccessPolicies.Create("readPolicy", TimeSpan.FromDays(7), AccessPermissions.Read); streamingLocator = _mediaContext.Locators.CreateLocator(LocatorType.OnDemandOrigin, asset, accessPolicy); } streamingLocator.Update(DateTime.Now + TimeSpan.FromDays(7)); return(streamingLocator); }
/// <summary> /// Create Asset and Add blob /// </summary> /// <param name="AssetName"></param> /// <param name="blobUrl"></param> /// <returns></returns> public IAsset CreateAsset(string AssetName, string blobUrl, string MediaStorageConn, string StorageConnectionString, string WorkflowName) { //Create Empty Asset Uri MezzamineFileUri = new Uri(blobUrl); int segmentscount = MezzamineFileUri.Segments.Count() - 1; IAsset currentAsset = _MediaServicesContext.Assets.Create(AssetName, AssetCreationOptions.None); //Add the File IAccessPolicy writePolicy = _MediaServicesContext.AccessPolicies.Create("writePolicy_" + currentAsset.Name, TimeSpan.FromMinutes(120), AccessPermissions.Write); ILocator destinationLocator = _MediaServicesContext.Locators.CreateLocator(LocatorType.Sas, currentAsset, writePolicy); //Asset Storage CloudStorageAccount assetStorageCount = CloudStorageAccount.Parse(MediaStorageConn); CloudBlobClient assetClient = assetStorageCount.CreateCloudBlobClient(); CloudBlobContainer assetContainer = assetClient.GetContainerReference(currentAsset.Uri.Segments[1]); //Mezzamine Storage CloudStorageAccount MezzamineStorageCount = CloudStorageAccount.Parse(StorageConnectionString); CloudBlobClient MezzamineClient = MezzamineStorageCount.CreateCloudBlobClient(); CloudBlobContainer MezzamineContainer = MezzamineClient.GetContainerReference(WorkflowName); Uri xFile = new Uri(blobUrl); int segmentIndex = xFile.Segments.Count() - 1; //Asset BLOB Xfile string AssetBlobName = Uri.UnescapeDataString(xFile.Segments[segmentIndex]); CloudBlockBlob assetBlob = assetContainer.GetBlockBlobReference(AssetBlobName); assetContainer.SetPermissions(new BlobContainerPermissions { PublicAccess = BlobContainerPublicAccessType.Blob } ); //Mezzamine BLOB Xfile string MezzamineBlobName = ""; for (int i = 2; i <= segmentIndex; i++) { MezzamineBlobName += xFile.Segments[i]; } MezzamineBlobName = Uri.UnescapeDataString(MezzamineBlobName); CloudBlockBlob MezzamineBlob = MezzamineContainer.GetBlockBlobReference(MezzamineBlobName); var sas = MezzamineContainer.GetSharedAccessSignature(new SharedAccessBlobPolicy() { SharedAccessStartTime = DateTime.UtcNow.AddMinutes(-15), SharedAccessExpiryTime = DateTime.UtcNow.AddDays(7), Permissions = SharedAccessBlobPermissions.Read, }); //USE decode URL for spetial characters var srcBlockBlobSasUri = string.Format("{0}{1}", Uri.UnescapeDataString(MezzamineBlob.Uri.AbsoluteUri), sas); assetBlob.StartCopyFromBlob(new Uri(srcBlockBlobSasUri)); CloudBlockBlob blobStatusCheck; blobStatusCheck = (CloudBlockBlob)assetContainer.GetBlobReferenceFromServer(AssetBlobName); while (blobStatusCheck.CopyState.Status == CopyStatus.Pending) { Task.Delay(TimeSpan.FromSeconds(10d)).Wait(); blobStatusCheck = (CloudBlockBlob)assetContainer.GetBlobReferenceFromServer(AssetBlobName); } assetBlob.FetchAttributes(); //Add the xFile to Asset var assetFile = currentAsset.AssetFiles.Create(assetBlob.Name); MezzamineBlob.FetchAttributes(); assetFile.ContentFileSize = MezzamineBlob.Properties.Length; assetFile.Update(); destinationLocator.Delete(); writePolicy.Delete(); currentAsset.AssetFiles.FirstOrDefault().IsPrimary = true; currentAsset.Update(); return(currentAsset); }
/// <summary> /// Ingest all Mezzamine files to Asset /// </summary> private void IngestAssets() { IAccessPolicy writePolicy = MediaContext.AccessPolicies.Create("writePolicy_" + currentAsset.Name, TimeSpan.FromMinutes(120), AccessPermissions.Write); ILocator destinationLocator = MediaContext.Locators.CreateLocator(LocatorType.Sas, currentAsset, writePolicy); //Asset Storage CloudStorageAccount assetStorageCount = CloudStorageAccount.Parse(myRequest.MediaStorageConn); CloudBlobClient assetClient = assetStorageCount.CreateCloudBlobClient(); CloudBlobContainer assetContainer = assetClient.GetContainerReference(currentAsset.Uri.Segments[1]); //Mezzamine Storage CloudStorageAccount MezzamineStorageCount = CloudStorageAccount.Parse(myRequest.ButlerRequest.StorageConnectionString); CloudBlobClient MezzamineClient = MezzamineStorageCount.CreateCloudBlobClient(); CloudBlobContainer MezzamineContainer = MezzamineClient.GetContainerReference(myRequest.ButlerRequest.WorkflowName); //Filter Ingest only MP4 foreach (string urlMezzamineFile in myRequest.ButlerRequest.MezzanineFiles.Where(mf => mf.ToLower().EndsWith(".mp4"))) { Uri xFile = new Uri(urlMezzamineFile); int segmentIndex = xFile.Segments.Count() - 1; //Asset BLOB Xfile string AssetBlobName = Uri.UnescapeDataString(xFile.Segments[segmentIndex]); CloudBlockBlob assetBlob = assetContainer.GetBlockBlobReference(AssetBlobName); assetContainer.SetPermissions(new BlobContainerPermissions { PublicAccess = BlobContainerPublicAccessType.Blob } ); //Mezzamine BLOB Xfile string MezzamineBlobName = ""; for (int i = 2; i <= segmentIndex; i++) { MezzamineBlobName += xFile.Segments[i]; } MezzamineBlobName = Uri.UnescapeDataString(MezzamineBlobName); CloudBlockBlob MezzamineBlob = MezzamineContainer.GetBlockBlobReference(MezzamineBlobName); var sas = MezzamineContainer.GetSharedAccessSignature(new SharedAccessBlobPolicy() { SharedAccessStartTime = DateTime.UtcNow.AddMinutes(-15), SharedAccessExpiryTime = DateTime.UtcNow.AddDays(7), Permissions = SharedAccessBlobPermissions.Read, }); //USE decode URL for spetial characters var srcBlockBlobSasUri = string.Format("{0}{1}", Uri.UnescapeDataString(MezzamineBlob.Uri.AbsoluteUri), sas); assetBlob.StartCopy(new Uri(srcBlockBlobSasUri)); Trace.TraceInformation("{0} in process {1} processId {2} Start copy MezzamineFile {3}", this.GetType().FullName, myRequest.ProcessTypeId, myRequest.ProcessInstanceId, MezzamineBlobName); CloudBlockBlob blobStatusCheck; blobStatusCheck = (CloudBlockBlob)assetContainer.GetBlobReferenceFromServer(AssetBlobName); while (blobStatusCheck.CopyState.Status == CopyStatus.Pending) { Task.Delay(TimeSpan.FromSeconds(10d)).Wait(); Trace.TraceInformation("{0} in process {1} processId {2} copying MezzamineFile {3} status {4}", this.GetType().FullName, myRequest.ProcessTypeId, myRequest.ProcessInstanceId, MezzamineBlobName, blobStatusCheck.CopyState.Status); blobStatusCheck = (CloudBlockBlob)assetContainer.GetBlobReferenceFromServer(AssetBlobName); } assetBlob.FetchAttributes(); //Add the xFile to Asset var assetFile = currentAsset.AssetFiles.Create(AssetBlobName); MezzamineBlob.FetchAttributes(); assetFile.ContentFileSize = MezzamineBlob.Properties.Length; assetFile.Update(); Trace.TraceInformation("{0} in process {1} processId {2} finish MezzamineFile {3}", this.GetType().FullName, myRequest.ProcessTypeId, myRequest.ProcessInstanceId, MezzamineBlobName); } destinationLocator.Delete(); writePolicy.Delete(); currentAsset.Update(); }
private void Cleanup(BlobTransferClient blobTransfer, FileEncryption fileEncryption, ILocator locator, IAccessPolicy accessPolicy) { lock (this._lock) { if (blobTransfer != null) { try { blobTransfer.TransferProgressChanged -= this.OnDownloadBlobTransferProgressChanged; } catch { } finally { blobTransfer = null; } } if (fileEncryption != null) { try { fileEncryption.Dispose(); } catch { } finally { fileEncryption = null; } } if (locator != null) { try { locator.Delete(); } catch { } finally { locator = null; } } if (accessPolicy != null) { try { accessPolicy.Delete(); } catch { } finally { accessPolicy = null; } } } }