Example #1
0
    // Download the output asset of the specified job to a local folder.
    static IAsset DownloadAssetToLocal(IAsset outputAsset, string outputFolder)
    {
        // Create a SAS locator to download the asset
        IAccessPolicy accessPolicy = _context.AccessPolicies.Create("File Download Policy", TimeSpan.FromDays(30), AccessPermissions.Read);
        ILocator      locator      = _context.Locators.CreateLocator(LocatorType.Sas, outputAsset, accessPolicy);

        BlobTransferClient blobTransfer = new BlobTransferClient
        {
            NumberOfConcurrentTransfers = 20,
            ParallelTransferThreadCount = 20
        };

        var downloadTasks = new List <Task>();

        foreach (IAssetFile outputFile in outputAsset.AssetFiles)
        {
            // Use the following event handler to check download progress.
            outputFile.DownloadProgressChanged += DownloadProgress;

            string localDownloadPath = Path.Combine(outputFolder, outputFile.Name);

            Console.WriteLine("File download path:  " + localDownloadPath);

            downloadTasks.Add(outputFile.DownloadAsync(Path.GetFullPath(localDownloadPath), blobTransfer, locator, CancellationToken.None));

            outputFile.DownloadProgressChanged -= DownloadProgress;
        }

        Task.WaitAll(downloadTasks.ToArray());

        return(outputAsset);
    }
Example #2
0
        public void ShouldReportProgressForFile()
        {
            string        fileName         = _smallWmv;
            bool          reportedProgress = false;
            IAsset        asset            = _dataContext.Assets.Create(Guid.NewGuid().ToString(), AssetCreationOptions.StorageEncrypted);
            IAccessPolicy policy           = _dataContext.AccessPolicies.Create("Write", TimeSpan.FromMinutes(5), AccessPermissions.Write);
            ILocator      locator          = _dataContext.Locators.CreateSasLocator(asset, policy);
            var           info             = new FileInfo(fileName);
            IAssetFile    file             = asset.AssetFiles.Create(info.Name);
            var           blobclient       = new BlobTransferClient
            {
                NumberOfConcurrentTransfers = 5,
                ParallelTransferThreadCount = 5
            };

            blobclient.TransferProgressChanged += (s, e) =>
            {
                Assert.AreEqual(fileName, e.LocalFile);
                Assert.IsTrue(e.BytesTransferred <= e.TotalBytesToTransfer);
                reportedProgress = true;
            };

            file.UploadAsync(fileName, blobclient, locator, CancellationToken.None).Wait();
            Assert.IsTrue(reportedProgress);
        }
Example #3
0
        public void ShouldCreateAssetAsyncWithMultipleFiles()
        {
            string[] files = Directory.GetFiles("TestFiles");

            IAsset        asset      = _dataContext.Assets.Create(Guid.NewGuid().ToString(), AssetCreationOptions.None);
            IAccessPolicy policy     = _dataContext.AccessPolicies.Create("Write", TimeSpan.FromMinutes(5), AccessPermissions.Write);
            ILocator      locator    = _dataContext.Locators.CreateSasLocator(asset, policy);
            var           blobclient = new BlobTransferClient
            {
                NumberOfConcurrentTransfers = 5,
                ParallelTransferThreadCount = 5
            };

            var tasks = new List <Task>();

            foreach (string filePath in files)
            {
                var        info = new FileInfo(filePath);
                IAssetFile file = asset.AssetFiles.Create(info.Name);
                tasks.Add(file.UploadAsync(filePath, blobclient, locator, CancellationToken.None));
            }

            Task.WaitAll(tasks.ToArray());
            Assert.AreEqual(AssetState.Initialized, asset.State);
            Assert.AreEqual(files.Length, asset.AssetFiles.Count());
            Assert.AreEqual(1, asset.Locators.Count);


            foreach (IAssetFile assetFile in asset.AssetFiles)
            {
                Assert.IsTrue(files.Any(f => Path.GetFileName(f).Equals(assetFile.Name, StringComparison.OrdinalIgnoreCase)));
            }
        }
        public void ShouldCreateAssetFileArrayWithEncryption()
        {
            var filePaths = new[] {WindowsAzureMediaServicesTestConfiguration.SmallWmv, WindowsAzureMediaServicesTestConfiguration.SmallWmv2};
            IAsset asset = _dataContext.Assets.Create(Guid.NewGuid().ToString(), AssetCreationOptions.StorageEncrypted);
            IAccessPolicy policy = _dataContext.AccessPolicies.Create("Write", TimeSpan.FromMinutes(5), AccessPermissions.Write);
            ILocator locator = _dataContext.Locators.CreateSasLocator(asset, policy);
            var blobclient = new BlobTransferClient
                {
                    NumberOfConcurrentTransfers = 5,
                    ParallelTransferThreadCount = 5
                };


            foreach (string filePath in filePaths)
            {
                var info = new FileInfo(filePath);
                IAssetFile file = asset.AssetFiles.Create(info.Name);
                file.UploadAsync(filePath, blobclient, locator, CancellationToken.None).Wait();
            }

            // Associate an access policy with the asset so we can download the files associated with it
            policy = _dataContext.AccessPolicies.Create("Test", TimeSpan.FromMinutes(10), AccessPermissions.Read);
            _dataContext.Locators.CreateSasLocator(asset, policy);

            Assert.IsNotNull(asset, "Asset should be non null");
            Assert.AreNotEqual(Guid.Empty, asset.Id, "Asset ID shuold not be null");
            Assert.IsTrue(asset.Options == AssetCreationOptions.StorageEncrypted, "AssetCreationOptions did not have the expected value");

            VerifyFileAndContentKeyMetadataForStorageEncryption(asset, _dataContext);
            VerifyStorageEncryptionOnFiles(asset, filePaths);
        }
