/// <summary>
        /// Download blob to local file
        /// </summary>
        /// <param name="blob">Source blob object</param>
        /// <param name="filePath">Destination file path</param>
        internal virtual async Task DownloadBlob(long taskId, IStorageBlobManagement localChannel, CloudBlob blob, string filePath)
        {
            string               activity = String.Format(Resources.ReceiveAzureBlobActivity, blob.Name, filePath);
            string               status   = Resources.PrepareDownloadingBlob;
            ProgressRecord       pr       = new ProgressRecord(OutputStream.GetProgressId(taskId), activity, status);
            DataMovementUserData data     = new DataMovementUserData()
            {
                Data      = blob,
                TaskId    = taskId,
                Channel   = localChannel,
                Record    = pr,
                TotalSize = blob.Properties.Length
            };

            await DataMovementTransferHelper.DoTransfer(() =>
            {
                return(this.TransferManager.DownloadAsync(blob, filePath,
                                                          new DownloadOptions()
                {
                    DisableContentMD5Validation = !this.checkMd5
                },
                                                          this.GetTransferContext(data),
                                                          this.CmdletCancellationToken));
            },
                                                        data.Record,
                                                        this.OutputStream);

            this.WriteCloudBlobObject(data.TaskId, data.Channel, blob);
        }
        /// <summary>
        /// upload file to azure blob
        /// </summary>
        /// <param name="taskId">Task id</param>
        /// <param name="filePath">local file path</param>
        /// <param name="blob">destination azure blob object</param>
        internal virtual async Task Upload2Blob(long taskId, IStorageBlobManagement localChannel, string filePath, StorageBlob.CloudBlob blob)
        {
            string         activity = String.Format(Resources.SendAzureBlobActivity, filePath, blob.Name, blob.Container.Name);
            string         status   = Resources.PrepareUploadingBlob;
            ProgressRecord pr       = new ProgressRecord(OutputStream.GetProgressId(taskId), activity, status);

            FileInfo fileInfo = new FileInfo(filePath);

            DataMovementUserData data = new DataMovementUserData()
            {
                Data      = blob,
                TaskId    = taskId,
                Channel   = localChannel,
                Record    = pr,
                TotalSize = fileInfo.Length
            };

            await DataMovementTransferHelper.DoTransfer(() =>
            {
                return(this.TransferManager.UploadAsync(filePath,
                                                        blob,
                                                        null,
                                                        this.GetTransferContext(data),
                                                        this.CmdletCancellationToken));
            },
                                                        data.Record,
                                                        this.OutputStream).ConfigureAwait(false);

            if (this.BlobProperties != null || this.BlobMetadata != null || this.pageBlobTier != null)
            {
                await Task.WhenAll(
                    this.SetBlobProperties(localChannel, blob, this.BlobProperties),
                    this.SetBlobMeta(localChannel, blob, this.BlobMetadata),
                    this.SetBlobTier(localChannel, blob, pageBlobTier)).ConfigureAwait(false);
            }

            try
            {
                await localChannel.FetchBlobAttributesAsync(
                    blob,
                    AccessCondition.GenerateEmptyCondition(),
                    this.RequestOptions,
                    this.OperationContext,
                    this.CmdletCancellationToken).ConfigureAwait(false);
            }
            catch (StorageException e)
            {
                //Handle the limited read permission, and handle the upload with write only permission
                if (!e.IsNotFoundException() && !e.IsForbiddenException())
                {
                    throw;
                }
            }

            WriteCloudBlobObject(data.TaskId, localChannel, blob);
        }
        public override void ExecuteCmdlet()
        {
            // Step 1: Validate source file.
            FileInfo localFile = new FileInfo(this.GetUnresolvedProviderPathFromPSPath(this.Source));

            if (!localFile.Exists)
            {
                throw new FileNotFoundException(string.Format(CultureInfo.CurrentCulture, Resources.SourceFileNotFound, this.Source));
            }

            // if FIPS policy is enabled, must use native MD5
            if (fipsEnabled)
            {
                CloudStorageAccount.UseV1MD5 = false;
            }

            bool isDirectory;

            string[] path = NamingUtil.ValidatePath(this.Path, out isDirectory);
            var      cloudFileToBeUploaded =
                BuildCloudFileInstanceFromPathAsync(localFile.Name, path, isDirectory).ConfigureAwait(false).GetAwaiter().GetResult();

            if (ShouldProcess(cloudFileToBeUploaded.Name, "Set file content"))
            {
                // Step 2: Build the CloudFile object which pointed to the
                // destination cloud file.
                this.RunTask(async taskId =>
                {
                    var progressRecord = new ProgressRecord(
                        this.OutputStream.GetProgressId(taskId),
                        string.Format(CultureInfo.CurrentCulture, Resources.SendAzureFileActivity, localFile.Name,
                                      cloudFileToBeUploaded.GetFullPath(), cloudFileToBeUploaded.Share.Name),
                        Resources.PrepareUploadingFile);

                    await DataMovementTransferHelper.DoTransfer(() =>
                                                                this.TransferManager.UploadAsync(
                                                                    localFile.FullName,
                                                                    cloudFileToBeUploaded,
                                                                    null,
                                                                    this.GetTransferContext(progressRecord, localFile.Length),
                                                                    this.CmdletCancellationToken),
                                                                progressRecord,
                                                                this.OutputStream).ConfigureAwait(false);


                    if (this.PassThru)
                    {
                        this.OutputStream.WriteObject(taskId, cloudFileToBeUploaded);
                    }
                });
            }
        }
        /// <summary>
        /// upload file to azure blob
        /// </summary>
        /// <param name="taskId">Task id</param>
        /// <param name="filePath">local file path</param>
        /// <param name="blob">destination azure blob object</param>
        internal virtual async Task Upload2Blob(long taskId, IStorageBlobManagement localChannel, string filePath, CloudBlob blob)
        {
            string         activity = String.Format(Resources.SendAzureBlobActivity, filePath, blob.Name, blob.Container.Name);
            string         status   = Resources.PrepareUploadingBlob;
            ProgressRecord pr       = new ProgressRecord(OutputStream.GetProgressId(taskId), activity, status);

            FileInfo fileInfo = new FileInfo(filePath);

            DataMovementUserData data = new DataMovementUserData()
            {
                Data      = blob,
                TaskId    = taskId,
                Channel   = localChannel,
                Record    = pr,
                TotalSize = fileInfo.Length
            };

            SingleTransferContext transferContext = this.GetTransferContext(data);

#pragma warning disable CS1998 // Async method lacks 'await' operators and will run synchronously
            transferContext.SetAttributesCallbackAsync = async(source, destination) =>
#pragma warning restore CS1998 // Async method lacks 'await' operators and will run synchronously
            {
                CloudBlob destBlob = destination as CloudBlob;
                SetAzureBlobContentCommand.SetBlobProperties(destBlob, this.BlobProperties);
                SetAzureBlobContentCommand.SetBlobMeta(destBlob, this.BlobMetadata);
            };

            await DataMovementTransferHelper.DoTransfer(() =>
            {
                return(this.TransferManager.UploadAsync(filePath,
                                                        blob,
                                                        null,
                                                        transferContext,
                                                        this.CmdletCancellationToken));
            },
                                                        data.Record,
                                                        this.OutputStream).ConfigureAwait(false);

            // Set blob permission with umask, since create Blob API still not support them
            SetBlobPermissionWithUMask((CloudBlockBlob)blob, this.Permission, this.Umask);

            WriteDataLakeGen2Item(localChannel, fileSystem.GetFileClient(blob.Name), taskId: data.TaskId);
        }
