/// <summary> /// Returns a new <see cref="IAsset"/> with the files in <paramref name="folderPath"/>. /// </summary> /// <param name="assets">The <see cref="AssetBaseCollection"/> 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> /// <returns>A new <see cref="IAsset"/> with the files in <paramref name="folderPath"/>.</returns> public static IAsset CreateFromFolder(this AssetBaseCollection assets, string folderPath, string storageAccountName, AssetCreationOptions options, Action <IAssetFile, UploadProgressChangedEventArgs> uploadProgressChangedCallback) { using (Task <IAsset> task = assets.CreateFromFolderAsync(folderPath, storageAccountName, options, uploadProgressChangedCallback, CancellationToken.None)) { return(task.Result); } }
/// <summary> /// Returns a new <see cref="IAsset"/> with the file in <paramref name="filePath"/>. /// </summary> /// <param name="assets">The <see cref="AssetBaseCollection"/> instance.</param> /// <param name="filePath">The path to the file to upload to the new <see cref="IAsset"/>.</param> /// <param name="strategy">The <see cref="IAccountSelectionStrategy"/> used to select a storage account for the new asset.</param> /// <param name="options">The <see cref="AssetCreationOptions"/>.</param> /// <param name="uploadProgressChangedCallback">A callback to report the upload progress of the file.</param> /// <returns>A new <see cref="IAsset"/> with the file in <paramref name="filePath"/>.</returns> public static IAsset CreateFromFile(this AssetBaseCollection assets, string filePath, IAccountSelectionStrategy strategy, AssetCreationOptions options, Action <IAssetFile, UploadProgressChangedEventArgs> uploadProgressChangedCallback) { using (Task <IAsset> task = assets.CreateFromFileAsync(filePath, strategy, options, uploadProgressChangedCallback, CancellationToken.None)) { return(task.Result); } }
/// <summary> /// Returns a new <see cref="IAsset"/> with the file in <paramref name="sourceBlob"/>. /// </summary> /// <param name="assets">The <see cref="AssetBaseCollection"/> instance.</param> /// <param name="sourceBlob">The <see cref="Microsoft.WindowsAzure.Storage.Blob.CloudBlockBlob"/> instance that contains the file to copy.</param> /// <param name="storageCredentials">The <see cref="Microsoft.WindowsAzure.Storage.Auth.StorageCredentials"/> instance for the new asset to create.</param> /// <param name="options">The <see cref="AssetCreationOptions"/>.</param> /// <returns>A new <see cref="IAsset"/> with the file in <paramref name="sourceBlob"/>.</returns> public static IAsset CreateFromBlob(this AssetBaseCollection assets, CloudBlockBlob sourceBlob, StorageCredentials storageCredentials, AssetCreationOptions options) { using (Task <IAsset> task = assets.CreateFromBlobAsync(sourceBlob, storageCredentials, options, CancellationToken.None)) { return(task.Result); } }
/// <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="assets">The <see cref="AssetBaseCollection"/> instance.</param> /// <param name="filePath">The path to the file to upload to the new <see cref="IAsset"/>.</param> /// <param name="strategy">The <see cref="IAccountSelectionStrategy"/> used to select a storage account for the new asset.</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 Task <IAsset> CreateFromFileAsync(this AssetBaseCollection assets, string filePath, IAccountSelectionStrategy strategy, AssetCreationOptions options, Action <IAssetFile, UploadProgressChangedEventArgs> uploadProgressChangedCallback, CancellationToken cancellationToken) { if (strategy == null) { throw new ArgumentNullException("strategy"); } string storageAccountName = strategy.SelectAccountForAsset(); return(assets.CreateFromFileAsync(filePath, storageAccountName, options, uploadProgressChangedCallback, cancellationToken)); }
/// <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="assets">The <see cref="AssetBaseCollection"/> 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> CreateFromFolderAsync(this AssetBaseCollection assets, string folderPath, string storageAccountName, AssetCreationOptions options, Action <IAssetFile, UploadProgressChangedEventArgs> uploadProgressChangedCallback, CancellationToken cancellationToken) { if (assets == null) { throw new ArgumentNullException("assets", "The assets collection 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)); } MediaContextBase context = assets.MediaContext; 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).ConfigureAwait(false); ILocator sasLocator = await context.Locators.CreateAsync(LocatorType.Sas, asset, AccessPermissions.Write | AccessPermissions.List, DefaultAccessPolicyDuration).ConfigureAwait(false); 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(asset.CreateAssetFileFromLocalFileAsync(filePath, sasLocator, uploadProgressChangedHandler, cancellationToken)); } await Task.Factory.ContinueWhenAll(uploadTasks.ToArray(), t => t, TaskContinuationOptions.ExecuteSynchronously).ConfigureAwait(false); await sasLocator.DeleteAsync().ConfigureAwait(false); 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="sourceBlob"/>. /// </summary> /// <param name="assets">The <see cref="AssetBaseCollection"/> instance.</param> /// <param name="sourceBlob">The <see cref="Microsoft.WindowsAzure.Storage.Blob.CloudBlockBlob"/> instance that contains the file to copy.</param> /// <param name="storageCredentials">The <see cref="Microsoft.WindowsAzure.Storage.Auth.StorageCredentials"/> instance for the new asset to create.</param> /// <param name="options">The <see cref="AssetCreationOptions"/>.</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="sourceBlob"/>.</returns> public static async Task <IAsset> CreateFromBlobAsync(this AssetBaseCollection assets, CloudBlockBlob sourceBlob, StorageCredentials storageCredentials, AssetCreationOptions options, CancellationToken cancellationToken) { if (assets == null) { throw new ArgumentNullException("assets", "The assets collection cannot be null."); } if (sourceBlob == null) { throw new ArgumentNullException("sourceBlob", "The source blob cannot be null."); } if (storageCredentials == null) { throw new ArgumentNullException("storageCredentials", "The destination storage credentials cannot be null."); } if (storageCredentials.IsAnonymous || storageCredentials.IsSAS) { throw new ArgumentException("The destination storage credentials must contain the account key credentials.", "destinationStorageCredentials"); } MediaContextBase context = assets.MediaContext; IAsset asset = await assets.CreateAsync(sourceBlob.Name, storageCredentials.AccountName, options, cancellationToken).ConfigureAwait(false); cancellationToken.ThrowIfCancellationRequested(); IRetryPolicy retryPolicy = context.MediaServicesClassFactory.GetBlobStorageClientRetryPolicy().AsAzureStorageClientRetryPolicy(); BlobRequestOptions blobOptions = new BlobRequestOptions { RetryPolicy = retryPolicy }; CloudBlobContainer container = new CloudBlobContainer(asset.Uri, storageCredentials); CloudBlockBlob blob = container.GetBlockBlobReference(sourceBlob.Name); await CopyBlobHelpers.CopyBlobAsync(sourceBlob, blob, blobOptions, cancellationToken).ConfigureAwait(false); cancellationToken.ThrowIfCancellationRequested(); IAssetFile assetFile = await asset.AssetFiles.CreateAsync(sourceBlob.Name, cancellationToken).ConfigureAwait(false); assetFile.IsPrimary = true; if (sourceBlob.Properties != null) { assetFile.ContentFileSize = sourceBlob.Properties.Length; assetFile.MimeType = sourceBlob.Properties.ContentType; } await assetFile.UpdateAsync().ConfigureAwait(false); return(asset); }
/// <summary> /// Returns a <see cref="System.Threading.Tasks.Task<IAsset>"/> instance for a new empty <see cref="IAsset"/> asset within one selected storage account from <paramref name="storageAccountNames"/> based on the default <see cref="IAccountSelectionStrategy"/>. /// </summary> /// <param name="assets">The <see cref="AssetBaseCollection"/> instance.</param> /// <param name="assetName">The asset name.</param> /// <param name="strategy">The <see cref="IAccountSelectionStrategy"/> used to select a storage account for the new asset.</param> /// <param name="options">The <see cref="AssetCreationOptions"/>.</param> /// <param name="token">The <see cref="System.Threading.CancellationToken"/> instance used for cancellation.</param> /// <returns>A <see cref="System.Threading.Tasks.Task<IAsset>"/> instance for a new empty <see cref="IAsset"/> within one selected storage account from the given <see cref="IAccountSelectionStrategy"/>.</returns> public static Task <IAsset> CreateAsync(this AssetBaseCollection assets, string assetName, IAccountSelectionStrategy strategy, AssetCreationOptions options, CancellationToken token) { if (assets == null) { throw new ArgumentNullException("assets"); } if (strategy == null) { throw new ArgumentNullException("strategy"); } string storageAccountName = strategy.SelectAccountForAsset(); return(assets.CreateAsync(assetName, storageAccountName, options, token)); }
/// <summary> /// Returns a new empty <see cref="IAsset"/> within one selected storage account from <paramref name="storageAccountNames"/> based on the default <see cref="IAccountSelectionStrategy"/>. /// </summary> /// <param name="assets">The <see cref="AssetBaseCollection"/> instance.</param> /// <param name="assetName">The asset name.</param> /// <param name="strategy">The <see cref="IAccountSelectionStrategy"/> used to select a storage account for the new asset.</param> /// <param name="options">The <see cref="AssetCreationOptions"/>.</param> /// <returns>A new empty <see cref="IAsset"/> within one selected storage account from the provided <see cref="IAccountSelectionStrategy"/>.</returns> public static IAsset Create(this AssetBaseCollection assets, string assetName, IAccountSelectionStrategy strategy, AssetCreationOptions options) { if (assets == null) { throw new ArgumentNullException("assets"); } if (strategy == null) { throw new ArgumentNullException("strategy"); } string storageAccountName = strategy.SelectAccountForAsset(); return(assets.Create(assetName, storageAccountName, options)); }
private void PostUploadAction(Task task, string path, FileEncryption fileEncryption, AssetCreationOptions assetCreationOptions, CancellationToken token) { try { task.ThrowIfFaulted(); token.ThrowIfCancellationRequested(); FileInfo fileInfo = new FileInfo(path); //Updating Name based on file name to avoid exceptions trying to download file.Mapping to storage account is through file name this.Name = fileInfo.Name; // Set the ContentFileSize base on the local file size this.ContentFileSize = fileInfo.Length; // Update the files associated with the asset with the encryption related metadata. if (assetCreationOptions.HasFlag(AssetCreationOptions.StorageEncrypted)) { AssetBaseCollection.AddEncryptionMetadataToAssetFile(this, fileEncryption); } else if (assetCreationOptions.HasFlag(AssetCreationOptions.CommonEncryptionProtected)) { AssetBaseCollection.SetAssetFileForCommonEncryption(this); } else if (assetCreationOptions.HasFlag(AssetCreationOptions.EnvelopeEncryptionProtected)) { AssetBaseCollection.SetAssetFileForEnvelopeEncryption(this); } this.Update(); } finally { if (fileEncryption != null) { fileEncryption.Dispose(); } } }
/// <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="assets">The <see cref="AssetBaseCollection"/> 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> CreateFromFileAsync(this AssetBaseCollection assets, string filePath, string storageAccountName, AssetCreationOptions options, Action <IAssetFile, UploadProgressChangedEventArgs> uploadProgressChangedCallback, CancellationToken cancellationToken) { if (assets == null) { throw new ArgumentNullException("assets", "The assets collection cannot be null."); } MediaContextBase context = assets.MediaContext; string assetName = Path.GetFileName(filePath); if (string.IsNullOrWhiteSpace(storageAccountName)) { storageAccountName = context.DefaultStorageAccount.Name; } IAsset asset = await assets.CreateAsync(assetName, storageAccountName, options, cancellationToken).ConfigureAwait(false); ILocator sasLocator = await context.Locators.CreateAsync(LocatorType.Sas, asset, AccessPermissions.Write | AccessPermissions.List, DefaultAccessPolicyDuration).ConfigureAwait(false); EventHandler <UploadProgressChangedEventArgs> uploadProgressChangedHandler = (s, e) => { IAssetFile assetFile = (IAssetFile)s; UploadProgressChangedEventArgs eventArgs = e; if (uploadProgressChangedCallback != null) { uploadProgressChangedCallback(assetFile, eventArgs); } }; await asset.CreateAssetFileFromLocalFileAsync(filePath, sasLocator, uploadProgressChangedHandler, cancellationToken).ConfigureAwait(false); await sasLocator.DeleteAsync().ConfigureAwait(false); return(asset); }
/// <summary> /// Returns a new <see cref="IAsset"/> with the file in <paramref name="filePath"/>. /// </summary> /// <param name="assets">The <see cref="AssetBaseCollection"/> 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> /// <returns>A new <see cref="IAsset"/> with the file in <paramref name="filePath"/>.</returns> public static IAsset CreateFromFile(this AssetBaseCollection assets, string filePath, string storageAccountName, AssetCreationOptions options) { return(assets.CreateFromFile(filePath, storageAccountName, options, null)); }
public override Task <IAssetFile> CreateAsync(string name, CancellationToken cancelation) { if (_parentAsset == null) { throw new InvalidOperationException(StringTable.AssetFileCreateParentAssetIsNull); } if (string.IsNullOrWhiteSpace(name)) { throw new ArgumentException(String.Format(CultureInfo.InvariantCulture, StringTable.ErrorCreatingAssetFileEmptyFileName)); } cancelation.ThrowIfCancellationRequested(); IMediaDataServiceContext dataContext = null; AssetFileData assetFile = null; return(Task.Factory.StartNew(() => { cancelation.ThrowIfCancellationRequested(); dataContext = MediaContext.MediaServicesClassFactory.CreateDataServiceContext(); FileEncryption fileEncryption = null; // Set a MIME type based on the extension of the file name string mimeType = AssetFileData.GetMimeType(name); assetFile = new AssetFileData { Name = name, ParentAssetId = _parentAsset.Id, MimeType = mimeType, }; try { // Update the files associated with the asset with the encryption related metadata. if (_parentAsset.Options.HasFlag(AssetCreationOptions.StorageEncrypted)) { IContentKey storageEncryptionKey = _parentAsset.ContentKeys.Where(c => c.ContentKeyType == ContentKeyType.StorageEncryption).FirstOrDefault(); cancelation.ThrowIfCancellationRequested(); if (storageEncryptionKey == null) { throw new InvalidOperationException(String.Format(CultureInfo.InvariantCulture, StringTable.StorageEncryptionContentKeyIsMissing, _parentAsset.Id)); } fileEncryption = new FileEncryption(storageEncryptionKey.GetClearKeyValue(), EncryptionUtils.GetKeyIdAsGuid(storageEncryptionKey.Id)); AssetBaseCollection.AddEncryptionMetadataToAssetFile(assetFile, fileEncryption); } else if (_parentAsset.Options.HasFlag(AssetCreationOptions.CommonEncryptionProtected)) { AssetBaseCollection.SetAssetFileForCommonEncryption(assetFile); } else if (_parentAsset.Options.HasFlag(AssetCreationOptions.EnvelopeEncryptionProtected)) { AssetBaseCollection.SetAssetFileForEnvelopeEncryption(assetFile); } } finally { if (fileEncryption != null) { fileEncryption.Dispose(); } } dataContext.AddObject(FileSet, assetFile); cancelation.ThrowIfCancellationRequested(); cancelation.ThrowIfCancellationRequested(); MediaRetryPolicy retryPolicy = this.MediaContext.MediaServicesClassFactory.GetSaveChangesRetryPolicy(dataContext as IRetryPolicyAdapter); return retryPolicy.ExecuteAsync <IMediaDataServiceResponse>(() => { cancelation.ThrowIfCancellationRequested(); return dataContext.SaveChangesAsync(assetFile); }, cancelation).Result; }, cancelation) .ContinueWith <IAssetFile>(t => { t.ThrowIfFaulted(); AssetFileData data = (AssetFileData)t.Result.AsyncState; return data; }, cancelation)); }
/// <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="assets">The <see cref="AssetBaseCollection"/> instance.</param> /// <param name="folderPath">The path to the folder with the files to upload to 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 Task <IAsset> CreateFromFolderAsync(this AssetBaseCollection assets, string folderPath, AssetCreationOptions options, Action <IAssetFile, UploadProgressChangedEventArgs> uploadProgressChangedCallback, CancellationToken cancellationToken) { return(assets.CreateFromFolderAsync(folderPath, (string)null, options, uploadProgressChangedCallback, cancellationToken)); }
/// <summary> /// Returns a <see cref="System.Threading.Tasks.Task<IAsset>"/> instance for the new <see cref="IAsset"/>. /// </summary> /// <param name="assets">The <see cref="AssetBaseCollection"/> instance.</param> /// <param name="folderPath">The path to the folder with the files to upload to the new <see cref="IAsset"/>.</param> /// <param name="options">The <see cref="AssetCreationOptions"/>.</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 new <see cref="IAsset"/>.</returns> public static Task <IAsset> CreateFromFolderAsync(this AssetBaseCollection assets, string folderPath, AssetCreationOptions options, CancellationToken cancellationToken) { return(assets.CreateFromFolderAsync(folderPath, options, null, cancellationToken)); }
/// <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="assets">The <see cref="AssetBaseCollection"/> 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="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 Task <IAsset> CreateFromFileAsync(this AssetBaseCollection assets, string filePath, string storageAccountName, AssetCreationOptions options, CancellationToken cancellationToken) { return(assets.CreateFromFileAsync(filePath, storageAccountName, options, null, cancellationToken)); }
/// <summary> /// Returns a new <see cref="IAsset"/> with the files in <paramref name="folderPath"/>. /// </summary> /// <param name="assets">The <see cref="AssetBaseCollection"/> instance.</param> /// <param name="folderPath">The path to the folder with the files to upload to the new <see cref="IAsset"/>.</param> /// <param name="options">The <see cref="AssetCreationOptions"/>.</param> /// <returns>A new <see cref="IAsset"/> with the files in <paramref name="folderPath"/>.</returns> public static IAsset CreateFromFolder(this AssetBaseCollection assets, string folderPath, AssetCreationOptions options) { return(assets.CreateFromFolder(folderPath, options, null)); }
/// <summary> /// Returns a new <see cref="IAsset"/> with the files in <paramref name="folderPath"/>. /// </summary> /// <param name="assets">The <see cref="AssetBaseCollection"/> instance.</param> /// <param name="folderPath">The path to the folder with the files to upload to 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> /// <returns>A new <see cref="IAsset"/> with the files in <paramref name="folderPath"/>.</returns> public static IAsset CreateFromFolder(this AssetBaseCollection assets, string folderPath, AssetCreationOptions options, Action <IAssetFile, UploadProgressChangedEventArgs> uploadProgressChangedCallback) { return(assets.CreateFromFolder(folderPath, (string)null, options, uploadProgressChangedCallback)); }