Example #1
0
        public override void ExecuteCmdlet()
        {
            CloudFile fileToBeDownloaded;

            string[] path = NamingUtil.ValidatePath(this.Path, true);
            switch (this.ParameterSetName)
            {
            case LocalConstants.FileParameterSetName:
                fileToBeDownloaded = this.File;
                break;

            case LocalConstants.ShareNameParameterSetName:
                var share = this.BuildFileShareObjectFromName(this.ShareName);
                fileToBeDownloaded = share.GetRootDirectoryReference().GetFileReferenceByPath(path);
                break;

            case LocalConstants.ShareParameterSetName:
                fileToBeDownloaded = this.Share.GetRootDirectoryReference().GetFileReferenceByPath(path);
                break;

            case LocalConstants.DirectoryParameterSetName:
                fileToBeDownloaded = this.Directory.GetFileReferenceByPath(path);
                break;

            default:
                throw new PSArgumentException(string.Format(CultureInfo.InvariantCulture, "Invalid parameter set name: {0}", this.ParameterSetName));
            }

            string resolvedDestination = this.GetUnresolvedProviderPathFromPSPath(
                string.IsNullOrWhiteSpace(this.Destination) ? "." : this.Destination);

            FileMode mode = this.Force ? FileMode.Create : FileMode.CreateNew;
            string   targetFile;

            if (LocalDirectory.Exists(resolvedDestination))
            {
                // If the destination pointed to an existing directory, we
                // would download the file into the folder with the same name
                // on cloud.
                targetFile = LocalPath.Combine(resolvedDestination, fileToBeDownloaded.GetBaseName());
            }
            else
            {
                // Otherwise we treat the destination as a file no matter if
                // there's one existing or not. The overwrite behavior is configured
                // by FileMode.
                targetFile = resolvedDestination;
            }

            if (ShouldProcess(targetFile, "Download"))
            {
                this.RunTask(async taskId =>
                {
                    await
                    fileToBeDownloaded.FetchAttributesAsync(null, this.RequestOptions, OperationContext,
                                                            CmdletCancellationToken);

                    var progressRecord = new ProgressRecord(
                        this.OutputStream.GetProgressId(taskId),
                        string.Format(CultureInfo.CurrentCulture, Resources.ReceiveAzureFileActivity,
                                      fileToBeDownloaded.GetFullPath(), targetFile),
                        Resources.PrepareDownloadingFile);

                    await DataMovementTransferHelper.DoTransfer(() =>
                    {
                        return(this.TransferManager.DownloadAsync(
                                   fileToBeDownloaded,
                                   targetFile,
                                   new DownloadOptions
                        {
                            DisableContentMD5Validation = !this.CheckMd5
                        },
                                   this.GetTransferContext(progressRecord, fileToBeDownloaded.Properties.Length),
                                   CmdletCancellationToken));
                    },
                                                                progressRecord,
                                                                this.OutputStream);

                    if (this.PassThru)
                    {
                        this.OutputStream.WriteObject(taskId, fileToBeDownloaded);
                    }
                });
            }
        }
        public override void ExecuteCmdlet()
        {
            CloudFile fileToBeDownloaded;

            string[] path = NamingUtil.ValidatePath(this.Path, true);
            switch (this.ParameterSetName)
            {
            case LocalConstants.FileParameterSetName:
                fileToBeDownloaded = this.File;
                break;

            case LocalConstants.ShareNameParameterSetName:
                var share = this.BuildFileShareObjectFromName(this.ShareName);
                fileToBeDownloaded = share.GetRootDirectoryReference().GetFileReferenceByPath(path);
                break;

            case LocalConstants.ShareParameterSetName:
                fileToBeDownloaded = this.Share.GetRootDirectoryReference().GetFileReferenceByPath(path);
                break;

            case LocalConstants.DirectoryParameterSetName:
                fileToBeDownloaded = this.Directory.GetFileReferenceByPath(path);
                break;

            default:
                throw new PSArgumentException(string.Format(CultureInfo.InvariantCulture, "Invalid parameter set name: {0}", this.ParameterSetName));
            }

            string resolvedDestination = this.GetUnresolvedProviderPathFromPSPath(
                string.IsNullOrWhiteSpace(this.Destination) ? "." : this.Destination);

            FileMode mode = this.Force ? FileMode.Create : FileMode.CreateNew;
            string   targetFile;

            if (LocalDirectory.Exists(resolvedDestination))
            {
                // If the destination pointed to an existing directory, we
                // would download the file into the folder with the same name
                // on cloud.
                targetFile = LocalPath.Combine(resolvedDestination, fileToBeDownloaded.GetBaseName());
            }
            else
            {
                // Otherwise we treat the destination as a file no matter if
                // there's one existing or not. The overwrite behavior is configured
                // by FileMode.
                targetFile = resolvedDestination;
            }

            this.RunTask(async taskId =>
            {
                var downloadJob = new TransferJob(
                    new TransferLocation(fileToBeDownloaded),
                    new TransferLocation(targetFile),
                    TransferMethod.SyncCopy);

                var progressRecord = new ProgressRecord(
                    this.OutputStream.GetProgressId(taskId),
                    string.Format(CultureInfo.CurrentCulture, Resources.ReceiveAzureFileActivity, fileToBeDownloaded.GetFullPath(), targetFile),
                    Resources.PrepareDownloadingFile);

                await this.RunTransferJob(downloadJob, progressRecord);

                if (this.PassThru)
                {
                    this.OutputStream.WriteObject(taskId, fileToBeDownloaded);
                }
            });
        }