/// <summary>
 /// Initializes a new instance of the <see cref="TransferJob"/> class.
 /// </summary>
 /// <param name="other">The other transfer job to copy properties.</param>
 private TransferJob(TransferJob other)
 {
     this.Overwrite  = other.Overwrite;
     this.CopyId     = other.CopyId;
     this.CheckPoint = other.CheckPoint.Copy();
     this.Status     = other.Status;
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="TransferJob"/> class.
 /// </summary>
 private TransferJob(TransferJob other)
 {
     this.Overwrite = other.Overwrite;
     this.CopyId = other.CopyId;
     this.CheckPoint = other.CheckPoint.Copy();
     this.Status = other.Status;
 }
        public FileAsyncCopyController(
            TransferScheduler transferScheduler,
            TransferJob transferJob,
            CancellationToken cancellationToken)
            : base(transferScheduler, transferJob, cancellationToken)
        {
            if (transferJob.Destination.Type != TransferLocationType.AzureFile)
            {
                throw new ArgumentException(
                    string.Format(
                        CultureInfo.CurrentCulture,
                        Resources.ParameterCannotBeNullException,
                        "Dest.AzureFile"),
                    "transferJob");
            }

            if (transferJob.Source.Type != TransferLocationType.SourceUri &&
                transferJob.Source.Type != TransferLocationType.AzureBlob &&
                transferJob.Source.Type != TransferLocationType.AzureFile)
            {
                throw new ArgumentException(
                    string.Format(
                        CultureInfo.CurrentCulture,
                        Resources.ProvideExactlyOneOfThreeParameters,
                        "Source.SourceUri",
                        "Source.Blob",
                        "Source.AzureFile"),
                    "transferJob");
            }

            this.destLocation = this.TransferJob.Destination as AzureFileLocation;
            this.destFile = this.destLocation.AzureFile;
        }
Esempio n. 4
0
        public void UpdateTransferJobStatus(TransferJob transferJob, TransferJobStatus targetStatus)
        {
            lock (this.ProgressTracker)
            {
                switch (targetStatus)
                {
                case TransferJobStatus.Transfer:
                    if (transferJob.Status == TransferJobStatus.Failed)
                    {
                        this.ProgressTracker.AddNumberOfFilesFailed(-1);
                    }

                    break;

                case TransferJobStatus.Skipped:
                    this.ProgressTracker.AddNumberOfFilesSkipped(1);
                    break;

                case TransferJobStatus.Finished:
                    this.ProgressTracker.AddNumberOfFilesTransferred(1);
                    break;

                case TransferJobStatus.Failed:
                    this.ProgressTracker.AddNumberOfFilesFailed(1);
                    break;

                case TransferJobStatus.NotStarted:
                case TransferJobStatus.Monitor:
                default:
                    break;
                }

                transferJob.Status = targetStatus;
            }
        }
        protected async Task RunTransferJob(TransferJob job, ProgressRecord record)
        {
            this.SetRequestOptionsInTransferJob(job);
            job.OverwritePromptCallback = this.ConfirmOverwrite;

            try
            {
                await this.transferJobRunner.RunTransferJob(job,
                        (percent, speed) =>
                        {
                            record.PercentComplete = (int)percent;
                            record.StatusDescription = string.Format(CultureInfo.CurrentCulture, Resources.FileTransmitStatus, (int)percent, Util.BytesToHumanReadableSize(speed));
                            this.OutputStream.WriteProgress(record);
                        },
                        this.CmdletCancellationToken);

                record.PercentComplete = 100;
                record.StatusDescription = Resources.TransmitSuccessfully;
                this.OutputStream.WriteProgress(record);
            }
            catch (OperationCanceledException)
            {
                record.StatusDescription = Resources.TransmitCancelled;
                this.OutputStream.WriteProgress(record);
            }
            catch (Exception e)
            {
                record.StatusDescription = string.Format(CultureInfo.CurrentCulture, Resources.TransmitFailed, e.Message);
                this.OutputStream.WriteProgress(record);
                throw;
            }
        }
Esempio n. 6
0
 /// <summary>
 /// Initializes a new instance of the <see cref="SingleObjectTransfer"/> class.
 /// </summary>
 /// <param name="other">Another <see cref="SingleObjectTransfer"/> object. </param>
 private SingleObjectTransfer(SingleObjectTransfer other)
     : base(other)
 {
     this.ProgressTracker      = other.ProgressTracker.Copy();
     this.transferJob          = other.transferJob.Copy();
     this.transferJob.Transfer = this;
 }
Esempio n. 7
0
 /// <summary>
 /// Initializes a new instance of the <see cref="TransferJob"/> class.
 /// </summary>
 private TransferJob(TransferJob other)
 {
     this.Source      = other.Source;
     this.Destination = other.Destination;
     this.Overwrite   = other.Overwrite;
     this.CopyId      = other.CopyId;
     this.CheckPoint  = other.CheckPoint.Copy();
     this.Status      = other.Status;
 }
Esempio n. 8
0
        private async Task ExecuteJobInternalAsync(
            TransferJob job,
            CancellationToken cancellationToken)
        {
            Debug.Assert(
                job.Status == TransferJobStatus.NotStarted ||
                job.Status == TransferJobStatus.Monitor ||
                job.Status == TransferJobStatus.Transfer);

            TransferControllerBase controller = null;

            switch (job.Transfer.TransferMethod)
            {
            case TransferMethod.SyncCopy:
                controller = new SyncTransferController(this, job, cancellationToken);
                break;

            case TransferMethod.AsyncCopy:
                controller = AsyncCopyController.CreateAsyncCopyController(this, job, cancellationToken);
                break;
            }

            Utils.CheckCancellation(this.cancellationTokenSource.Token);
            this.controllerQueue.Add(controller, this.cancellationTokenSource.Token);

            try
            {
                await controller.TaskCompletionSource.Task;
            }
#if EXPECT_INTERNAL_WRAPPEDSTORAGEEXCEPTION
            catch (Exception ex) when(ex is StorageException || (ex is AggregateException && ex.InnerException is StorageException))
            {
                var storageException = ex as StorageException ?? ex.InnerException as StorageException;

                if (storageException.InnerException is OperationCanceledException)
                {
                    throw storageException.InnerException;
                }

                throw new TransferException(TransferErrorCode.Unknown,
                                            Resources.UncategorizedException,
                                            storageException);
            }
#else
            catch (StorageException se)
            {
                throw new TransferException(
                          TransferErrorCode.Unknown,
                          Resources.UncategorizedException,
                          se);
            }
#endif
            finally
            {
                controller.Dispose();
            }
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="TransferJob"/> class.
 /// </summary>
 private TransferJob(TransferJob other)
 {
     this.Source = other.Source;
     this.Destination = other.Destination;
     this.Overwrite = other.Overwrite;
     this.CopyId = other.CopyId;
     this.CheckPoint = other.CheckPoint.Copy();
     this.Status = other.Status;
 }
 public Task RunTransferJob(TransferJob job, Action<double, double> progressReport, CancellationToken cancellationToken)
 {
     try
     {
         return runnerValidation(job);
     }
     catch (AssertFailedException e)
     {
         this.assertException = e;
         throw new MockupException("AssertFailed");
     }
 }
 private static void UpdateProgress(TransferJob job, Action updateAction)
 {
     try
     {
         job.ProgressUpdateLock?.EnterReadLock();
         updateAction();
     }
     finally
     {
         job.ProgressUpdateLock?.ExitReadLock();
     }
 }
Esempio n. 12
0
        /// <summary>
        /// Execute a transfer job asynchronously.
        /// </summary>
        /// <param name="job">Transfer job to be executed.</param>
        /// <param name="cancellationToken">Token used to notify the job that it should stop.</param>
        public Task ExecuteJobAsync(
            TransferJob job,
            CancellationToken cancellationToken)
        {
            if (null == job)
            {
                throw new ArgumentNullException("job");
            }

            lock (this.disposeLock)
            {
                this.CheckDisposed();

                return(ExecuteJobInternalAsync(job, cancellationToken));
            }
        }
        public Task RunTransferJob(TransferJob job, Action<double, double> progressReport, CancellationToken cancellationToken)
        {
            TaskCompletionSource<object> downloadCompletionSource = new TaskCompletionSource<object>();

            job.Starting += (sender, eventArgs) =>
            {
                if (cancellationToken.IsCancellationRequested)
                {
                    return;
                }

                progressReport(0, 0);
            };

            job.ProgressUpdated += (sender, eventArgs) =>
            {
                if (cancellationToken.IsCancellationRequested)
                {
                    return;
                }

                progressReport(eventArgs.Progress, eventArgs.Speed);
            };

            job.Finished += (sender, eventArgs) =>
            {
                if (cancellationToken.IsCancellationRequested)
                {
                    return;
                }

                if (eventArgs.Exception == null)
                {
                    downloadCompletionSource.SetResult(null);
                }
                else
                {
                    downloadCompletionSource.SetException(eventArgs.Exception);
                }
            };

            this.manager.ExecuteJobAsync(job, cancellationToken);

            return downloadCompletionSource.Task;
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="SingleObjectTransfer"/> class.
        /// This constructor will check whether source and destination is valid for the operation:
        /// Uri is only valid for non-staging copy.
        /// cannot copy from local file/stream to local file/stream
        /// </summary>
        /// <param name="source">Transfer source.</param>
        /// <param name="dest">Transfer destination.</param>
        /// <param name="transferMethod">Transfer method, see <see cref="TransferMethod"/> for detail available methods.</param>
        public SingleObjectTransfer(TransferLocation source, TransferLocation dest, TransferMethod transferMethod)
            : base(source, dest, transferMethod)
        {
            if (null == source)
            {
                throw new ArgumentNullException("source");
            }

            if (null == dest)
            {
                throw new ArgumentNullException("dest");
            }

            if ((null != source.FilePath || null != source.Stream) &&
                (null != dest.FilePath || null != dest.Stream))
            {
                throw new InvalidOperationException(Resources.LocalToLocalTransferUnsupportedException);
            }

            if ((null != source.Blob) &&
                (null != dest.Blob))
            {
                if (source.Blob.BlobType != dest.Blob.BlobType)
                {
                    throw new InvalidOperationException(Resources.SourceAndDestinationBlobTypeDifferent);
                }

                if (StorageExtensions.Equals(source.Blob, dest.Blob))
                {
                    throw new InvalidOperationException(Resources.SourceAndDestinationLocationCannotBeEqualException);
                }
            }

            if ((null != source.AzureFile) &&
                (null != dest.AzureFile) &&
                string.Equals(source.AzureFile.Uri.Host, dest.AzureFile.Uri.Host, StringComparison.OrdinalIgnoreCase) &&
                string.Equals(source.AzureFile.Uri.AbsolutePath, dest.AzureFile.Uri.AbsolutePath, StringComparison.OrdinalIgnoreCase))
            {
                throw new InvalidOperationException(Resources.SourceAndDestinationLocationCannotBeEqualException);
            }

            this.transferJob          = new TransferJob(this.Source, this.Destination);
            this.transferJob.Transfer = this;
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="SingleObjectTransfer"/> class.
        /// This constructor will check whether source and destination is valid for the operation:
        /// Uri is only valid for non-staging copy. 
        /// cannot copy from local file/stream to local file/stream 
        /// </summary>
        /// <param name="source">Transfer source.</param>
        /// <param name="dest">Transfer destination.</param>
        /// <param name="transferMethod">Transfer method, see <see cref="TransferMethod"/> for detail available methods.</param>
        public SingleObjectTransfer(TransferLocation source, TransferLocation dest, TransferMethod transferMethod)
            : base(source, dest, transferMethod)
        {
            if (null == source)
            {
                throw new ArgumentNullException("source");
            }

            if (null == dest)
            {
                throw new ArgumentNullException("dest");
            }

            if ((null != source.FilePath || null != source.Stream)
                && (null != dest.FilePath || null != dest.Stream))
            {
                throw new InvalidOperationException(Resources.LocalToLocalTransferUnsupportedException);
            }

            if ((null != source.Blob)
                && (null != dest.Blob))
            {
                if (source.Blob.BlobType != dest.Blob.BlobType)
                {
                    throw new InvalidOperationException(Resources.SourceAndDestinationBlobTypeDifferent);
                }

                if (StorageExtensions.Equals(source.Blob, dest.Blob))
                {
                    throw new InvalidOperationException(Resources.SourceAndDestinationLocationCannotBeEqualException);
                }
            }

            if ((null != source.AzureFile)
                && (null != dest.AzureFile)
                && string.Equals(source.AzureFile.Uri.Host, dest.AzureFile.Uri.Host, StringComparison.OrdinalIgnoreCase)
                && string.Equals(source.AzureFile.Uri.AbsolutePath, dest.AzureFile.Uri.AbsolutePath, StringComparison.OrdinalIgnoreCase))
            {
                throw new InvalidOperationException(Resources.SourceAndDestinationLocationCannotBeEqualException);
            }

            this.transferJob = new TransferJob(this.Source, this.Destination);
            this.transferJob.Transfer = this;
        }
Esempio n. 16
0
        /// <summary>
        /// Initializes a new instance of the <see cref="SingleObjectTransfer"/> class.
        /// This constructor will check whether source and destination is valid for the operation:
        /// Uri is only valid for non-staging copy.
        /// cannot copy from local file/stream to local file/stream
        /// </summary>
        /// <param name="source">Transfer source.</param>
        /// <param name="dest">Transfer destination.</param>
        /// <param name="transferMethod">Transfer method, see <see cref="TransferMethod"/> for detail available methods.</param>
        public SingleObjectTransfer(TransferLocation source, TransferLocation dest, TransferMethod transferMethod)
            : base(source, dest, transferMethod)
        {
            Debug.Assert(source != null && dest != null);
            Debug.Assert(
                source.Type == TransferLocationType.FilePath || source.Type == TransferLocationType.Stream ||
                source.Type == TransferLocationType.AzureBlob || source.Type == TransferLocationType.AzureFile ||
                source.Type == TransferLocationType.SourceUri);
            Debug.Assert(
                dest.Type == TransferLocationType.FilePath || dest.Type == TransferLocationType.Stream ||
                dest.Type == TransferLocationType.AzureBlob || dest.Type == TransferLocationType.AzureFile ||
                dest.Type == TransferLocationType.SourceUri);
            Debug.Assert(!((source.Type == TransferLocationType.FilePath || source.Type == TransferLocationType.Stream) &&
                           (dest.Type == TransferLocationType.FilePath || dest.Type == TransferLocationType.Stream)));

            if (source.Type == TransferLocationType.AzureBlob && dest.Type == TransferLocationType.AzureBlob)
            {
                CloudBlob sourceBlob = (source as AzureBlobLocation).Blob;
                CloudBlob destBlob   = (dest as AzureBlobLocation).Blob;
                if (sourceBlob.BlobType != destBlob.BlobType)
                {
                    throw new InvalidOperationException(Resources.SourceAndDestinationBlobTypeDifferent);
                }

                if (StorageExtensions.Equals(sourceBlob, destBlob))
                {
                    throw new InvalidOperationException(Resources.SourceAndDestinationLocationCannotBeEqualException);
                }
            }

            if (source.Type == TransferLocationType.AzureFile && dest.Type == TransferLocationType.AzureFile)
            {
                CloudFile sourceFile = (source as AzureFileLocation).AzureFile;
                CloudFile destFile   = (dest as AzureFileLocation).AzureFile;
                if (string.Equals(sourceFile.Uri.Host, destFile.Uri.Host, StringComparison.OrdinalIgnoreCase) &&
                    string.Equals(sourceFile.Uri.AbsolutePath, destFile.Uri.AbsolutePath, StringComparison.OrdinalIgnoreCase))
                {
                    throw new InvalidOperationException(Resources.SourceAndDestinationLocationCannotBeEqualException);
                }
            }

            this.transferJob = new TransferJob(this);
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="SingleObjectTransfer"/> class.
        /// This constructor will check whether source and destination is valid for the operation:
        /// Uri is only valid for non-staging copy. 
        /// cannot copy from local file/stream to local file/stream 
        /// </summary>
        /// <param name="source">Transfer source.</param>
        /// <param name="dest">Transfer destination.</param>
        /// <param name="transferMethod">Transfer method, see <see cref="TransferMethod"/> for detail available methods.</param>
        public SingleObjectTransfer(TransferLocation source, TransferLocation dest, TransferMethod transferMethod)
            : base(source, dest, transferMethod)
        {
            Debug.Assert(source != null && dest != null);
            Debug.Assert(
                source.Type == TransferLocationType.FilePath || source.Type == TransferLocationType.Stream ||
                source.Type == TransferLocationType.AzureBlob || source.Type == TransferLocationType.AzureFile ||
                source.Type == TransferLocationType.SourceUri);
            Debug.Assert(
                dest.Type == TransferLocationType.FilePath || dest.Type == TransferLocationType.Stream ||
                dest.Type == TransferLocationType.AzureBlob || dest.Type == TransferLocationType.AzureFile ||
                dest.Type == TransferLocationType.SourceUri);
            Debug.Assert(!((source.Type == TransferLocationType.FilePath || source.Type == TransferLocationType.Stream) &&
                (dest.Type == TransferLocationType.FilePath || dest.Type == TransferLocationType.Stream)));

            if (source.Type == TransferLocationType.AzureBlob && dest.Type == TransferLocationType.AzureBlob)
            {
                CloudBlob sourceBlob = (source as AzureBlobLocation).Blob;
                CloudBlob destBlob = (dest as AzureBlobLocation).Blob;
                if (sourceBlob.BlobType != destBlob.BlobType)
                {
                    throw new InvalidOperationException(Resources.SourceAndDestinationBlobTypeDifferent);
                }

                if (StorageExtensions.Equals(sourceBlob, destBlob))
                {
                    throw new InvalidOperationException(Resources.SourceAndDestinationLocationCannotBeEqualException);
                }
            }

            if (source.Type == TransferLocationType.AzureFile && dest.Type == TransferLocationType.AzureFile)
            {
                CloudFile sourceFile = (source as AzureFileLocation).AzureFile;
                CloudFile destFile = (dest as AzureFileLocation).AzureFile;
                if (string.Equals(sourceFile.Uri.Host, destFile.Uri.Host, StringComparison.OrdinalIgnoreCase) &&
                    string.Equals(sourceFile.Uri.AbsolutePath, destFile.Uri.AbsolutePath, StringComparison.OrdinalIgnoreCase))
                {
                    throw new InvalidOperationException(Resources.SourceAndDestinationLocationCannotBeEqualException);
                }
            }

            this.transferJob = new TransferJob(this);
        }
        protected TransferControllerBase(TransferScheduler transferScheduler, TransferJob transferJob, CancellationToken userCancellationToken)
        {
            if (null == transferScheduler)
            {
                throw new ArgumentNullException("transferScheduler");
            }

            if (null == transferJob)
            {
                throw new ArgumentNullException("transferJob");
            }

            this.Scheduler = transferScheduler;
            this.TransferJob = transferJob;

            this.transferSchedulerCancellationTokenRegistration =
                this.Scheduler.CancellationTokenSource.Token.Register(this.CancelWork);

            this.userCancellationTokenRegistration = userCancellationToken.Register(this.CancelWork);
            this.TaskCompletionSource = new TaskCompletionSource<object>();
        }
        public BlobAsyncCopyController(
            TransferScheduler transferScheduler,
            TransferJob transferJob,
            CancellationToken cancellationToken)
            : base(transferScheduler, transferJob, cancellationToken)
        {
            this.destLocation = transferJob.Destination as AzureBlobLocation;
            CloudBlob transferDestBlob = this.destLocation.Blob;
            if (null == transferDestBlob)
            {
                throw new ArgumentException(
                    string.Format(
                        CultureInfo.CurrentCulture,
                        Resources.ParameterCannotBeNullException,
                        "Dest.Blob"),
                    "transferJob");
            }

            if (transferDestBlob.IsSnapshot)
            {
                throw new ArgumentException(Resources.DestinationMustBeBaseBlob, "transferJob");
            }

            AzureBlobLocation sourceBlobLocation = transferJob.Source as AzureBlobLocation;
            if (sourceBlobLocation != null)
            {
                if (sourceBlobLocation.Blob.BlobType != transferDestBlob.BlobType)
                {
                    throw new ArgumentException(Resources.SourceAndDestinationBlobTypeDifferent, "transferJob");
                }

                if (StorageExtensions.Equals(sourceBlobLocation.Blob, transferDestBlob))
                {
                    throw new InvalidOperationException(Resources.SourceAndDestinationLocationCannotBeEqualException);
                }
            }

            this.destBlob = transferDestBlob;
        }
Esempio n. 20
0
        private async Task ExecuteJobInternalAsync(
            TransferJob job,
            CancellationToken cancellationToken)
        {
            Debug.Assert(
                job.Status == TransferJobStatus.NotStarted ||
                job.Status == TransferJobStatus.Monitor ||
                job.Status == TransferJobStatus.Transfer);

            TransferControllerBase controller = null;

            switch (job.Transfer.TransferMethod)
            {
            case TransferMethod.SyncCopy:
                controller = new SyncTransferController(this, job, cancellationToken);
                break;

            case TransferMethod.AsyncCopy:
                controller = AsyncCopyController.CreateAsyncCopyController(this, job, cancellationToken);
                break;
            }

            Utils.CheckCancellation(this.cancellationTokenSource);
            this.controllerQueue.Add(controller, this.cancellationTokenSource.Token);

            try
            {
                await controller.TaskCompletionSource.Task;
            }
            catch (StorageException sex)
            {
                throw new TransferException(TransferErrorCode.Unknown, Resources.UncategorizedException, sex);
            }
            finally
            {
                controller.Dispose();
            }
        }
        private async Task ExecuteJobInternalAsync(
            TransferJob job,
            CancellationToken cancellationToken)
        {
            Debug.Assert(
                job.Status == TransferJobStatus.NotStarted ||
                job.Status == TransferJobStatus.Monitor ||
                job.Status == TransferJobStatus.Transfer);

            TransferControllerBase controller = null;
            switch (job.Transfer.TransferMethod)
            {
                case TransferMethod.SyncCopy:
                    controller = new SyncTransferController(this, job, cancellationToken);
                    break;

                case TransferMethod.AsyncCopy:
                    controller = AsyncCopyController.CreateAsyncCopyController(this, job, cancellationToken);
                    break;
            }

            Utils.CheckCancellation(this.cancellationTokenSource);
            this.controllerQueue.Add(controller, this.cancellationTokenSource.Token);

            try
            {
                await controller.TaskCompletionSource.Task;
            }
            catch(StorageException sex)
            {
                throw new TransferException(TransferErrorCode.Unknown, Resources.UncategorizedException, sex);
            }
            finally
            {
                controller.Dispose();
            }
        }
        /// <summary>
        /// Execute a transfer job asynchronously.
        /// </summary>
        /// <param name="job">Transfer job to be executed.</param>
        /// <param name="cancellationToken">Token used to notify the job that it should stop.</param>
        public Task ExecuteJobAsync(
            TransferJob job,
            CancellationToken cancellationToken)
        {
            if (null == job)
            {
                throw new ArgumentNullException("job");
            }

            lock (this.disposeLock)
            {
                this.CheckDisposed();

                return ExecuteJobInternalAsync(job, cancellationToken);
            }
        }
        /// <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
            };

            TransferJob downloadJob = new TransferJob(
                new TransferLocation(blob),
                new TransferLocation(filePath),
                TransferMethod.SyncCopy);

            BlobRequestOptions requestOptions = downloadJob.Source.RequestOptions as BlobRequestOptions;

            if (null == requestOptions)
            {
                requestOptions = new BlobRequestOptions();
            }

            requestOptions.DisableContentMD5Validation = !checkMd5;
            downloadJob.Source.RequestOptions = requestOptions;

            await this.RunTransferJob(downloadJob, data);

            this.WriteCloudBlobObject(data.TaskId, data.Channel, blob);
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="SingleObjectTransfer"/> class.
 /// </summary>
 /// <param name="info">Serialization information.</param>
 /// <param name="context">Streaming context.</param>
 protected SingleObjectTransfer(SerializationInfo info, StreamingContext context)
     : base(info, context)
 {
     this.transferJob = (TransferJob)info.GetValue(TransferJobName, typeof(TransferJob));
     this.transferJob.Transfer = this;
 }
        protected void SetRequestOptionsInTransferJob(TransferJob transferJob)
        {
            var cmdletOptions = this.RequestOptions;

            if (cmdletOptions == null)
            {
                return;
            }

            if (null != transferJob.Source.AzureFile)
            {
                this.SetRequestOptions(transferJob.Source, cmdletOptions);
            }

            if (null != transferJob.Destination.AzureFile)
            {
                this.SetRequestOptions(transferJob.Destination, cmdletOptions);
            }
        }
        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 uploadJob = new TransferJob(
                    new TransferLocation(localFile.FullName),
                    new TransferLocation(cloudFileToBeUploaded),
                    TransferMethod.SyncCopy);

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

                await this.RunTransferJob(uploadJob, progressRecord);

                if (this.PassThru)
                {
                    this.OutputStream.WriteObject(taskId, cloudFileToBeUploaded);
                }
            });
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="SingleObjectTransfer"/> class.
 /// </summary>
 /// <param name="other">Another <see cref="SingleObjectTransfer"/> object. </param>
 private SingleObjectTransfer(SingleObjectTransfer other)
     : base(other)
 {
     this.transferJob = other.transferJob.Copy();
     this.transferJob.Transfer = this;
 }
Esempio n. 28
0
 /// <summary>
 /// Initializes a new instance of the <see cref="SingleObjectTransfer"/> class.
 /// </summary>
 /// <param name="info">Serialization information.</param>
 /// <param name="context">Streaming context.</param>
 protected SingleObjectTransfer(SerializationInfo info, StreamingContext context)
     : base(info, context)
 {
     this.transferJob          = (TransferJob)info.GetValue(TransferJobName, typeof(TransferJob));
     this.transferJob.Transfer = this;
 }
        protected void SetRequestOptionsInTransferJob(TransferJob transferJob)
        {
            BlobRequestOptions cmdletOptions = RequestOptions;

            if (cmdletOptions == null)
            {
                return;
            }

            if (null != transferJob.Source.Blob)
            {
                this.SetRequestOptions(transferJob.Source, cmdletOptions);
            }

            if (null != transferJob.Destination.Blob)
            {
                this.SetRequestOptions(transferJob.Destination, cmdletOptions);
            }
        }
        public void UpdateTransferJobStatus(TransferJob transferJob, TransferJobStatus targetStatus)
        {
            lock (this.ProgressTracker)
            {
                switch (targetStatus)
                {
                    case TransferJobStatus.Transfer:
                    case TransferJobStatus.Monitor:
                        if (transferJob.Status == TransferJobStatus.Failed)
                        {
                            this.ProgressTracker.AddNumberOfFilesFailed(-1);
                        }

                        break;

                    case TransferJobStatus.Skipped:
                        this.ProgressTracker.AddNumberOfFilesSkipped(1);
                        break;

                    case TransferJobStatus.Finished:
                        this.ProgressTracker.AddNumberOfFilesTransferred(1);
                        break;

                    case TransferJobStatus.Failed:
                        this.ProgressTracker.AddNumberOfFilesFailed(1);
                        break;

                    case TransferJobStatus.NotStarted:
                    
                    default:
                        break;
                }

                transferJob.Status = targetStatus;
            }
        }
        protected async Task EnqueueStartCopyJob(TransferJob startCopyJob, DataMovementUserData userData)
        {
            await this.RunTransferJob(startCopyJob, userData);

            this.OutputStream.WriteVerbose(userData.TaskId, startCopyJob.CopyId);
            Dictionary<string, string> destBlobPath = userData.Data as Dictionary<string, string>;

            if (destBlobPath != null)
            {
                var destChannel = userData.Channel;
                this.OutputStream.WriteVerbose(userData.TaskId, String.Format(Resources.CopyDestinationBlobPending, destBlobPath["Blob"], destBlobPath["Container"], startCopyJob.CopyId));
                CloudBlobContainer container = destChannel.GetContainerReference(destBlobPath["Container"]);
                CloudBlob destBlob = this.GetDestinationBlobWithCopyId(destChannel, container, destBlobPath["Blob"]);
                if (destBlob != null)
                {
                    this.WriteCloudBlobObject(userData.TaskId, destChannel, destBlob);
                }
            }
        }
        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);
                }
            });
        }
        /// <summary>
        /// Start copy using transfer mangager by source uri
        /// </summary>
        /// <param name="uri">source uri</param>
        /// <param name="destContainer">Destination CloudBlobContainer object</param>
        /// <param name="destBlobName">Destination blob name</param>
        /// <returns>Destination CloudBlob object</returns>
        private async Task StartCopyInTransferManager(long taskId, IStorageBlobManagement destChannel, Uri uri, CloudBlobContainer destContainer, string destBlobName)
        {
            NameUtil.ValidateContainerName(destContainer.Name);
            NameUtil.ValidateBlobName(destBlobName);
            Dictionary<string, string> BlobPath = new Dictionary<string, string>()
            {
                {"Container", destContainer.Name},
                {"Blob", destBlobName}
            };

            DataMovementUserData data = new DataMovementUserData()
            {
                Data = BlobPath,
                TaskId = taskId,
                Channel = destChannel,
                Record = null
            };

            TransferJob startCopyJob = new TransferJob(
                new TransferLocation(uri), 
                new TransferLocation(destContainer.GetBlockBlobReference(destBlobName)),
                TransferMethod.AsyncCopyInAzureStorageWithoutMonitor);

            await this.EnqueueStartCopyJob(startCopyJob, data);
        }
        /// <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);

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

            TransferJob uploadJob = 
                new TransferJob(
                    new TransferLocation(filePath),
                    new TransferLocation(blob),
                    TransferMethod.SyncCopy);

            await this.RunTransferJob(uploadJob, data);

            if (this.BlobProperties != null || this.BlobMetadata != null)
            {
                await TaskEx.WhenAll(
                    this.SetBlobProperties(localChannel, blob, this.BlobProperties),
                    this.SetBlobMeta(localChannel, blob, this.BlobMetadata));
            }

            try
            {
                await localChannel.FetchBlobAttributesAsync(
                    blob,
                    AccessCondition.GenerateEmptyCondition(),
                    this.RequestOptions,
                    this.OperationContext,
                    this.CmdletCancellationToken);
            }
            catch (StorageException e)
            {
                //Handle the limited read permission.
                if (!e.IsNotFoundException())
                {
                    throw;
                }
            }

            WriteCloudBlobObject(data.TaskId, localChannel, blob);
        }