/// <summary> /// Returns a <see cref="System.Threading.Tasks.Task<IAsset>"/> instance for a new <see cref="IAsset"/> with the files in <paramref name="folderPath"/>. /// </summary> /// <param name="context">The <see cref="CloudMediaContext"/> instance.</param> /// <param name="folderPath">The path to the folder with the files to upload to the new <see cref="IAsset"/>.</param> /// <param name="storageAccountName">The name of the Storage Account where to store the new <see cref="IAsset"/>.</param> /// <param name="options">The <see cref="AssetCreationOptions"/>.</param> /// <param name="uploadProgressChangedCallback">A callback to report upload progress of the files.</param> /// <param name="cancellationToken">The <see cref="System.Threading.CancellationToken"/> instance used for cancellation.</param> /// <returns>A <see cref="System.Threading.Tasks.Task<IAsset>"/> instance for a new <see cref="IAsset"/> with the files in <paramref name="folderPath"/>.</returns> public static async Task <IAsset> CreateAssetFromFolderAsync(this CloudMediaContext context, string folderPath, string storageAccountName, AssetCreationOptions options, Action <IAssetFile, UploadProgressChangedEventArgs> uploadProgressChangedCallback, CancellationToken cancellationToken) { if (context == null) { throw new ArgumentNullException("context", "The context cannot be null."); } IEnumerable <string> filePaths = Directory.EnumerateFiles(folderPath); if (!filePaths.Any()) { throw new FileNotFoundException( string.Format(CultureInfo.InvariantCulture, "No files in directory, check the folder path: '{0}'", folderPath)); } if (string.IsNullOrWhiteSpace(storageAccountName)) { storageAccountName = context.DefaultStorageAccount.Name; } string assetName = Path.GetFileName(Path.GetFullPath(folderPath.TrimEnd('\\'))); IAsset asset = await context.Assets.CreateAsync(assetName, storageAccountName, options, cancellationToken); ILocator sasLocator = await context.CreateLocatorAsync(asset, LocatorType.Sas, AccessPermissions.Write | AccessPermissions.List, DefaultAccessPolicyDuration); EventHandler <UploadProgressChangedEventArgs> uploadProgressChangedHandler = (s, e) => { IAssetFile assetFile = (IAssetFile)s; UploadProgressChangedEventArgs eventArgs = e; if (uploadProgressChangedCallback != null) { uploadProgressChangedCallback(assetFile, eventArgs); } }; IList <Task> uploadTasks = new List <Task>(); foreach (string filePath in filePaths) { uploadTasks.Add( context.CreateAssetFileFromLocalFileAsync(asset, filePath, sasLocator, uploadProgressChangedHandler, cancellationToken)); } await Task.WhenAll(uploadTasks); await sasLocator.DeleteAsync(); return(asset); }
/// <summary> /// Returns a <see cref="System.Threading.Tasks.Task<IAsset>"/> instance for a new <see cref="IAsset"/> with the file in <paramref name="filePath"/>. /// </summary> /// <param name="context">The <see cref="CloudMediaContext"/> instance.</param> /// <param name="filePath">The path to the file to upload to the new <see cref="IAsset"/>.</param> /// <param name="storageAccountName">The name of the Storage Account where to store the new <see cref="IAsset"/>.</param> /// <param name="options">The <see cref="AssetCreationOptions"/>.</param> /// <param name="uploadProgressChangedCallback">A callback to report the upload progress of the file.</param> /// <param name="cancellationToken">The <see cref="System.Threading.CancellationToken"/> instance used for cancellation.</param> /// <returns>A <see cref="System.Threading.Tasks.Task<IAsset>"/> instance for a new <see cref="IAsset"/> with the file in <paramref name="filePath"/>.</returns> public static async Task <IAsset> CreateAssetFromFileAsync(this CloudMediaContext context, string filePath, string storageAccountName, AssetCreationOptions options, Action <IAssetFile, UploadProgressChangedEventArgs> uploadProgressChangedCallback, CancellationToken cancellationToken) { if (context == null) { throw new ArgumentNullException("context", "The context cannot be null."); } string assetName = Path.GetFileName(filePath); if (string.IsNullOrWhiteSpace(storageAccountName)) { storageAccountName = context.DefaultStorageAccount.Name; } IAsset asset = await context.Assets.CreateAsync(assetName, storageAccountName, options, cancellationToken); ILocator sasLocator = await context.CreateLocatorAsync(asset, LocatorType.Sas, AccessPermissions.Write | AccessPermissions.List, DefaultAccessPolicyDuration); EventHandler <UploadProgressChangedEventArgs> uploadProgressChangedHandler = (s, e) => { IAssetFile assetFile = (IAssetFile)s; UploadProgressChangedEventArgs eventArgs = e; if (uploadProgressChangedCallback != null) { uploadProgressChangedCallback(assetFile, eventArgs); } }; await context.CreateAssetFileFromLocalFileAsync(asset, filePath, sasLocator, uploadProgressChangedHandler, cancellationToken); await sasLocator.DeleteAsync(); return(asset); }