Represents a client to operate on Windows Azure Blobs.
Esempio n. 1
0
        internal static async Task <IAssetFile> CreateAssetFileFromLocalFileAsync(this IAsset asset, string filePath, ILocator sasLocator, EventHandler <UploadProgressChangedEventArgs> uploadProgressChangedEventArgs, CancellationToken cancellationToken)
        {
            string     assetFileName = Path.GetFileName(filePath);
            IAssetFile assetFile     = await asset.AssetFiles.CreateAsync(assetFileName, cancellationToken).ConfigureAwait(false);

            MediaContextBase context = asset.GetMediaContext();

            assetFile.UploadProgressChanged += uploadProgressChangedEventArgs;

            BlobTransferClient blobTransferClient = new BlobTransferClient
            {
                NumberOfConcurrentTransfers = context.NumberOfConcurrentTransfers,
                ParallelTransferThreadCount = context.ParallelTransferThreadCount
            };

            await assetFile.UploadAsync(filePath, blobTransferClient, sasLocator, cancellationToken).ConfigureAwait(false);

            assetFile.UploadProgressChanged -= uploadProgressChangedEventArgs;

            if (assetFileName.EndsWith(ILocatorExtensions.ManifestFileExtension, StringComparison.OrdinalIgnoreCase))
            {
                assetFile.IsPrimary = true;
                await assetFile.UpdateAsync().ConfigureAwait(false);
            }

            return(assetFile);
        }
Esempio n. 2
0
        static void 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);
            var downloadTasks = new List<Task>();

            // 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];

            foreach (var outputAsset in job.OutputMediaAssets)
            {
                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
                };

                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);
                    downloadTasks.Add(outputFile.DownloadAsync(Path.GetFullPath(localDownloadPath), blobTransfer, locator, CancellationToken.None));
                    outputFile.DownloadProgressChanged -= DownloadProgress;
                }
            }
            Task.WaitAll(downloadTasks.ToArray());
        }
Esempio n. 3
0
        /// <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();
                }
            }
        }
Esempio n. 4
0
        /// <summary>
        /// Can be replaced by DownloadToFolder() in the medial extension lib!
        /// </summary>
        /// <param name="jobId"></param>
        /// <param name="outputFolder"></param>
        public static void DownloadAssetsToLocal(string jobId, string outputFolder, Action<Object, DownloadProgressChangedEventArgs> downloadProgressFunc = null)
        {
            // iterate through the OutputAssets collection, and download all assets if there are many.
            // Get a reference to the job.
            IJob job = GetJob(jobId);
            var downloadTasks = new List<Task>();

            // Get a reference to the first output asset.   // IAsset outputAsset = job.OutputMediaAssets[0];
            foreach (var outputAsset in job.OutputMediaAssets)
            {
                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
                };

                //downloadProgressFunc = downloadProgressFunc == null ? DownloadProgress : new EventHandler<Object,DownloadProgressChangedEventArgs>(new downloadProgressFunc; //figure out the right delegate
                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);
                    downloadTasks.Add(outputFile.DownloadAsync(Path.GetFullPath(localDownloadPath), blobTransfer, locator, CancellationToken.None));
                    outputFile.DownloadProgressChanged -= DownloadProgress;
                }
            }
            Task.WaitAll(downloadTasks.ToArray());
        }