Example #5
0
        private IAsset CreateAssetAndUploadNFilesUsingAsyncCall(int expected)
        {
            IAsset asset = _dataContext.Assets.Create("TestWithMultipleFiles", AssetCreationOptions.None);

            VerifyAsset(asset);
            DirectoryInfo info = Directory.CreateDirectory(Guid.NewGuid().ToString());

            var           files   = new List <Task>();
            var           client  = new BlobTransferClient();
            IAccessPolicy policy  = _dataContext.AccessPolicies.Create("Write", TimeSpan.FromMinutes(20), AccessPermissions.Write);
            ILocator      locator = _dataContext.Locators.CreateSasLocator(asset, policy);


            for (int i = 0; i < expected; i++)
            {
                string     fileName;
                string     fullFilePath = CreateNewFileFromOriginal(info, out fileName);
                IAssetFile file         = asset.AssetFiles.Create(fileName);
                files.Add(file.UploadAsync(fullFilePath, client, locator, CancellationToken.None));
            }
            Task.WaitAll(files.ToArray());
            foreach (Task task in files)
            {
                Assert.IsTrue(task.IsCompleted);
                Assert.IsFalse(task.IsFaulted);
                Assert.IsNull(task.Exception);
            }
            return(asset);
        }
        public void ShouldCreateAssetFileArrayWithEncryption()
        {
            var           filePaths  = new[] { WindowsAzureMediaServicesTestConfiguration.SmallWmv, WindowsAzureMediaServicesTestConfiguration.SmallWmv2 };
            IAsset        asset      = _mediaContext.Assets.Create(Guid.NewGuid().ToString(), AssetCreationOptions.StorageEncrypted);
            IAccessPolicy policy     = _mediaContext.AccessPolicies.Create("Write", TimeSpan.FromMinutes(5), AccessPermissions.Write);
            ILocator      locator    = _mediaContext.Locators.CreateSasLocator(asset, policy);
            var           blobclient = new BlobTransferClient
            {
                NumberOfConcurrentTransfers = 5,
                ParallelTransferThreadCount = 5
            };


            foreach (string filePath in filePaths)
            {
                var        info = new FileInfo(filePath);
                IAssetFile file = asset.AssetFiles.Create(info.Name);
                file.UploadAsync(filePath, blobclient, locator, CancellationToken.None).Wait();
            }

            // Associate an access policy with the asset so we can download the files associated with it
            policy = _mediaContext.AccessPolicies.Create("Test", TimeSpan.FromMinutes(10), AccessPermissions.Read);
            _mediaContext.Locators.CreateSasLocator(asset, policy);

            Assert.IsNotNull(asset, "Asset should be non null");
            Assert.AreNotEqual(Guid.Empty, asset.Id, "Asset ID should not be null");
            Assert.IsTrue(asset.Options == AssetCreationOptions.StorageEncrypted, "AssetCreationOptions did not have the expected value");

            VerifyFileAndContentKeyMetadataForStorageEncryption(asset, _mediaContext);
            VerifyStorageEncryptionOnFiles(asset, filePaths);
        }
        public void ShouldCreateAssetFileArrayWithPlayReadyEncryption()
        {
            // Note that these files are not really PlayReady encrypted.  For the purposes of this test that is okay.
            IAsset        asset      = _mediaContext.Assets.Create(Guid.NewGuid().ToString(), AssetCreationOptions.CommonEncryptionProtected);
            IAccessPolicy policy     = _mediaContext.AccessPolicies.Create("Write", TimeSpan.FromMinutes(5), AccessPermissions.Write);
            ILocator      locator    = _mediaContext.Locators.CreateSasLocator(asset, policy);
            var           blobclient = new BlobTransferClient
            {
                NumberOfConcurrentTransfers = 5,
                ParallelTransferThreadCount = 5
            };


            foreach (string filePath in new[] { WindowsAzureMediaServicesTestConfiguration.SmallWmv, WindowsAzureMediaServicesTestConfiguration.SmallWmv2 })
            {
                var        info = new FileInfo(filePath);
                IAssetFile file = asset.AssetFiles.Create(info.Name);
                file.UploadAsync(filePath, blobclient, locator, CancellationToken.None).Wait();
            }

            Guid keyId = Guid.NewGuid();

            byte[] contentKey = GetRandomBuffer(16);

            IContentKey key = _mediaContext.ContentKeys.Create(keyId, contentKey);

            asset.ContentKeys.Add(key);

            Assert.IsNotNull(asset, "Asset should be non null");
            Assert.AreNotEqual(Guid.Empty, asset.Id, "Asset ID should not be null");
            Assert.IsTrue(asset.Options == AssetCreationOptions.CommonEncryptionProtected, "AssetCreationOptions did not have the expected value");

            VerifyFileAndContentKeyMetadataForCommonEncryption(asset);
            VerifyContentKeyVersusExpectedValue2(asset, contentKey, keyId);
        }
