示例#1
0
		/// <summary>
		/// Downloads the specified blob to the specified location.
		/// </summary>
		/// <param name="uri">The blob url from which file a needs should be downloaded. If blob has private read permissions then an appropriate SAS url need to be passed.</param>
		/// <param name="localFile">The full path where file will be saved.</param>
		/// <param name="fileEncryption">The file encryption if file has been encrypted. Pass null if no encryption has been used.</param>
		/// <param name="initializationVector">The initialization vector if encryption has been used.</param>
		/// <param name="client">The azure client to access a blob.</param>
		/// <param name="cancellationToken">The cancellation token to cancel the download operation.</param>
		/// <param name="retryPolicy">The RetryPolicy delegate returns a ShouldRetry delegate, which can be used to implement a custom retry policy.RetryPolicies class can bee used to get default policies</param>
        /// <param name="getSharedAccessSignature">A callback function which returns Sas signature for the file to be downloaded</param>
		/// <param name="start">Start pos to download</param>
		/// <param name="length">Number of bytes to download, -1 to download all.</param>
		/// <returns>A task that downloads the specified blob.</returns>
		public virtual Task DownloadBlob(
			Uri uri, 
			string localFile, 
			FileEncryption fileEncryption, 
			ulong initializationVector, 
			CloudBlobClient client, 
			CancellationToken cancellationToken, 
			IRetryPolicy retryPolicy,
			Func<string> getSharedAccessSignature = null, 
			long start = 0, 
			long length = -1
            )
		{
			if (client != null && getSharedAccessSignature != null)
			{
				throw new InvalidOperationException("The arguments client and sharedAccessSignature cannot both be non-null");
			}

			if (start < 0)
			{
				throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, "Initial offset {0} to download a file must not be negative", start));
			}

			// To download the asset file as a whole or byte-range, the localFile should not be empty
			if (string.IsNullOrEmpty(localFile))
			{
				throw new ArgumentException("Parameter localFile must be set with a full path");
			}

			if (length < -1)
			{
				throw new ArgumentException("Parameter length must be equals or greater than -1");
			}

			if (_forceSharedAccessSignatureRetry != TimeSpan.Zero)
			{
				// The following sleep is for unit test purpose and we will force the shared access signature to expire and hit retry code path
				Thread.Sleep(_forceSharedAccessSignatureRetry);
			}

			BlobDownloader blobDownloader = new BlobDownloader(new MemoryManagerFactory());

			blobDownloader.TransferCompleted += (sender, args) =>
				{
					if (TransferCompleted != null)
					{
						TransferCompleted(sender, args);
					}
					else if (args.Error != null)
					{
						throw args.Error;
					}
				};

			blobDownloader.TransferProgressChanged += (sender, args) =>
				{
					if (TransferProgressChanged != null)
					{
						TransferProgressChanged(sender, args);
					}
				};

			return blobDownloader.DownloadBlob(
				uri,
				localFile,
				fileEncryption,
				initializationVector,
				client,
				cancellationToken,
				retryPolicy,
				getSharedAccessSignature,
				start,
                length,
                parallelTransferThreadCount: ParallelTransferThreadCount,
                numberOfConcurrentTransfers: NumberOfConcurrentTransfers);
		}
        /// <summary>
        /// Downloads the specified blob to the specified location.
        /// </summary>
        /// <param name="uri">The blob url from which file a needs should be downloaded. If blob has private read permissions then an appropriate SAS url need to be passed.</param>
        /// <param name="localFile">The full path where file will be saved.</param>
        /// <param name="fileEncryption">The file encryption if file has been encrypted. Pass null if no encryption has been used.</param>
        /// <param name="initializationVector">The initialization vector if encryption has been used.</param>
        /// <param name="client">The azure client to access a blob.</param>
        /// <param name="cancellationToken">The cancellation token to cancel the download operation.</param>
        /// <param name="retryPolicy">The RetryPolicy delegate returns a ShouldRetry delegate, which can be used to implement a custom retry policy.RetryPolicies class can bee used to get default policies</param>
        /// <param name="getSharedAccessSignature">A callback function which returns Sas signature for the file to be downloaded</param>
        /// <param name="start">Start pos to download</param>
        /// <param name="length">Number of bytes to download, -1 to download all.</param>
        /// <returns>A task that downloads the specified blob.</returns>
        public virtual Task DownloadBlob(
			Uri uri, 
			string localFile, 
			FileEncryption fileEncryption, 
			ulong initializationVector, 
			CloudBlobClient client, 
			CancellationToken cancellationToken, 
			IRetryPolicy retryPolicy,
			Func<string> getSharedAccessSignature = null, 
			long start = 0, 
			long length = -1
            )
        {
            if (client != null && getSharedAccessSignature != null)
            {
                throw new InvalidOperationException("The arguments client and sharedAccessSignature cannot both be non-null");
            }

            if (start < 0)
            {
                throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, "Initial offset {0} to download a file must not be negative", start));
            }

            // To download the asset file as a whole or byte-range, the localFile should not be empty
            if (string.IsNullOrEmpty(localFile))
            {
                throw new ArgumentException("Parameter localFile must be set with a full path");
            }

            if (length < -1)
            {
                throw new ArgumentException("Parameter length must be equals or greater than -1");
            }

            if (_forceSharedAccessSignatureRetry != TimeSpan.Zero)
            {
                // The following sleep is for unit test purpose and we will force the shared access signature to expire and hit retry code path
                Thread.Sleep(_forceSharedAccessSignatureRetry);
            }

            BlobDownloader blobDownloader = new BlobDownloader(new MemoryManagerFactory());

            blobDownloader.TransferCompleted += (sender, args) =>
                {
                    if (TransferCompleted != null)
                    {
                        TransferCompleted(sender, args);
                    }
                    else if (args.Error != null)
                    {
                        throw args.Error;
                    }
                };

            blobDownloader.TransferProgressChanged += (sender, args) =>
                {
                    if (TransferProgressChanged != null)
                    {
                        TransferProgressChanged(sender, args);
                    }
                };

            return blobDownloader.DownloadBlob(
                uri,
                localFile,
                fileEncryption,
                initializationVector,
                client,
                cancellationToken,
                retryPolicy,
                getSharedAccessSignature,
                start,
                length,
                parallelTransferThreadCount: ParallelTransferThreadCount,
                numberOfConcurrentTransfers: NumberOfConcurrentTransfers);
        }