Esempio n. 5
0
        /// <summary>
        /// Downloads the file asynchronously .
        /// </summary>
        /// <param name="destinationPath">The path to download the file to.</param>
        /// <param name="blobTransferClient">The <see cref="BlobTransferClient"/> which is used to download files.</param>
        /// <param name="locator">An asset <see cref="ILocator"/> which defines permissions associated with the Asset.</param>
        /// <param name="retryPolicy">The retry policy.</param>
        /// <param name="cancellationToken">The cancellation token.</param>
        /// <returns>A function delegate that returns the future result to be available through the Task.</returns>
        internal Task DownloadToFileAsync(string destinationPath, BlobTransferClient blobTransferClient, ILocator locator, IRetryPolicy retryPolicy, CancellationToken cancellationToken)
        {
            FileEncryption fileEncryption = this.GetFileEncryption();

            cancellationToken.Register(() => this.Cleanup(null, fileEncryption, null, null));
            return(Task.Factory.StartNew(() =>
            {
                cancellationToken.ThrowIfCancellationRequested(() => this.Cleanup(null, fileEncryption, null, null));
                ulong iv = Convert.ToUInt64(this.InitializationVector, CultureInfo.InvariantCulture);
                UriBuilder uriBuilder = new UriBuilder(locator.Path);
                uriBuilder.Path += String.Concat("/", Name);
                blobTransferClient.TransferProgressChanged += this.OnDownloadBlobTransferProgressChanged;
                blobTransferClient.DownloadBlob(uriBuilder.Uri, destinationPath, fileEncryption, iv, cancellationToken, retryPolicy).Wait(cancellationToken);
                cancellationToken.ThrowIfCancellationRequested(() => this.Cleanup(null, fileEncryption, null, null));
            },
                                         cancellationToken)
                   .ContinueWith(
                       t =>
            {
                t.ThrowIfFaulted(() => this.Cleanup(null, fileEncryption, null, null));
                cancellationToken.ThrowIfCancellationRequested(() => this.Cleanup(null, fileEncryption, null, null));
                this.Cleanup(null, fileEncryption, null, null);
            },
                       cancellationToken));
        }