예제 #5
0
        public override void ExecuteCmdlet()
        {
            // Step 1: Validate source file.
            FileInfo localFile = new FileInfo(this.GetUnresolvedProviderPathFromPSPath(this.Source));

            if (!localFile.Exists)
            {
                throw new FileNotFoundException(string.Format(CultureInfo.CurrentCulture, Resources.SourceFileNotFound, this.Source));
            }

            // Step 2: Build the CloudFile object which pointed to the
            // destination cloud file.
            this.RunTask(async taskId =>
            {
                bool isDirectory;
                string[] path             = NamingUtil.ValidatePath(this.Path, out isDirectory);
                var cloudFileToBeUploaded = await this.BuildCloudFileInstanceFromPathAsync(localFile.Name, path, isDirectory);

                var progressRecord = new ProgressRecord(
                    this.OutputStream.GetProgressId(taskId),
                    string.Format(CultureInfo.CurrentCulture, Resources.SendAzureFileActivity, localFile.Name, cloudFileToBeUploaded.GetFullPath(), cloudFileToBeUploaded.Share.Name),
                    Resources.PrepareUploadingFile);

                await DataMovementTransferHelper.DoTransfer(() =>
                {
                    return(this.TransferManager.UploadAsync(
                               localFile.FullName,
                               cloudFileToBeUploaded,
                               null,
                               this.GetTransferContext(progressRecord, localFile.Length),
                               this.CmdletCancellationToken));
                },
                                                            progressRecord,
                                                            this.OutputStream);


                if (this.PassThru)
                {
                    this.OutputStream.WriteObject(taskId, cloudFileToBeUploaded);
                }
            });
        }
예제 #6
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);
                    }
                });
            }
        }