Example #8
0
        /// <summary>
        /// Too complicated. The other method is pretty much simple.
        /// </summary>
        /// <param name="jobID"></param>
        /// <param name="outputFolder"></param>
        /// <returns></returns>
        static IAsset DownloadAsset(string jobID, string outputFolder)
        {
            IJob          job          = GetJob(jobID);
            IAsset        outputAsset  = job.OutputMediaAssets[0];
            IAccessPolicy accessPolicy = context.AccessPolicies.Create("File Download Policy", TimeSpan.FromDays(30), AccessPermissions.Read);
            ILocator      locator      = context.Locators.CreateLocator(LocatorType.Sas, outputAsset, accessPolicy);

            BlobTransferClient client = new BlobTransferClient()
            {
                NumberOfConcurrentTransfers = 10,
                ParallelTransferThreadCount = 10
            };

            var downloadTasks = new List <Task>();

            foreach (IAssetFile outputFile in outputAsset.AssetFiles)
            {
                string localDownloadPath = Path.Combine(outputFolder, outputFile.Name);
                Console.Write("File download path: " + localDownloadPath);
                downloadTasks.Add(
                    outputFile.DownloadAsync(
                        Path.GetFullPath(localDownloadPath),
                        client,
                        locator,
                        CancellationToken.None)
                    );
            }
            Task.WaitAll(downloadTasks.ToArray());
            return(outputAsset);
        }
        public void ShouldCreateAssetFile()
        {
            IAsset             asset              = _mediaContext.Assets.Create("Empty", AssetCreationOptions.StorageEncrypted);
            IAssetFile         file               = asset.AssetFiles.CreateAsync(Path.GetFileName(_smallWmv), CancellationToken.None).Result;
            IAccessPolicy      policy             = _mediaContext.AccessPolicies.Create("temp", TimeSpan.FromMinutes(10), AccessPermissions.Write);
            ILocator           locator            = _mediaContext.Locators.CreateSasLocator(asset, policy);
            BlobTransferClient blobTransferClient = _mediaContext.MediaServicesClassFactory.GetBlobTransferClient();
            bool transferCompletedFired           = false;

            blobTransferClient.TransferCompleted += (sender, args) =>
            {
                transferCompletedFired = true;
                Assert.AreEqual(BlobTransferType.Upload, args.TransferType, "file.UploadAsync Transfer completed expected BlobTransferType is Upload");
            };
            file.UploadAsync(_smallWmv, blobTransferClient, locator, CancellationToken.None).Wait();
            Assert.IsNotNull(asset, "Asset should be non null");
            Assert.IsTrue(transferCompletedFired, "TransferCompleted event has not been fired");
            Assert.AreNotEqual(Guid.Empty, asset.Id, "Asset ID shuold not be null");
            Assert.AreEqual(AssetState.Initialized, asset.State, "Asset state wrong");

            foreach (IAssetFile ifile in asset.AssetFiles)
            {
                if (ifile.IsPrimary)
                {
                    Assert.IsTrue(string.Compare(Path.GetFileName(_smallWmv), ifile.Name, StringComparison.InvariantCultureIgnoreCase) == 0, "Main file is wrong");
                    break;
                }
            }
        }
        private IAsset CreateSmoothAsset(string[] filePaths)
        {
            IAsset        asset      = _mediaContext.Assets.Create(Guid.NewGuid().ToString(), AssetCreationOptions.StorageEncrypted);
            IAccessPolicy policy     = _mediaContext.AccessPolicies.Create("Write", TimeSpan.FromMinutes(5), AccessPermissions.Write);
            ILocator      locator    = _mediaContext.Locators.CreateSasLocator(asset, policy);
            var           blobclient = new BlobTransferClient
            {
                NumberOfConcurrentTransfers = 5,
                ParallelTransferThreadCount = 5
            };


            foreach (string filePath in filePaths)
            {
                var        info = new FileInfo(filePath);
                IAssetFile file = asset.AssetFiles.Create(info.Name);
                file.UploadAsync(filePath, blobclient, locator, CancellationToken.None).Wait();
                if (WindowsAzureMediaServicesTestConfiguration.SmallIsm == filePath)
                {
                    file.IsPrimary = true;
                    file.Update();
                }
            }
            return(asset);
        }
        public void ShouldCancelDownloadToFileAsyncTaskAfter50Milliseconds()
        {
            string fileUploaded    = _smallWmv;
            string outputDirectory = "Download" + Guid.NewGuid();
            string fileDownloaded  = Path.Combine(outputDirectory, Path.GetFileName(fileUploaded));

            IAsset     asset     = AssetTests.CreateAsset(_mediaContext, Path.GetFullPath(fileUploaded), AssetCreationOptions.StorageEncrypted);
            IAssetFile assetFile = asset.AssetFiles.First();

            Assert.AreEqual(assetFile.Asset.Id, asset.Id);
            Assert.AreEqual(1, asset.Locators.Count);

            CleanDirectory(outputDirectory);

            var                source             = new CancellationTokenSource();
            IAccessPolicy      accessPolicy       = _mediaContext.AccessPolicies.Create("SdkDownload", TimeSpan.FromHours(12), AccessPermissions.Read);
            ILocator           locator            = _mediaContext.Locators.CreateSasLocator(asset, accessPolicy);
            BlobTransferClient blobTransferClient = _mediaContext.MediaServicesClassFactory.GetBlobTransferClient();


            Exception canceledException  = null;
            Task      downloadToFileTask = null;

            try
            {
                downloadToFileTask = assetFile.DownloadAsync(fileDownloaded, blobTransferClient, locator, source.Token);

                // Send a cancellation signal after 2 seconds.
                Thread.Sleep(50);
                source.Cancel();

                // Block the thread waiting for the job to finish.
                downloadToFileTask.Wait();
            }
            catch (AggregateException exception)
            {
                Assert.AreEqual(1, exception.InnerExceptions.Count);
                canceledException = exception.InnerException;
            }
            finally
            {
                if (File.Exists(fileDownloaded))
                {
                    File.Delete(fileDownloaded);
                }
            }

            Assert.IsNotNull(canceledException);
            Assert.IsInstanceOfType(canceledException, typeof(OperationCanceledException));

            // The async task ends in a Canceled state.
            Assert.AreEqual(TaskStatus.Canceled, downloadToFileTask.Status);

            CloudMediaContext newContext = WindowsAzureMediaServicesTestConfiguration.CreateCloudMediaContext();

            IAsset retreivedAsset = newContext.Assets.Where(a => a.Id == asset.Id).Single();

            Assert.AreEqual(2, retreivedAsset.Locators.Count);
        }
        public async Task <IAsset> UploadAssetAsync(string localFilePath, UpdateProgressAction updateProgress, Guid assetProgressMoniker, CancellationToken cancellationToken)
        {
            Logger.Debug("UploadAssetAsync() invoked with localFilePath value '{0}'.", localFilePath);

            IAsset        asset = null;
            IAccessPolicy uploadAccessPolicy = null;

            try {
                var assetName = Guid.NewGuid().ToString();
                asset = await Context.Assets.CreateAsync(assetName, AssetCreationOptions.None, cancellationToken).ConfigureAwait(continueOnCapturedContext: false);

                var assetFile = await asset.AssetFiles.CreateAsync(Path.GetFileName(localFilePath), cancellationToken).ConfigureAwait(continueOnCapturedContext: false);

                assetFile.IsPrimary = true;

                uploadAccessPolicy = await Context.AccessPolicies.CreateAsync("Upload Policy", TimeSpan.FromDays(1), AccessPermissions.Write | AccessPermissions.List).ConfigureAwait(continueOnCapturedContext: false);

                var uploadLocator = await Context.Locators.CreateLocatorAsync(LocatorType.Sas, asset, uploadAccessPolicy).ConfigureAwait(continueOnCapturedContext: false);

                var uploadClient = new BlobTransferClient();

                uploadClient.TransferProgressChanged += (sender, e) => updateProgress(new WamsUploadProgressInfo(assetProgressMoniker, e));
                await assetFile.UploadAsync(localFilePath, uploadClient, uploadLocator, cancellationToken).ConfigureAwait(continueOnCapturedContext: false);

                await uploadLocator.DeleteAsync().ConfigureAwait(continueOnCapturedContext: false);

                await uploadAccessPolicy.DeleteAsync().ConfigureAwait(continueOnCapturedContext: false);

                Logger.Information("New asset with ID '{0}' was uploaded from temp file with name '{1}'.", asset.Id, localFilePath);

                return(asset);
            }
            catch (Exception ex) {
                if (ex is OperationCanceledException)
                {
                    Logger.Information("Upload of asset with ID '{0}' from temp file with name '{1}' was canceled.", asset.Id, localFilePath);
                }
                else
                {
                    Logger.Error(ex, "Error while uploading asset from temp file with name '{0}'. Cleaning up asset and any locators and access policy created for upload.", localFilePath);
                }

                try {
                    if (asset != null)
                    {
                        asset.Delete(); // Deletes any locators also.
                    }
                    if (uploadAccessPolicy != null)
                    {
                        uploadAccessPolicy.Delete();
                    }
                }
                catch (Exception iex) {
                    Logger.Warning(iex, "Error while cleaning up asset and any locators and access policy created for upload.");
                }

                throw;
            }
        }
Example #13
0
        static public IAsset CreateAssetAndUpload(string inputfile)
        {
            #region
            // List MediaProcessors

            /*
             * var processor = mediaContext.MediaProcessors.ToList();
             * Console.WriteLine("-------------------------------------------");
             * Console.WriteLine("Available Media Processors:\n");
             * foreach (var item in processor)
             * {
             *  Console.WriteLine($"{item.Description}, Version {item.Version}");
             * }
             * Console.WriteLine("-------------------------------------------");
             */
            #endregion

            // Create asset
            Console.WriteLine("Creating asset...");
            var assetName = "Bitten by the Frost";
            var asset     = mediaContext.Assets.Create(assetName, AssetCreationOptions.None); // Set encryption at rest here

            // Create file object associated with asset
            var fileName  = Path.GetFileName(inputfile);
            var assetFile = asset.AssetFiles.Create(fileName);
            Console.WriteLine($"Created assetFile {assetFile.Name}");

            // Create access policy and SAS locator (needed for upload)
            var policy  = mediaContext.AccessPolicies.Create("Write", TimeSpan.FromHours(1), AccessPermissions.Write);
            var locator = mediaContext.Locators.CreateSasLocator(asset, policy);

            // Upload to asset file object (you upload to assetFile not to asset)
            Console.WriteLine($"Uploading {assetFile.Name} [{assetFile.MimeType}]");

            // Use the UploadAsync method to ensure that the calls are not blocking
            // and the files are uploaded in parallel.
            // https://azure.microsoft.com/en-gb/documentation/articles/media-services-dotnet-upload-files/
            var blobTransferClient = new BlobTransferClient();
            blobTransferClient.NumberOfConcurrentTransfers = 5;
            blobTransferClient.ParallelTransferThreadCount = 5;
            blobTransferClient.TransferProgressChanged    += UploadProgress;
            assetFile.UploadAsync(inputfile, blobTransferClient, locator, new CancellationToken()).Wait();
            Console.WriteLine($"\nUpload finished: {assetFile.Name}");

            // Select default (primary) file within asset
            assetFile.IsPrimary = true;

            // Cleanup
            locator.Delete();

            // Kick off encoding job
            Console.WriteLine("Adding encoding job..");
            IJob job = CreateEncodingJob(asset);

            // Show task (encode) progress
            GetTaskProgress(job).Wait();

            return(asset);
        }