Esempio n. 6
0
        /// <summary>
        /// Uploads the destinationPath with given path asynchronously
        /// </summary>
        /// <param name="path">The path of a destinationPath to upload</param>
        /// <param name="blobTransferClient">The <see cref="BlobTransferClient"/> which is used to upload files.</param>
        /// <param name="locator">An asset <see cref="ILocator"/> which defines permissions associated with the Asset.</param>
        /// <param name="token">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(string path, BlobTransferClient blobTransferClient, ILocator locator, CancellationToken token)
        {
            if (blobTransferClient == null)
            {
                throw new ArgumentNullException("blobTransferClient");
            }

            if (locator == null)
            {
                throw new ArgumentNullException("locator");
            }

            if (path == null)
            {
                throw new ArgumentNullException("path");
            }

            if (!File.Exists(path))
            {
                throw new FileNotFoundException(path);
            }

            ValidateFileName(path);

            IContentKey          contentKeyData       = null;
            FileEncryption       fileEncryption       = null;
            AssetCreationOptions assetCreationOptions = this.Asset.Options;

            if (assetCreationOptions.HasFlag(AssetCreationOptions.StorageEncrypted))
            {
                contentKeyData = this.Asset.ContentKeys.Where(c => c.ContentKeyType == ContentKeyType.StorageEncryption).FirstOrDefault();
                if (contentKeyData == null)
                {
                    throw new InvalidOperationException(String.Format(CultureInfo.InvariantCulture, StringTable.StorageEncryptionContentKeyIsMissing, this.Asset.Id));
                }

                fileEncryption = new FileEncryption(contentKeyData.GetClearKeyValue(), EncryptionUtils.GetKeyIdAsGuid(contentKeyData.Id));
            }

            EventHandler <BlobTransferProgressChangedEventArgs> handler = (s, e) => OnUploadProgressChanged(path, e);

            blobTransferClient.TransferProgressChanged += handler;

            MediaRetryPolicy retryPolicy = this.GetMediaContext().MediaServicesClassFactory.GetBlobStorageClientRetryPolicy();

            return(blobTransferClient.UploadBlob(
                       new Uri(locator.BaseUri),
                       path,
                       null,
                       fileEncryption,
                       token,
                       retryPolicy.AsAzureStorageClientRetryPolicy(),
                       () => locator.ContentAccessComponent)
                   .ContinueWith(
                       ts =>
            {
                blobTransferClient.TransferProgressChanged -= handler;
                this.PostUploadAction(ts, path, fileEncryption, assetCreationOptions, token);
            }));
        }
        /// <summary>
        /// Returns a <see cref="System.Threading.Tasks.Task"/> instance to download all the asset files in the <paramref name="asset"/> to the <paramref name="folderPath"/>.
        /// </summary>
        /// <param name="context">The <see cref="CloudMediaContext"/> instance.</param>
        /// <param name="asset">The <see cref="IAsset"/> instance where to download the asset files.</param>
        /// <param name="folderPath">The path to the folder where to download the asset files in the <paramref name="asset"/>.</param>
        /// <param name="downloadProgressChangedCallback">A callback to report download progress for each asset file in the <paramref name="asset"/>.</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 download all the asset files in the <paramref name="asset"/>.</returns>
        public static async Task DownloadAssetFilesToFolderAsync(this CloudMediaContext context, IAsset asset, string folderPath, Action <IAssetFile, DownloadProgressChangedEventArgs> downloadProgressChangedCallback, CancellationToken cancellationToken)
        {
            if (context == null)
            {
                throw new ArgumentNullException("context", "The context cannot be null.");
            }

            if (asset == null)
            {
                throw new ArgumentNullException("asset", "The asset cannot be null.");
            }

            if (!Directory.Exists(folderPath))
            {
                throw new ArgumentException(
                          string.Format(CultureInfo.InvariantCulture, "The folder '{0}' does not exist.", folderPath),
                          "folderPath");
            }

            ILocator sasLocator = await context.CreateLocatorAsync(asset, LocatorType.Sas, AccessPermissions.Read, DefaultAccessPolicyDuration);

            EventHandler <DownloadProgressChangedEventArgs> downloadProgressChangedHandler =
                (s, e) =>
            {
                IAssetFile assetFile = (IAssetFile)s;
                DownloadProgressChangedEventArgs eventArgs = e;

                if (downloadProgressChangedCallback != null)
                {
                    downloadProgressChangedCallback(assetFile, eventArgs);
                }
            };

            List <Task>       downloadTasks = new List <Task>();
            List <IAssetFile> assetFiles    = asset.AssetFiles.ToList();

            foreach (IAssetFile assetFile in assetFiles)
            {
                string             localDownloadPath  = Path.Combine(folderPath, assetFile.Name);
                BlobTransferClient blobTransferClient = new BlobTransferClient
                {
                    NumberOfConcurrentTransfers = context.NumberOfConcurrentTransfers,
                    ParallelTransferThreadCount = context.ParallelTransferThreadCount
                };

                assetFile.DownloadProgressChanged += downloadProgressChangedHandler;

                downloadTasks.Add(
                    assetFile.DownloadAsync(Path.GetFullPath(localDownloadPath), blobTransferClient, sasLocator, cancellationToken));
            }

            await Task.WhenAll(downloadTasks);

            await sasLocator.DeleteAsync();

            assetFiles.ForEach(af => af.DownloadProgressChanged -= downloadProgressChangedHandler);
        }
        /// <summary>
        /// Asynchronously downloads the represented file to the specified destination path.
        /// </summary>
        /// <param name="destinationPath">The path to download the file to.</param>
        /// <param name="blobTransferClient">The <see cref="BlobTransferClient"/> which is used to download files.</param>
        /// <param name="locator">An asset <see cref="ILocator"/> which defines permissions associated with the Asset.</param>
        /// <param name="cancellationToken">The cancellation token.</param>
        /// <returns>
        /// A function delegate that returns the future result to be available through the Task.
        /// </returns>
        public Task DownloadAsync(string destinationPath, BlobTransferClient blobTransferClient, ILocator locator, CancellationToken cancellationToken)
        {
            if (this.IsFragmented())
            {
                throw new NotSupportedException(StringTable.NotSupportedFragblobDownload);
            }

            MediaRetryPolicy retryPolicy = this.GetMediaContext().MediaServicesClassFactory.GetBlobStorageClientRetryPolicy();

            return(this.DownloadToFileAsync(destinationPath, blobTransferClient, locator, retryPolicy.AsAzureStorageClientRetryPolicy(), cancellationToken));
        }
        public static IAsset CreateAssetAndUploadMultipleFiles(this CloudMediaContext cloudMediaContext, AssetCreationOptions assetCreationOptions, string folderPath)
        {
            var assetName = "UploadMultipleFiles_" + DateTime.UtcNow.ToString();

            var asset = cloudMediaContext.CreateEmptyAsset(assetName, assetCreationOptions);

            var accessPolicy = cloudMediaContext.AccessPolicies.Create(assetName, TimeSpan.FromDays(30),
                                                                AccessPermissions.Write | AccessPermissions.List);
            var locator = cloudMediaContext.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 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(this._cloudMediaContext.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._cloudMediaContext.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 = new BlobTransferClient
                {
                    NumberOfConcurrentTransfers = this._cloudMediaContext.NumberOfConcurrentTransfers,
                    ParallelTransferThreadCount = this._cloudMediaContext.ParallelTransferThreadCount
                };
                UploadAsync(path, blobTransfer, locator, cancellationToken).Wait();
                locator.Delete();
                cancellationToken.ThrowIfCancellationRequested(() => this.Cleanup(null, null, null, accessPolicy));
                accessPolicy.Delete();
            },
                       cancellationToken));
        }
