/// <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 <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); }