Example #14
0
        /// <summary>
        /// Verifies the and download asset.
        /// </summary>
        /// <param name="asset">The asset.</param>
        /// <param name="expectedFileCount">The expected file count.</param>
        private void VerifyAndDownloadAssetFileNTimes(IAssetFile assetFile, IAsset asset, int count, int expirationTime, bool cleanOutputDirectory, bool expectFailure = false)
        {
            var source = new CancellationTokenSource();

            if (expirationTime == 0)
            {
                expirationTime = 1000;
            }
            IAccessPolicy accessPolicy = _mediaContext.AccessPolicies.Create("SdkDownload", TimeSpan.FromSeconds(expirationTime),
                                                                             AccessPermissions.Read);
            ILocator locator      = _mediaContext.Locators.CreateSasLocator(asset, accessPolicy);
            var      blobTransfer = new BlobTransferClient
            {
                NumberOfConcurrentTransfers = _mediaContext.NumberOfConcurrentTransfers,
                ParallelTransferThreadCount = _mediaContext.ParallelTransferThreadCount
            };

            var downloads = new List <Task>();
            var paths     = new List <string>();

            if (cleanOutputDirectory)
            {
                CleanDirectory(_outputDirectory);
            }
            try
            {
                for (int i = 0; i < count; i++)
                {
                    foreach (IAssetFile file in _mediaContext.Files.Where(c => c.ParentAssetId == asset.Id))
                    {
                        string path = Path.Combine(_outputDirectory, Guid.NewGuid().ToString());
                        paths.Add(path);
                        downloads.Add(assetFile.DownloadAsync(path, blobTransfer, locator, source.Token));
                    }
                }
                Task.WaitAll(downloads.ToArray());
            }
            finally
            {
                foreach (var fileDownloaded in paths)
                {
                    if (File.Exists(fileDownloaded))
                    {
                        File.Delete(fileDownloaded);
                    }
                    else
                    {
                        if (!expectFailure)
                        {
                            Assert.Fail();
                        }
                    }
                }
            }
        }
        private static void UploadFile(ILocator locator, IAsset asset, string filePath, CloudMediaContext mediaContext)
        {
            var                info = new FileInfo(filePath);
            IAssetFile         file = asset.AssetFiles.Create(info.Name);
            BlobTransferClient blobTransferClient = mediaContext.MediaServicesClassFactory.GetBlobTransferClient();
            Task               task = file.UploadAsync(filePath, blobTransferClient, locator, CancellationToken.None);

            task.Wait();
            Assert.IsTrue(task.IsCompleted);
            Assert.IsTrue(!task.IsFaulted);
        }
        static public IAsset CreateAssetAndUploadMultipleFiles(AssetCreationOptions assetCreationOptions, string folderPath)
        {
            CloudMediaContext _context = CloudContextHelper.GetContext();

            var assetName = "UploadMultipleFiles_" + DateTime.UtcNow.ToString();

            IAsset asset = _context.Assets.Create(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("Created assetFile {0}", assetFile.Name);

                // It is recommended to validate AccestFiles before upload.
                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);
        }
    /// <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());
    }
Example #18
0
        public void When_Uploading_Multiple_Files_The_Progress_Event_Should_Only_Be_For_The_Bound_AssetFile()
        {
            IAsset        asset        = _mediaContext.Assets.Create("test", AssetCreationOptions.None);
            string        fileUploaded = _smallWmv;
            var           file         = new FileInfo(fileUploaded);
            IAssetFile    fileInfo     = asset.AssetFiles.Create(Path.GetFileName(_smallWmv));
            IAccessPolicy policy       = _mediaContext.AccessPolicies.Create("Write", TimeSpan.FromMinutes(10),
                                                                             AccessPermissions.Write);
            ILocator locator = _mediaContext.Locators.CreateLocator(LocatorType.Sas, asset, policy);
            var      btc     = new BlobTransferClient
            {
                NumberOfConcurrentTransfers = 5,
                ParallelTransferThreadCount = 5
            };

            int allProgressEventsFiredCount = 0;

            btc.TransferProgressChanged += (sender, args) => { allProgressEventsFiredCount++; };

            bool progressFired           = false;
            bool wrongFileSize           = true;
            int  fileProgressEventsCount = 0;

            fileInfo.UploadProgressChanged += (s, e) =>
            {
                progressFired = true;
                wrongFileSize = e.TotalBytes != file.Length;
                fileProgressEventsCount++;
            };

            Task uploadTask = fileInfo.UploadAsync(fileUploaded, btc, locator, CancellationToken.None);

            string competingFile = WindowsAzureMediaServicesTestConfiguration.GetVideoSampleFilePath(TestContext,
                                                                                                     WindowsAzureMediaServicesTestConfiguration.SmallMp41);

            var retryPolicy =
                _mediaContext.MediaServicesClassFactory.GetBlobStorageClientRetryPolicy()
                .AsAzureStorageClientRetryPolicy();

            btc.UploadBlob(CreateUrl(locator, Path.GetFileName(competingFile)), competingFile, null, null,
                           CancellationToken.None, retryPolicy).Wait();

            uploadTask.Wait();

            Assert.IsTrue(progressFired, "No upload progress event fired");
            Assert.IsFalse(wrongFileSize, "Received the wrong file size from the upload progress event");
            Assert.IsTrue(condition: fileProgressEventsCount < allProgressEventsFiredCount,
                          message:
                          "Unexpected number of fired events, it should be more than the events fired for the uploaded file.");
        }
Example #19
0
        public async Task Start()
        {
            var context = this.createContext.Invoke();

            var blobTransferClient = new BlobTransferClient();

            blobTransferClient.TransferProgressChanged += this.OnBlobTransferProgressChanged;
            blobTransferClient.TransferCompleted       += this.OnBlobTransferClientOnTransferCompleted;

            this.asset = await CreateEmptyAsset(context, this.AssetName, AssetCreationOptions.None);

            this.locator = await CreateSasLocatorAsync(context, this.asset);

            var fileName = Path.GetFileName(this.FilePath);

            var assetFile = await this.asset.AssetFiles.CreateAsync(fileName, CancellationToken.None);

            await assetFile.UploadAsync(this.FilePath, blobTransferClient, this.locator, CancellationToken.None);
        }
        public static IAsset CreateAsset(CloudMediaContext datacontext, string filePath, AssetCreationOptions options)
        {
            IAsset             asset              = datacontext.Assets.Create(Guid.NewGuid().ToString(), options);
            IAccessPolicy      policy             = datacontext.AccessPolicies.Create("Write", TimeSpan.FromMinutes(5), AccessPermissions.Write);
            ILocator           locator            = datacontext.Locators.CreateSasLocator(asset, policy);
            var                info               = new FileInfo(filePath);
            IAssetFile         file               = asset.AssetFiles.Create(info.Name);
            BlobTransferClient blobTransferClient = datacontext.MediaServicesClassFactory.GetBlobTransferClient();

            blobTransferClient.NumberOfConcurrentTransfers = 5;
            blobTransferClient.ParallelTransferThreadCount = 5;
            file.UploadAsync(filePath,
                             blobTransferClient,
                             locator,
                             CancellationToken.None).Wait();
            file.IsPrimary = true;
            file.Update();

            return(asset);
        }