Esempio n. 11
0
        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;
            }
        }
        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);
        }
        /// <summary>
        /// Uploads the destinationPath with given path asynchronously
        /// </summary>
        /// <param name="path">The path of a destinationPath to upload</param>
        /// <param name="blobTransferClient">The <see cref="BlobTransferClient"/> which is used to upload files.</param>
        /// <param name="locator">An asset <see cref="ILocator"/> which defines permissions associated with the Asset.</param>
        /// <param name="token">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(string path, BlobTransferClient blobTransferClient, ILocator locator, CancellationToken token)
        {
            if (path == null)
            {
                throw new ArgumentNullException("path");
            }

            if (!File.Exists(path))
            {
                throw new FileNotFoundException(path);
            }

            if (this.IsFragmented())
            {
                throw new NotSupportedException(StringTable.NotSupportedFragblobUpload);
            }

            ValidateFileName(path);

            var fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);

            return(UploadAsync(fs, blobTransferClient, locator, token)
                   .ContinueWith(t => fs.Dispose())); // we can't put a using block as the file stream would get disposed before completing the UploadAsync task
        }
        internal static async Task<IAssetFile> CreateAssetFileFromLocalFileAsync(this IAsset asset, string filePath, ILocator sasLocator, EventHandler<UploadProgressChangedEventArgs> uploadProgressChangedEventArgs, CancellationToken cancellationToken)
        {
            string assetFileName = Path.GetFileName(filePath);
            IAssetFile assetFile = await asset.AssetFiles.CreateAsync(assetFileName, cancellationToken);
            MediaContextBase context = asset.GetMediaContext();

            assetFile.UploadProgressChanged += uploadProgressChangedEventArgs;

            BlobTransferClient blobTransferClient = new BlobTransferClient
            {
                NumberOfConcurrentTransfers = context.NumberOfConcurrentTransfers,
                ParallelTransferThreadCount = context.ParallelTransferThreadCount
            };

            await assetFile.UploadAsync(filePath, blobTransferClient, sasLocator, cancellationToken);

            assetFile.UploadProgressChanged -= uploadProgressChangedEventArgs;

            if (assetFileName.EndsWith(ILocatorExtensions.ManifestFileExtension, StringComparison.OrdinalIgnoreCase))
            {
                assetFile.IsPrimary = true;
                await assetFile.UpdateAsync();
            }

            return assetFile;
        }
 /// <summary>
 /// Asynchronously downloads the represented file to the specified destination path.
 /// </summary>
 /// <param name="destinationPath">The path to download the file to.</param>
 /// <param name="blobTransferClient">The <see cref="BlobTransferClient"/> which is used to download files.</param>
 /// <param name="locator">An asset <see cref="ILocator"/> which defines permissions associated with the Asset.</param>
 /// <param name="cancellationToken">The cancellation token.</param>
 /// <returns>
 /// A function delegate that returns the future result to be available through the Task.
 /// </returns>
 public Task DownloadAsync(string destinationPath, BlobTransferClient blobTransferClient, ILocator locator, CancellationToken cancellationToken)
 {
     return this.DownloadToFileAsync(destinationPath, blobTransferClient, locator, AzureStorageClientRetryPolicyFactory.DefaultPolicy.AsAzureStorageClientRetryPolicy(), cancellationToken);
 }
        /// <summary>
        /// Download the asset to a local folder.
        /// The download progress is displayed in the console window.
        /// </summary>
        /// <param name="asset"></param>
        /// <param name="outputFolder"></param>
        /// <returns></returns>
        public static IAsset DownloadAssetToLocal(IAsset asset, string outputFolder)
        {
            IAccessPolicy accessPolicy = _context.AccessPolicies.Create("File Download Policy", TimeSpan.FromDays(30),
                                                                        AccessPermissions.Read);
            ILocator locator = _context.Locators.CreateLocator(LocatorType.Sas, asset, accessPolicy);
            BlobTransferClient blobTransfer = new BlobTransferClient
                                                  {
                                                      NumberOfConcurrentTransfers = 10,
                                                      ParallelTransferThreadCount = 10
                                                  };

            Console.WriteLine("Files will be downloaded to:");
            Console.WriteLine("{0}", outputFolder);
            Console.WriteLine();

            var downloadTasks = new List<Task>();
            foreach (IAssetFile outputFile in asset.AssetFiles)
            {
                // Use the following event handler to check download progress.
                outputFile.DownloadProgressChanged += DownloadProgress;

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

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

                outputFile.DownloadProgressChanged -= DownloadProgress;
            }

            Task.WaitAll(downloadTasks.ToArray());

            return asset;
        }
