コード例 #1
0
        /// <summary>
        /// Copies the files in the <paramref name="sourceAsset"/> into into the <paramref name="destinationAsset"/> instance.
        /// </summary>
        /// <param name="sourceAsset">The <see cref="IAsset"/> instance that contains the asset files to copy.</param>
        /// <param name="destinationAsset">The <see cref="IAsset"/> instance that receives asset files.</param>
        /// <param name="destinationStorageCredentials">The <see cref="Microsoft.WindowsAzure.Storage.Auth.StorageCredentials"/> instance for the <paramref name="destinationAsset"/> Storage Account.</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 copy the files in the <paramref name="sourceAsset"/> into into the <paramref name="destinationAsset"/> instance.</returns>
        public static async Task CopyAsync(this IAsset sourceAsset, IAsset destinationAsset, StorageCredentials destinationStorageCredentials, CancellationToken cancellationToken)
        {
            if (sourceAsset == null)
            {
                throw new ArgumentNullException("sourceAsset", "The source asset cannot be null.");
            }

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

            if (destinationStorageCredentials == null)
            {
                throw new ArgumentNullException("destinationStorageCredentials", "The destination storage credentials cannot be null.");
            }

            if (destinationStorageCredentials.IsAnonymous || destinationStorageCredentials.IsSAS)
            {
                throw new ArgumentException("The destination storage credentials must contain the account key credentials.", "destinationStorageCredentials");
            }

            if (!string.IsNullOrWhiteSpace(destinationStorageCredentials.AccountName) && !destinationStorageCredentials.AccountName.Equals(destinationAsset.StorageAccountName, StringComparison.OrdinalIgnoreCase))
            {
                throw new ArgumentException("The destination storage credentials does not belong to the destination asset storage account.", "destinationStorageCredentials");
            }

            MediaContextBase sourceContext = sourceAsset.GetMediaContext();
            ILocator         sourceLocator = null;

            try
            {
                sourceLocator = await sourceContext.Locators.CreateAsync(LocatorType.Sas, sourceAsset, AccessPermissions.Read | AccessPermissions.List, AssetBaseCollectionExtensions.DefaultAccessPolicyDuration).ConfigureAwait(false);

                cancellationToken.ThrowIfCancellationRequested();

                IRetryPolicy       retryPolicy = sourceContext.MediaServicesClassFactory.GetBlobStorageClientRetryPolicy().AsAzureStorageClientRetryPolicy();
                BlobRequestOptions options     = new BlobRequestOptions {
                    RetryPolicy = retryPolicy
                };
                CloudBlobContainer sourceContainer      = new CloudBlobContainer(sourceAsset.Uri, new StorageCredentials(sourceLocator.ContentAccessComponent));
                CloudBlobContainer destinationContainer = new CloudBlobContainer(destinationAsset.Uri, destinationStorageCredentials);

                await CopyBlobHelpers.CopyBlobsAsync(sourceContainer, destinationContainer, options, cancellationToken).ConfigureAwait(false);

                cancellationToken.ThrowIfCancellationRequested();

                await CopyAssetFilesAsync(sourceAsset, destinationAsset, cancellationToken).ConfigureAwait(false);
            }
            finally
            {
                if (sourceLocator != null)
                {
                    sourceLocator.Delete();
                }
            }
        }
コード例 #2
0
        /// <summary>
        /// Returns a <see cref="System.Threading.Tasks.Task&lt;IAsset&gt;"/> 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&lt;IAsset&gt;"/> 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);
        }