Example #21
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="outputAsset"></param>
        /// <param name="outputFolder"></param>
        private void DownloadAssetToLocal(IAsset outputAsset, string outputFolder)
        {
            IAccessPolicy      accessPolicy = _mediaContext.AccessPolicies.Create("File Download Policy", TimeSpan.FromDays(30), AccessPermissions.Read);
            ILocator           locator      = _mediaContext.Locators.CreateSasLocator(outputAsset, accessPolicy);
            BlobTransferClient blobTransfer = new BlobTransferClient
            {
                NumberOfConcurrentTransfers = 10,
                ParallelTransferThreadCount = 10
            };
            var downloadTasks = new List <Task>();

            foreach (IAssetFile outputFile in outputAsset.AssetFiles)
            {
                if (outputFile.MimeType == "text/vtt")
                {
                    string localDownloadPath = Path.Combine(outputFolder, outputFile.Name);
                    downloadTasks.Add(outputFile.DownloadAsync(Path.GetFullPath(localDownloadPath), blobTransfer, locator, CancellationToken.None));
                }
            }
            Task.WaitAll(downloadTasks.ToArray());
        }
Example #22
0
        static IAsset DownloadAssetToLocal(string jobId, string outputFolder)
        {
            // This method illustrates how to download a single asset.
            // However, you can iterate through the OutputAssets
            // collection, and download all assets if there are many.

            // Get a reference to the job.
            IJob job = GetJob(jobId);
            // Get a reference to the first output asset. If there were multiple
            // output media assets you could iterate and handle each one.
            IAsset outputAsset = job.OutputMediaAssets[0];

            IAccessPolicy      accessPolicy = _context.AccessPolicies.Create("File Download Policy", TimeSpan.FromDays(30), AccessPermissions.Read);
            ILocator           locator      = _context.Locators.CreateSasLocator(outputAsset, accessPolicy);
            BlobTransferClient blobTransfer = new BlobTransferClient
            {
                NumberOfConcurrentTransfers = 10,
                ParallelTransferThreadCount = 10
            };

            var downloadTasks = new List <Task>();

            foreach (IAssetFile outputFile in outputAsset.AssetFiles)
            {
                // Use the following event handler to check download progress.
                outputFile.DownloadProgressChanged += DownloadProgress;

                string localDownloadPath = Path.Combine(outputFolder, outputFile.Name);

                Console.WriteLine("File download path:  " + localDownloadPath);

                downloadTasks.Add(outputFile.DownloadAsync(Path.GetFullPath(localDownloadPath), blobTransfer, locator, CancellationToken.None));

                outputFile.DownloadProgressChanged -= DownloadProgress;
            }

            Task.WaitAll(downloadTasks.ToArray());

            return(outputAsset);
        }
Example #23
0
        public static IAsset CreateAssetAndUploadNFilesUsingAsyncCall(int expected, CloudMediaContext mediaContext, string sourceFileName)
        {
            IAsset asset = mediaContext.Assets.Create("TestWithMultipleFiles", AssetCreationOptions.None);

            VerifyAsset(asset);
            DirectoryInfo info = null;

            try
            {
                info = Directory.CreateDirectory(Guid.NewGuid().ToString());

                var files = new List <Task>();
                BlobTransferClient blobTransferClient = mediaContext.MediaServicesClassFactory.GetBlobTransferClient();
                IAccessPolicy      policy             = mediaContext.AccessPolicies.Create("Write", TimeSpan.FromMinutes(20), AccessPermissions.Write);
                ILocator           locator            = mediaContext.Locators.CreateSasLocator(asset, policy);

                for (int i = 0; i < expected; i++)
                {
                    string     fileName;
                    string     fullFilePath = CreateNewFileFromOriginal(info, sourceFileName, out fileName);
                    IAssetFile file         = asset.AssetFiles.Create(fileName);
                    files.Add(file.UploadAsync(fullFilePath, blobTransferClient, locator, CancellationToken.None));
                }
                Task.WaitAll(files.ToArray());
                foreach (Task task in files)
                {
                    Assert.IsTrue(task.IsCompleted);
                    Assert.IsFalse(task.IsFaulted);
                    Assert.IsNull(task.Exception);
                }
            }
            finally
            {
                if (info != null)
                {
                    info.Delete(recursive: true);
                }
            }
            return(asset);
        }
Example #24
0
        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 ShouldThrowArgumentExceptionWhenUploadAsyncFileNameNotEqualToAssetFileName()
        {
            IAsset        asset        = _mediaContext.Assets.Create("test", AssetCreationOptions.None);
            string        fileUploaded = _smallWmv;
            IAssetFile    fileInfo     = asset.AssetFiles.Create("test.txt");
            IAccessPolicy policy       = _mediaContext.AccessPolicies.Create("Write", TimeSpan.FromMinutes(1), AccessPermissions.Write);
            ILocator      locator      = _mediaContext.Locators.CreateLocator(LocatorType.Sas, asset, policy);

            try
            {
                BlobTransferClient blobTransferClient = _mediaContext.MediaServicesClassFactory.GetBlobTransferClient();

                fileInfo.UploadAsync(fileUploaded,
                                     blobTransferClient,
                                     locator,
                                     CancellationToken.None);
            }
            catch (ArgumentException ex)
            {
                AssertFileMismatchException(fileUploaded, ex);
                throw;
            }
        }
    /// <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);
    }
Example #27
0
        public void ShouldCreateAssetAndUploadAndDownload10FilesUsingAsyncCall()
        {
            const int expected = 10;
            IAsset    asset    = CreateAssetAndUploadNFilesUsingAsyncCall(expected);

            Assert.AreEqual(expected, _dataContext.Files.Where(c => c.ParentAssetId == asset.Id).Count());
            IAccessPolicy accessPolicy = _dataContext.AccessPolicies.Create("SdkDownload", TimeSpan.FromHours(12), AccessPermissions.Read);
            ILocator      locator      = _dataContext.Locators.CreateSasLocator(asset, accessPolicy);
            var           blobTransfer = new BlobTransferClient
            {
                NumberOfConcurrentTransfers = _dataContext.NumberOfConcurrentTransfers,
                ParallelTransferThreadCount = _dataContext.ParallelTransferThreadCount
            };

            var downloads = new List <Task>();
            var paths     = new List <string>();

            foreach (IAssetFile file in _dataContext.Files.Where(c => c.ParentAssetId == asset.Id))
            {
                string path = Path.GetTempFileName();
                paths.Add(path);
                downloads.Add(file.DownloadAsync(path, blobTransfer, locator, CancellationToken.None));
            }
            Task.WaitAll(downloads.ToArray());

            int i = 0;

            foreach (Task download in downloads)
            {
                Assert.IsTrue(download.IsCompleted);
                Assert.IsFalse(download.IsCanceled);
                Assert.IsNull(download.Exception);
                Assert.IsTrue(File.Exists(paths[i]));
                i++;
            }
        }