Esempio n. 17
0
        /// <summary>
        /// Asynchronously downloads the represented file to the specified destination path.
        /// </summary>
        /// <param name="destinationPath">The path to download the file to.</param>
        /// <param name="blobTransferClient">The <see cref="BlobTransferClient"/> which is used to download files.</param>
        /// <param name="locator">An asset <see cref="ILocator"/> which defines permissions associated with the Asset.</param>
        /// <param name="cancellationToken">The cancellation token.</param>
        /// <returns>
        /// A function delegate that returns the future result to be available through the Task.
        /// </returns>
        public Task DownloadAsync(string destinationPath, BlobTransferClient blobTransferClient, ILocator locator, CancellationToken cancellationToken)
        {
            MediaRetryPolicy retryPolicy = this.GetMediaContext().MediaServicesClassFactory.GetBlobStorageClientRetryPolicy();

            return(this.DownloadToFileAsync(destinationPath, blobTransferClient, locator, retryPolicy.AsAzureStorageClientRetryPolicy(), cancellationToken));
        }
Esempio n. 18
0
        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;
                    }
                }
            }
        }
        /// <summary>
        /// Uploads the destinationPath with given path asynchronously
        /// </summary>
        /// <param name="path">The path of a destinationPath to upload</param>
        /// <param name="blobTransferClient">The <see cref="BlobTransferClient"/> which is used to upload files.</param>
        /// <param name="locator">An asset <see cref="ILocator"/> which defines permissions associated with the Asset.</param>
        /// <param name="token">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(string path, BlobTransferClient blobTransferClient, ILocator locator, CancellationToken token)
        {
            if (blobTransferClient == null)
            {
                throw new ArgumentNullException("blobTransferClient");
            }

            if (locator == null)
            {
                throw new ArgumentNullException("locator");
            }

            if (path == null)
            {
                throw new ArgumentNullException("path");
            }

            if (!File.Exists(path))
            {
                throw new FileNotFoundException(path);
            }

            ValidateFileName(path);

            IContentKey contentKeyData = null;
            FileEncryption fileEncryption = null;
            AssetCreationOptions assetCreationOptions = this.Asset.Options;
            if (assetCreationOptions.HasFlag(AssetCreationOptions.StorageEncrypted))
            {
                contentKeyData = this.Asset.ContentKeys.Where(c => c.ContentKeyType == ContentKeyType.StorageEncryption).FirstOrDefault();
                if (contentKeyData == null)
                {
                    throw new InvalidOperationException(String.Format(CultureInfo.InvariantCulture, StringTable.StorageEncryptionContentKeyIsMissing, this.Asset.Id));
                }

                fileEncryption = new FileEncryption(contentKeyData.GetClearKeyValue(), EncryptionUtils.GetKeyIdAsGuid(contentKeyData.Id));
            }

            EventHandler<BlobTransferProgressChangedEventArgs> handler = (s, e) => OnUploadProgressChanged(path, e);

            blobTransferClient.TransferProgressChanged += handler;

            return blobTransferClient.UploadBlob(
                new Uri(locator.Path),
                path,
                null,
                fileEncryption,
                token,
                AzureStorageClientRetryPolicyFactory.DefaultPolicy.AsAzureStorageClientRetryPolicy())
                .ContinueWith(
                ts=>
                {
                    blobTransferClient.TransferProgressChanged -= handler;
                    this.PostUploadAction(ts, path, fileEncryption, assetCreationOptions, token);
                });
        }
 /// <summary>
 /// Downloads the file asynchronously .
 /// </summary>
 /// <param name="destinationPath">The path to download the file to.</param>
 /// <param name="blobTransferClient">The <see cref="BlobTransferClient"/> which is used to download files.</param>
 /// <param name="locator">An asset <see cref="ILocator"/> which defines permissions associated with the Asset.</param>
 /// <param name="retryPolicy">The retry policy.</param>
 /// <param name="cancellationToken">The cancellation token.</param>
 /// <returns>A function delegate that returns the future result to be available through the Task.</returns>
 internal Task DownloadToFileAsync(string destinationPath,BlobTransferClient blobTransferClient, ILocator locator, IRetryPolicy retryPolicy, CancellationToken cancellationToken)
 {
     FileEncryption fileEncryption = this.GetFileEncryption();
     cancellationToken.Register(() => this.Cleanup(null, fileEncryption, null, null));
     return Task.Factory.StartNew(() =>
     {
         cancellationToken.ThrowIfCancellationRequested(() => this.Cleanup(null, fileEncryption, null, null));
         ulong iv = Convert.ToUInt64(this.InitializationVector, CultureInfo.InvariantCulture);
         UriBuilder uriBuilder = new UriBuilder(locator.Path);
         uriBuilder.Path += String.Concat("/", Name);
         blobTransferClient.TransferProgressChanged += this.OnDownloadBlobTransferProgressChanged;
         blobTransferClient.DownloadBlob(uriBuilder.Uri, destinationPath, fileEncryption, iv, cancellationToken, retryPolicy).Wait(cancellationToken);
         cancellationToken.ThrowIfCancellationRequested(() => this.Cleanup(null, fileEncryption, null, null));
     },
             cancellationToken)
         .ContinueWith(
             t =>
             {
                 t.ThrowIfFaulted(() => this.Cleanup(null, fileEncryption, null, null));
                 cancellationToken.ThrowIfCancellationRequested(() => this.Cleanup(null, fileEncryption, null, null));
                 this.Cleanup(null, fileEncryption, null, null);
             },
             cancellationToken);
 }
        public void DoDownloadFileFromAsset(IAsset asset, IAssetFile File, object folder, int index)
        {
            // If download is in the queue, let's wait our turn
            DoGridTransferWaitIfNeeded(index);

            string labeldb = string.Format("Starting download of '{0}' of asset '{1}' to {2}", File.Name, asset.Name, folder as string);
            ILocator sasLocator = null;
            var locatorTask = Task.Factory.StartNew(() =>
            {
                sasLocator = _context.Locators.Create(LocatorType.Sas, asset, AccessPermissions.Read, TimeSpan.FromHours(24));
            });
            locatorTask.Wait();

            TextBoxLogWriteLine(labeldb);

            BlobTransferClient blobTransferClient = new BlobTransferClient
            {
                NumberOfConcurrentTransfers = _context.NumberOfConcurrentTransfers,
                ParallelTransferThreadCount = _context.ParallelTransferThreadCount
            };

            Task.Factory.StartNew(async () =>
            {
                bool Error = false;
                try
                {
                    await File.DownloadAsync(Path.Combine(folder as string, File.Name), blobTransferClient, sasLocator, CancellationToken.None);
                    sasLocator.Delete();
                }
                catch (Exception e)
                {
                    Error = true;
                    TextBoxLogWriteLine(string.Format("Download of file '{0}' failed !", File.Name), true);
                    TextBoxLogWriteLine(e);
                    DoGridTransferDeclareError(index, e);
                }
                if (!Error)
                {
                    DoGridTransferDeclareCompleted(index, folder.ToString());
                }
            });
        }
        /// <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 this._cloudMediaContext.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._cloudMediaContext.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 = new BlobTransferClient
                                               {
                                                   NumberOfConcurrentTransfers = this._cloudMediaContext.NumberOfConcurrentTransfers,
                                                   ParallelTransferThreadCount = this._cloudMediaContext.ParallelTransferThreadCount
                                               };
                            UploadAsync(path, blobTransfer, locator, cancellationToken).Wait();
                            locator.Delete();
                            cancellationToken.ThrowIfCancellationRequested(() => this.Cleanup(null, null, null, accessPolicy));
                            accessPolicy.Delete();
                        },
                    cancellationToken);
        }
        /// <summary>
        /// Uploads the stream asynchronously
        /// </summary>
        /// <param name="stream">The stream to upload</param>
        /// <param name="blobTransferClient">The <see cref="BlobTransferClient"/> which is used to upload files.</param>
        /// <param name="locator">An locator <see cref="ILocator"/> which defines permissions associated with the Asset.</param>
        /// <param name="token">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, BlobTransferClient blobTransferClient, ILocator locator, CancellationToken token)
        {
            if (this.IsFragmented())
            {
                throw new NotSupportedException(StringTable.NotSupportedFragblobUpload);
            }

            if (blobTransferClient == null)
            {
                throw new ArgumentNullException("blobTransferClient");
            }

            if (locator == null)
            {
                throw new ArgumentNullException("locator");
            }

            if (stream == null)
            {
                throw new ArgumentNullException("stream");
            }

            IContentKey          contentKeyData       = null;
            FileEncryption       fileEncryption       = null;
            AssetCreationOptions assetCreationOptions = this.Asset.Options;

            if (assetCreationOptions.HasFlag(AssetCreationOptions.StorageEncrypted))
            {
                contentKeyData = this.Asset.ContentKeys.FirstOrDefault(c => c.ContentKeyType == ContentKeyType.StorageEncryption);
                if (contentKeyData == null)
                {
                    throw new InvalidOperationException(String.Format(CultureInfo.InvariantCulture, StringTable.StorageEncryptionContentKeyIsMissing, this.Asset.Id));
                }

                fileEncryption = new FileEncryption(contentKeyData.GetClearKeyValue(), EncryptionUtils.GetKeyIdAsGuid(contentKeyData.Id));
                ulong iv = Convert.ToUInt64(this.InitializationVector, CultureInfo.InvariantCulture);
                fileEncryption.SetInitializationVectorForFile(this.Name, iv);
            }

            EventHandler <BlobTransferProgressChangedEventArgs> handler = (s, e) => OnUploadProgressChanged(this.Name, e);

            blobTransferClient.TransferProgressChanged += handler;

            MediaRetryPolicy retryPolicy = this.GetMediaContext().MediaServicesClassFactory.GetBlobStorageClientRetryPolicy();

            var streamLength = stream.Length;

            return(blobTransferClient.UploadBlob(
                       new Uri(locator.BaseUri),
                       this.Name,
                       stream,
                       null,
                       fileEncryption,
                       token,
                       retryPolicy.AsAzureStorageClientRetryPolicy(),
                       () => locator.ContentAccessComponent)
                   .ContinueWith(
                       ts =>
            {
                blobTransferClient.TransferProgressChanged -= handler;
                this.PostUploadAction(ts, streamLength, token);
            }));
        }
        /// <summary>
        /// Returns a <see cref="System.Threading.Tasks.Task"/> instance to download all the asset files in the <paramref name="asset"/> to the <paramref name="folderPath"/>.
        /// </summary>
        /// <param name="asset">The <see cref="IAsset"/> instance where to download the asset files.</param>
        /// <param name="folderPath">The path to the folder where to download the asset files in the <paramref name="asset"/>.</param>
        /// <param name="downloadProgressChangedCallback">A callback to report download progress for each asset file in the <paramref name="asset"/>.</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 download all the asset files in the <paramref name="asset"/>.</returns>
        public static async Task DownloadToFolderAsync(this IAsset asset, string folderPath, Action<IAssetFile, DownloadProgressChangedEventArgs> downloadProgressChangedCallback, CancellationToken cancellationToken)
        {
            if (asset == null)
            {
                throw new ArgumentNullException("asset", "The asset cannot be null.");
            }

            if (!Directory.Exists(folderPath))
            {
                throw new ArgumentException(
                    string.Format(CultureInfo.InvariantCulture, "The folder '{0}' does not exist.", folderPath),
                    "folderPath");
            }

            MediaContextBase context = asset.GetMediaContext();

            ILocator sasLocator = await context.Locators.CreateAsync(LocatorType.Sas, asset, AccessPermissions.Read, AssetBaseCollectionExtensions.DefaultAccessPolicyDuration);

            EventHandler<DownloadProgressChangedEventArgs> downloadProgressChangedHandler =
                (s, e) =>
                {
                    IAssetFile assetFile = (IAssetFile)s;
                    DownloadProgressChangedEventArgs eventArgs = e;

                    if (downloadProgressChangedCallback != null)
                    {
                        downloadProgressChangedCallback(assetFile, eventArgs);
                    }
                };

            List<Task> downloadTasks = new List<Task>();
            List<IAssetFile> assetFiles = asset.AssetFiles.ToList();
            foreach (IAssetFile assetFile in assetFiles)
            {
                string localDownloadPath = Path.Combine(folderPath, assetFile.Name);
                BlobTransferClient blobTransferClient = new BlobTransferClient
                {
                    NumberOfConcurrentTransfers = context.NumberOfConcurrentTransfers,
                    ParallelTransferThreadCount = context.ParallelTransferThreadCount
                };

                assetFile.DownloadProgressChanged += downloadProgressChangedHandler;

                downloadTasks.Add(
                    assetFile.DownloadAsync(Path.GetFullPath(localDownloadPath), blobTransferClient, sasLocator, cancellationToken));
            }

            await Task.Factory.ContinueWhenAll(downloadTasks.ToArray(), t => t, TaskContinuationOptions.ExecuteSynchronously);

            await sasLocator.DeleteAsync();

            assetFiles.ForEach(af => af.DownloadProgressChanged -= downloadProgressChangedHandler);
        }
        /// <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._cloudMediaContext.AccessPolicies.Create("SdkDownload", TimeSpan.FromHours(12), AccessPermissions.Read);
                locator = this._cloudMediaContext.Locators.CreateSasLocator(this.Asset, accessPolicy);
                BlobTransferClient blobTransfer = new BlobTransferClient
                            {
                                NumberOfConcurrentTransfers = this._cloudMediaContext.NumberOfConcurrentTransfers,
                                ParallelTransferThreadCount = this._cloudMediaContext.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();
                }

            }
        }
        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;
                    }
                }
            }
        }
Esempio n. 27
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;
        }
 /// <summary>
 /// Asynchronously downloads the represented file to the specified destination path.
 /// </summary>
 /// <param name="destinationPath">The path to download the file to.</param>
 /// <param name="blobTransferClient">The <see cref="BlobTransferClient"/> which is used to download files.</param>
 /// <param name="locator">An asset <see cref="ILocator"/> which defines permissions associated with the Asset.</param>
 /// <param name="cancellationToken">The cancellation token.</param>
 /// <returns>
 /// A function delegate that returns the future result to be available through the Task.
 /// </returns>
 public Task DownloadAsync(string destinationPath, BlobTransferClient blobTransferClient, ILocator locator, CancellationToken cancellationToken)
 {
     return(this.DownloadToFileAsync(destinationPath, blobTransferClient, locator, AzureStorageClientRetryPolicyFactory.DefaultPolicy.AsAzureStorageClientRetryPolicy(), cancellationToken));
 }