Example #28
0
        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);
        }
        public void ShouldCreateAssetAsyncWithMultipleFiles()
        {
            string[] files = Directory.GetFiles("TestFiles");

            IAsset asset = _dataContext.Assets.Create(Guid.NewGuid().ToString(), AssetCreationOptions.None);
            IAccessPolicy policy = _dataContext.AccessPolicies.Create("Write", TimeSpan.FromMinutes(5), AccessPermissions.Write);
            ILocator locator = _dataContext.Locators.CreateSasLocator(asset, policy);
            var blobclient = new BlobTransferClient
                {
                    NumberOfConcurrentTransfers = 5,
                    ParallelTransferThreadCount = 5
                };

            var tasks = new List<Task>();
            foreach (string filePath in files)
            {
                var info = new FileInfo(filePath);
                IAssetFile file = asset.AssetFiles.Create(info.Name);
                tasks.Add(file.UploadAsync(filePath, blobclient, locator, CancellationToken.None));
            }

            Task.WaitAll(tasks.ToArray());
            Assert.AreEqual(AssetState.Initialized, asset.State);
            Assert.AreEqual(files.Length, asset.AssetFiles.Count());
            Assert.AreEqual(1, asset.Locators.Count);


            foreach (IAssetFile assetFile in asset.AssetFiles)
            {
                Assert.IsTrue(files.Any(f => Path.GetFileName(f).Equals(assetFile.Name, StringComparison.OrdinalIgnoreCase)));
            }
        }
        private IAsset CreateSmoothAsset(string[] filePaths)
        {
            IAsset asset = _mediaContext.Assets.Create(Guid.NewGuid().ToString(), AssetCreationOptions.StorageEncrypted);
            IAccessPolicy policy = _mediaContext.AccessPolicies.Create("Write", TimeSpan.FromMinutes(5), AccessPermissions.Write);
            ILocator locator = _mediaContext.Locators.CreateSasLocator(asset, policy);
            var blobclient = new BlobTransferClient
            {
                NumberOfConcurrentTransfers = 5,
                ParallelTransferThreadCount = 5
            };

            foreach (string filePath in filePaths)
            {
                var info = new FileInfo(filePath);
                IAssetFile file = asset.AssetFiles.Create(info.Name);
                file.UploadAsync(filePath, blobclient, locator, CancellationToken.None).Wait();
                if (WindowsAzureMediaServicesTestConfiguration.SmallIsm == filePath)
                {
                    file.IsPrimary = true;
                    file.Update();
                }
            }
            return asset;
        }
        public void ShouldCancelDownloadToFileAsyncTaskAfter50Milliseconds()
        {
            string fileUploaded = _smallWmv;
            string outputDirectory = "Download" + Guid.NewGuid();
            string fileDownloaded = Path.Combine(outputDirectory, Path.GetFileName(fileUploaded));

            IAsset asset = AssetTests.CreateAsset(_mediaContext, Path.GetFullPath(fileUploaded), AssetCreationOptions.StorageEncrypted);
            IAssetFile assetFile = asset.AssetFiles.First();

            Assert.AreEqual(assetFile.Asset.Id, asset.Id);
            Assert.AreEqual(1, asset.Locators.Count);

            CleanDirectory(outputDirectory);

            var source = new CancellationTokenSource();
            IAccessPolicy accessPolicy = _mediaContext.AccessPolicies.Create("SdkDownload", TimeSpan.FromHours(12), AccessPermissions.Read);
            ILocator locator = _mediaContext.Locators.CreateSasLocator(asset, accessPolicy);
            var blobTransfer = new BlobTransferClient
                {
                    NumberOfConcurrentTransfers = _mediaContext.NumberOfConcurrentTransfers,
                    ParallelTransferThreadCount = _mediaContext.ParallelTransferThreadCount
                };

            Exception canceledException = null;
            Task downloadToFileTask = null;
            try
            {
                downloadToFileTask = assetFile.DownloadAsync(fileDownloaded, blobTransfer, locator, source.Token);

                // Send a cancellation signal after 2 seconds.
                Thread.Sleep(50);
                source.Cancel();

                // Block the thread waiting for the job to finish.
                downloadToFileTask.Wait();
            }
            catch (AggregateException exception)
            {
                Assert.AreEqual(1, exception.InnerExceptions.Count);
                canceledException = exception.InnerException;
            }
            finally
            {
                if (File.Exists(fileDownloaded))
                {
                    File.Delete(fileDownloaded);
                }
            }

            Assert.IsNotNull(canceledException);
            Assert.IsInstanceOfType(canceledException, typeof (OperationCanceledException));

            // The async task ends in a Canceled state.
            Assert.AreEqual(TaskStatus.Canceled, downloadToFileTask.Status);

            CloudMediaContext newContext = WindowsAzureMediaServicesTestConfiguration.CreateCloudMediaContext();

            IAsset retreivedAsset = newContext.Assets.Where(a => a.Id == asset.Id).Single();

            Assert.AreEqual(2, retreivedAsset.Locators.Count);
        }
        public void When_Uploading_Multiple_Files_The_Progress_Event_Should_Only_Be_For_The_Bound_AssetFile()
        {
            IAsset asset = _mediaContext.Assets.Create("test", AssetCreationOptions.None);
            string fileUploaded = _smallWmv;
            var file = new FileInfo(fileUploaded);
            IAssetFile fileInfo = asset.AssetFiles.Create(Path.GetFileName(_smallWmv));
            IAccessPolicy policy = _mediaContext.AccessPolicies.Create("Write", TimeSpan.FromMinutes(1), AccessPermissions.Write);
            ILocator locator = _mediaContext.Locators.CreateLocator(LocatorType.Sas, asset, policy);
            var btc = new BlobTransferClient
                {
                    NumberOfConcurrentTransfers = 5,
                    ParallelTransferThreadCount = 5
                };

            int allProgressEventsFiredCount = 0;

            btc.TransferProgressChanged += (sender, args) => { allProgressEventsFiredCount++; };

            bool progressFired = false;
            bool wrongFileSize = true;
            int fileProgressEventsCount = 0;

            fileInfo.UploadProgressChanged += (s, e) =>
                {
                    progressFired = true;
                    wrongFileSize = e.TotalBytes != file.Length;
                    fileProgressEventsCount++;
                };

            Task uploadTask = fileInfo.UploadAsync(fileUploaded, btc, locator, CancellationToken.None);

            string competingFile = WindowsAzureMediaServicesTestConfiguration.GetVideoSampleFilePath(TestContext, WindowsAzureMediaServicesTestConfiguration.SmallMp41);

            var retryPolicy = _mediaContext.MediaServicesClassFactory.GetBlobStorageClientRetryPolicy().AsAzureStorageClientRetryPolicy();

            btc.UploadBlob(CreateUrl(locator, Path.GetFileName(competingFile)), competingFile, null, null, CancellationToken.None, retryPolicy).Wait();

            uploadTask.Wait();

            Assert.IsTrue(progressFired, "No upload progress event fired");
            Assert.IsFalse(wrongFileSize, "Received the wrong file size from the upload progress event");
            Assert.IsTrue(condition: fileProgressEventsCount < allProgressEventsFiredCount, message: "Unexpected number of fired events, it should be more than the events fired for the uploaded file.");
        }
    /// <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();
    }
        public void ShouldCreateAssetFileArrayWithPlayReadyEncryption()
        {
            // Note that these files are not really PlayReady encrypted.  For the purposes of this test that is okay.
            IAsset asset = _mediaContext.Assets.Create(Guid.NewGuid().ToString(), AssetCreationOptions.CommonEncryptionProtected);
            IAccessPolicy policy = _mediaContext.AccessPolicies.Create("Write", TimeSpan.FromMinutes(5), AccessPermissions.Write);
            ILocator locator = _mediaContext.Locators.CreateSasLocator(asset, policy);
            var blobclient = new BlobTransferClient
                {
                    NumberOfConcurrentTransfers = 5,
                    ParallelTransferThreadCount = 5
                };

            foreach (string filePath in new[] { WindowsAzureMediaServicesTestConfiguration.SmallWmv, WindowsAzureMediaServicesTestConfiguration.SmallWmv2 })
            {
                var info = new FileInfo(filePath);
                IAssetFile file = asset.AssetFiles.Create(info.Name);
                file.UploadAsync(filePath, blobclient, locator, CancellationToken.None).Wait();
            }

            Guid keyId = Guid.NewGuid();
            byte[] contentKey = GetRandomBuffer(16);

            IContentKey key = _mediaContext.ContentKeys.Create(keyId, contentKey);
            asset.ContentKeys.Add(key);

            Assert.IsNotNull(asset, "Asset should be non null");
            Assert.AreNotEqual(Guid.Empty, asset.Id, "Asset ID should not be null");
            Assert.IsTrue(asset.Options == AssetCreationOptions.CommonEncryptionProtected, "AssetCreationOptions did not have the expected value");

            VerifyFileAndContentKeyMetadataForCommonEncryption(asset);
            VerifyContentKeyVersusExpectedValue2(asset, contentKey, keyId);
        }
Example #35
0
        static async Task MainAsync(string[] args)
        {
            var ingestDirectory = new DirectoryInfo(ConfigurationManager.AppSettings["watchFolder"]);
            var context         = new CloudMediaContext(new MediaServicesCredentials(ConfigurationManager.AppSettings["accountName"], ConfigurationManager.AppSettings["accountKey"]));
            var commonId        = Guid.NewGuid().ToString();

            // Ask whether to do direct upload or external (bulk) upload
            Console.WriteLine("Watching directory " + ConfigurationManager.AppSettings["watchFolder"]);
            Console.WriteLine("Enter D for direct upload or E for external upload:");
            var mode = Console.ReadLine().ToUpper();

            // 1. Create Asset
            #region 1. Create Asset
            var asset = await context.Assets.CreateAsync(ingestDirectory.Name, AssetCreationOptions.None, CancellationToken.None); // Create asset

            asset.AlternateId = $"custom-id-{commonId}";                                                                           // Set the AlternateId to something custom
            asset.Name        = $"custom-name-{commonId}";                                                                         // Give the Asset a name
            asset.Update();

            Console.WriteLine(string.Format("Asset id :{0}", asset.Id));
            Console.WriteLine(string.Format("Asset AlternateId :{0}", asset.AlternateId));
            #endregion

            if (mode == "D")
            {
                // 2. Upload
                #region 2. Upload
                var blobTransferClient = new BlobTransferClient();
                blobTransferClient.NumberOfConcurrentTransfers = 20;
                blobTransferClient.ParallelTransferThreadCount = 20;

                blobTransferClient.TransferProgressChanged += blobTransferClient_TransferProgressChanged;


                var accessPolicy = await context.AccessPolicies.CreateAsync($"{asset.Name} policy", TimeSpan.FromDays(30), AccessPermissions.Write | AccessPermissions.List);

                var locator = await context.Locators.CreateLocatorAsync(LocatorType.Sas, asset, accessPolicy);


                var filePaths   = Directory.EnumerateFiles(ConfigurationManager.AppSettings["watchFolder"]);
                var uploadTasks = new List <Task>();
                foreach (var filePath in filePaths)
                {
                    var assetFile = await asset.AssetFiles.CreateAsync(Path.GetFileName(filePath), CancellationToken.None);

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

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

                await Task.WhenAll(uploadTasks.ToArray());

                Console.WriteLine("Done uploading the files");

                blobTransferClient.TransferProgressChanged -= blobTransferClient_TransferProgressChanged;
                #endregion
            }
            else if (mode == "E")
            {
                // 2. Create IngestManifest
                #region 2. Create IngestManifest
                var manifest = await context.IngestManifests.CreateAsync($"ingest-{commonId}");

                Console.WriteLine(string.Format("manifest id : {0}", manifest.Id));
                Console.WriteLine(string.Format("manifest BlobStorageUriForUpload: {0}", manifest.BlobStorageUriForUpload));
                #endregion

                // 3. Add the file names to the IngestManifest as assets
                #region 3. Add the file names to the IngestManifest as assets
                // Retrieve files in the directory
                var fileList = new List <string>();
                foreach (var item in ingestDirectory.EnumerateFiles())
                {
                    fileList.Add(item.Name);
                    Console.WriteLine(string.Format("File : {0}", item.Name));
                }

                await manifest.IngestManifestAssets.CreateAsync(asset, fileList.ToArray <string>(), CancellationToken.None);

                #endregion

                // 4. Check external upload progress, this could be an external process as an Azure Function
                #region 4. Check external upload progress, this could be an external process as an Azure Function
                bool isFinished = false;
                while (!isFinished)
                {
                    manifest = context.IngestManifests.Where(m => m.Id == manifest.Id).FirstOrDefault();

                    Console.WriteLine("** Current time - {0}", DateTime.Now.ToLongTimeString());
                    Console.WriteLine("  PendingFilesCount  : {0}", manifest.Statistics.PendingFilesCount);
                    Console.WriteLine("  FinishedFilesCount : {0}", manifest.Statistics.FinishedFilesCount);
                    Console.WriteLine("  {0}% complete.\n", (float)manifest.Statistics.FinishedFilesCount / (float)(manifest.Statistics.FinishedFilesCount + manifest.Statistics.PendingFilesCount) * 100);

                    if (manifest.Statistics.PendingFilesCount == 0)
                    {
                        Console.WriteLine("External upload finished");
                        isFinished = true;
                        break;
                    }

                    if (manifest.Statistics.FinishedFilesCount < manifest.Statistics.PendingFilesCount)
                    {
                        Console.WriteLine("Waiting 5 seconds for external upload to finish..");
                    }

                    await Task.Delay(5000);
                }

                // Clean-up, delete manifest blob
                await manifest.DeleteAsync();

                #endregion
            }

            // 5. Create the manifest for the uploaded asset and upload it into the asset
            #region 5. Create the manifest for the uploaded asset and upload it into the asset
            Console.WriteLine("Generating .ism file for the asset");
            var smildata          = AMSHelper.LoadAndUpdateManifestTemplate(asset);
            var smilXMLDocument   = XDocument.Parse(smildata.Content);
            var smildataAssetFile = asset.AssetFiles.Create(smildata.FileName);

            Stream stream = new MemoryStream(); // Create a stream
            smilXMLDocument.Save(stream);       // Save XDocument into the stream
            stream.Position = 0;                // Rewind the stream ready to read from it elsewhere
            smildataAssetFile.Upload(stream);

            // Update the asset to set the primary file as the ism file
            AMSHelper.SetFileAsPrimary(asset, smildata.FileName);
            #endregion

            // 6. Create locators and publish the asset
            #region 6. Create locators and publish the asset
            // Create a 30-day readonly access policy if it doesn't exist
            var policy = context.AccessPolicies.ToList().FirstOrDefault(p => p.Name == "Streaming policy");
            if (policy == null)
            {
                policy = await context.AccessPolicies.CreateAsync("Streaming policy", TimeSpan.FromDays(30), AccessPermissions.Read);
            }

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

            // Create a full URL to the manifest file. Use this for playback
            // in streaming media clients.
            Console.WriteLine("URL to manifest for client streaming using Smooth Streaming protocol: ");
            Console.WriteLine(asset.GetSmoothStreamingUri());

            Console.WriteLine("URL to manifest for client streaming using HLS protocol: ");
            Console.WriteLine(asset.GetHlsUri());

            Console.WriteLine("URL to manifest for client streaming using MPEG DASH protocol: ");
            Console.WriteLine(asset.GetMpegDashUri());

            Console.WriteLine();
            #endregion

            Console.ReadLine();
        }
        private IAsset CreateAssetAndUploadNFilesUsingAsyncCall(int expected)
        {
            IAsset asset = _dataContext.Assets.Create("TestWithMultipleFiles", AssetCreationOptions.None);
            VerifyAsset(asset);
            DirectoryInfo info = Directory.CreateDirectory(Guid.NewGuid().ToString());

            var files = new List<Task>();
            var client = new BlobTransferClient();
            IAccessPolicy policy = _dataContext.AccessPolicies.Create("Write", TimeSpan.FromMinutes(20), AccessPermissions.Write);
            ILocator locator = _dataContext.Locators.CreateSasLocator(asset, policy);


            for (int i = 0; i < expected; i++)
            {
                string fileName;
                string fullFilePath = CreateNewFileFromOriginal(info, out fileName);
                IAssetFile file = asset.AssetFiles.Create(fileName);
                files.Add(file.UploadAsync(fullFilePath, client, locator, CancellationToken.None));
            }
            Task.WaitAll(files.ToArray());
            foreach (Task task in files)
            {
                Assert.IsTrue(task.IsCompleted);
                Assert.IsFalse(task.IsFaulted);
                Assert.IsNull(task.Exception);
            }
            return asset;
        }
        public void ShouldCreateAssetFile()
        {
            IAsset asset = _mediaContext.Assets.Create("Empty", AssetCreationOptions.StorageEncrypted);
            IAssetFile file = asset.AssetFiles.CreateAsync(Path.GetFileName(_smallWmv), CancellationToken.None).Result;
            IAccessPolicy policy = _mediaContext.AccessPolicies.Create("temp", TimeSpan.FromMinutes(10), AccessPermissions.Write);
            ILocator locator = _mediaContext.Locators.CreateSasLocator(asset, policy);
            BlobTransferClient blobTransferClient = new BlobTransferClient();
            bool transferCompletedFired = false;
            blobTransferClient.TransferCompleted += (sender, args) =>
            {
                transferCompletedFired = true;
                Assert.AreEqual(BlobTransferType.Upload, args.TransferType, "file.UploadAsync Transfer completed expected BlobTransferType is Upload");
            };
            file.UploadAsync(_smallWmv, blobTransferClient, locator, CancellationToken.None).Wait();
            Assert.IsNotNull(asset, "Asset should be non null");
            Assert.IsTrue(transferCompletedFired, "TransferCompleted event has not been fired");
            Assert.AreNotEqual(Guid.Empty, asset.Id, "Asset ID shuold not be null");
            Assert.AreEqual(AssetState.Initialized, asset.State, "Asset state wrong");

            foreach (IAssetFile ifile in asset.AssetFiles)
            {
                if (ifile.IsPrimary)
                {
                    Assert.IsTrue(string.Compare(Path.GetFileName(_smallWmv), ifile.Name, StringComparison.InvariantCultureIgnoreCase) == 0, "Main file is wrong");
                    break;
                }
            }
        }
        /// <summary>
        /// Verifies the and download asset.
        /// </summary>
        /// <param name="asset">The asset.</param>
        /// <param name="expectedFileCount">The expected file count.</param>
        private void VerifyAndDownloadAssetFileNTimes(IAssetFile assetFile, IAsset asset,int count,int expirationTime, bool cleanOutputDirectory,bool expectFailure = false)
        {
            var source = new CancellationTokenSource();
            if (expirationTime == 0)
            {
                expirationTime = 1000;
            }
            IAccessPolicy accessPolicy = _mediaContext.AccessPolicies.Create("SdkDownload", TimeSpan.FromSeconds(expirationTime),
                AccessPermissions.Read);
            ILocator locator = _mediaContext.Locators.CreateSasLocator(asset, accessPolicy);
            var blobTransfer = new BlobTransferClient
            {
                NumberOfConcurrentTransfers = _mediaContext.NumberOfConcurrentTransfers,
                ParallelTransferThreadCount = _mediaContext.ParallelTransferThreadCount
            };

            var downloads = new List<Task>();
            var paths = new List<string>();
            if (cleanOutputDirectory)
            {
                CleanDirectory(_outputDirectory);
            }
            try
            {
                for (int i = 0; i < count; i++)
                {
                    foreach (IAssetFile file in _mediaContext.Files.Where(c => c.ParentAssetId == asset.Id))
                    {
                        string path = Path.Combine(_outputDirectory, Guid.NewGuid().ToString());
                        paths.Add(path);
                        downloads.Add(assetFile.DownloadAsync(path, blobTransfer, locator, source.Token));
                    }
                }
                Task.WaitAll(downloads.ToArray());
            }
            finally
            {
                foreach (var fileDownloaded in paths)
                {
                    if (File.Exists(fileDownloaded))
                    {
                        File.Delete(fileDownloaded);
                    }
                    else
                    {
                        if (!expectFailure)
                        {
                            Assert.Fail();
                        }
                    }
                }
            }
        }
        public void ShouldCreateAssetAndUploadAndDownload10FilesUsingAsyncCall()
        {
            const int expected = 10;
            IAsset asset = CreateAssetAndUploadNFilesUsingAsyncCall(expected);
            Assert.AreEqual(expected, _dataContext.Files.Where(c => c.ParentAssetId == asset.Id).Count());
            IAccessPolicy accessPolicy = _dataContext.AccessPolicies.Create("SdkDownload", TimeSpan.FromHours(12), AccessPermissions.Read);
            ILocator locator = _dataContext.Locators.CreateSasLocator(asset, accessPolicy);
            var blobTransfer = new BlobTransferClient
                {
                    NumberOfConcurrentTransfers = _dataContext.NumberOfConcurrentTransfers,
                    ParallelTransferThreadCount = _dataContext.ParallelTransferThreadCount
                };

            var downloads = new List<Task>();
            var paths = new List<string>();
            foreach (IAssetFile file in _dataContext.Files.Where(c => c.ParentAssetId == asset.Id))
            {
                string path = Path.GetTempFileName();
                paths.Add(path);
                downloads.Add(file.DownloadAsync(path, blobTransfer, locator, CancellationToken.None));
            }
            Task.WaitAll(downloads.ToArray());

            int i = 0;
            foreach (Task download in downloads)
            {
                Assert.IsTrue(download.IsCompleted);
                Assert.IsFalse(download.IsCanceled);
                Assert.IsNull(download.Exception);
                Assert.IsTrue(File.Exists(paths[i]));
                i++;
            }
        }
        public void ShouldReportProgressForFile()
        {
            string fileName = _smallWmv;
            bool reportedProgress = false;
            IAsset asset = _dataContext.Assets.Create(Guid.NewGuid().ToString(), AssetCreationOptions.StorageEncrypted);
            IAccessPolicy policy = _dataContext.AccessPolicies.Create("Write", TimeSpan.FromMinutes(5), AccessPermissions.Write);
            ILocator locator = _dataContext.Locators.CreateSasLocator(asset, policy);
            var info = new FileInfo(fileName);
            IAssetFile file = asset.AssetFiles.Create(info.Name);
            var blobclient = new BlobTransferClient
                {
                    NumberOfConcurrentTransfers = 5,
                    ParallelTransferThreadCount = 5
                };
            blobclient.TransferProgressChanged += (s, e) =>
                {
                    Assert.AreEqual(fileName, e.LocalFile);
                    Assert.IsTrue(e.BytesTransferred <= e.TotalBytesToTransfer);
                    reportedProgress = true;
                };

            file.UploadAsync(fileName, blobclient, locator, CancellationToken.None).Wait();
            Assert.IsTrue(reportedProgress);
        }