Beispiel #1
0
        /// <see cref="IComponentUpdateChecker.DownloadLatestAsync"/>
        public virtual async Task DownloadLatestAsync(IProgress <DownloadProgressChangedEventArgs> progress, CancellationToken cancellationToken)
        {
            if (LocalLocation.Exists)
            {
                // Make a backup in case the new version has issues.
                var backupFile = new FileInfo(String.Format("{0}_{1:yyyyMMdd_HHmmss}.bak", LocalLocation.FullName, _clock.Now));
                await LocalLocation.CopyToAsync(backupFile, true, cancellationToken).ConfigureAwait(false);

                LocalLocation.Delete();
            }

            var response = await HttpClient.GetAsync(DownloadLocation, cancellationToken).ConfigureAwait(false);

            using (var source = await response.Content.ReadAsStreamAsync().ConfigureAwait(false))
                using (var destination = LocalLocation.Open(FileMode.OpenOrCreate))
                {
                    await source.CopyToAsync(destination).ConfigureAwait(false);
                }
        }
        public void Location_should_be_wrapped_inside_quote_when_transfer_to_string()
        {
            var localLocation = new LocalLocation()
            {
                Path        = "location with space",
                UseWildCard = true,
            };

            Assert.Equal("\"location with space*\"", localLocation.ToString());

            var remoteLocation = new RemoteSasLocation()
            {
                SasToken    = "sastoken",
                Container   = "container",
                Path        = "path with space",
                ResourceUri = "remote/uri",
                UseWildCard = false,
            };

            Assert.Equal("\"remote/uri/container/path with spacesastoken\"", remoteLocation.ToString());
        }
        public async Task Upload_local_file_to_remote_dir_with_dir_name_include_space_async()
        {
            var hasInitMessage     = false;
            var hasInfoMessage     = false;
            var hasProgressMessage = false;
            var hasEndOfJobMessage = false;
            var jobCompleted       = false;
            var copyOption         = new CopyOption();
            var fileName           = this.GetRandomFileName();
            var localFile          = new LocalLocation()
            {
                UseWildCard = false,
            };

            // create random file
            using (var fs = new FileStream(fileName, FileMode.Create, FileAccess.Write))
            {
                fs.SetLength(1024 * 1024); // 1MB
                await fs.FlushAsync();

                localFile.Path = fileName;
            }

            var remoteLocation = new RemoteSasLocation()
            {
                ResourceUri = this.resourceUri,
                Container   = this.container,
                Path        = "directory with space/",
                SasToken    = this.sasToken,
            };

            var client = new AZCopyClient();

            client.OutputMessageHandler += (object sender, JsonOutputTemplate e) =>
            {
                this.output.WriteLine(e.MessageContent);
                if (e.MessageType == MessageType.Info)
                {
                    hasInfoMessage = true;
                }

                if (e.MessageType == MessageType.Init)
                {
                    hasInitMessage = true;
                }

                if (e.MessageType == MessageType.Progress)
                {
                    hasProgressMessage = true;
                }

                if (e.MessageType == MessageType.EndOfJob)
                {
                    hasEndOfJobMessage = true;
                }
            };

            client.JobStatusMessageHandler += (object sender, ListJobSummaryResponse e) =>
            {
                jobCompleted = e.JobStatus == JobStatus.Completed;
            };

            await client.CopyAsync(localFile, remoteLocation, copyOption);

            var deleteOption = new RemoveOption();
            await client.RemoveAsync(remoteLocation, deleteOption);

            Assert.True(hasInitMessage);
            Assert.True(hasProgressMessage);
            Assert.True(hasEndOfJobMessage);
            Assert.True(jobCompleted);
        }
        public async Task TestUploadAndDeleteExistingLocalFile()
        {
            var isSkip    = false;
            var localFile = new LocalLocation()
            {
                UseWildCard = false,
            };

            var fileName = this.GetRandomFileName();

            var sasLocation = new RemoteSasLocation()
            {
                ResourceUri = this.resourceUri,
                Container   = this.container,
                Path        = fileName,
                SasToken    = this.sasToken,
            };

            var option = new CopyOption();

            option.Overwrite = "ifSourceNewer";
            option.CapMbps   = 4;

            var client = new AZCopyClient();

            client.OutputMessageHandler += (object sender, JsonOutputTemplate e) =>
            {
                this.output.WriteLine(e.MessageContent);
            };

            client.JobStatusMessageHandler += (object sender, ListJobSummaryResponse e) =>
            {
                if (e.JobStatus == JobStatus.CompletedWithSkipped)
                {
                    isSkip = true;
                }
            };

            // create random file
            using (var fs = new FileStream(fileName, FileMode.Create, FileAccess.Write))
            {
                fs.SetLength(1024 * 1024); // 1MB
                await fs.FlushAsync();

                localFile.Path = fileName;
            }

            await client.CopyAsync(localFile, sasLocation, option);

            await Task.Delay(3 * 1000); // delay 3 s

            // upload again
            await client.CopyAsync(localFile, sasLocation, option);

            var deleteOption = new RemoveOption()
            {
                Recursive = true,
            };
            await client.RemoveAsync(sasLocation, deleteOption);

            Assert.True(isSkip);
        }
        public async Task TestUploadAndDeleteLocalFolderWithPatternToSASAsync()
        {
            var hasInfoMessage     = false;
            var hasInitMessage     = false;
            var hasProgressMessage = false;
            var hasEndOfJobMessage = false;
            var jobCompleted       = false;
            var totalFiles         = 0;

            var localFile = new LocalLocation()
            {
                UseWildCard = true,
                Path        = @"TestData/fruits",
            };

            var sasLocation = new RemoteSasLocation()
            {
                ResourceUri = this.resourceUri,
                Container   = this.container,
                Path        = @"fruits",
                SasToken    = this.sasToken,
            };

            var option = new CopyOption()
            {
                Recursive      = true,
                IncludePattern = "*.jpg;*.png",
            };

            var client = new AZCopyClient();

            client.OutputMessageHandler += (object sender, JsonOutputTemplate e) =>
            {
                Console.WriteLine(e.MessageContent);
                if (e.MessageType == MessageType.Info)
                {
                    hasInfoMessage = true;
                }

                if (e.MessageType == MessageType.Init)
                {
                    hasInitMessage = true;
                }

                if (e.MessageType == MessageType.Progress)
                {
                    hasProgressMessage = true;
                }

                if (e.MessageType == MessageType.EndOfJob)
                {
                    hasEndOfJobMessage = true;
                }
            };

            client.JobStatusMessageHandler += (object sender, ListJobSummaryResponse e) =>
            {
                jobCompleted = e.JobStatus == JobStatus.Completed;
                totalFiles   = e.TotalTransfers;
            };

            await client.CopyAsync(localFile, sasLocation, option);

            Assert.True(hasInfoMessage);
            Assert.True(hasInitMessage);
            Assert.True(hasProgressMessage);
            Assert.True(hasEndOfJobMessage);
            Assert.True(jobCompleted);
            Assert.Equal(6, totalFiles);

            hasInitMessage     = false;
            hasProgressMessage = false;
            hasEndOfJobMessage = false;
            jobCompleted       = false;

            var deleteOption = new RemoveOption()
            {
                Recursive = true,
            };
            await client.RemoveAsync(sasLocation, deleteOption);

            Assert.True(hasInitMessage);
            Assert.True(hasProgressMessage);
            Assert.True(hasEndOfJobMessage);
            Assert.True(jobCompleted);
        }
        public async Task TestUploadLocalSingleFileToSASCancellationAsync()
        {
            var hasInfoMessage     = false;
            var hasInitMessage     = false;
            var hasEndOfJobMessage = false;
            var jobCancelled       = false;

            var localFile = new LocalLocation()
            {
                UseWildCard = false,
            };

            var fileName    = this.GetRandomFileName();
            var sasLocation = new RemoteSasLocation()
            {
                ResourceUri = this.resourceUri,
                Container   = this.container,
                Path        = fileName,
                SasToken    = this.sasToken,
            };

            var option = new CopyOption();
            var client = new AZCopyClient();

            client.OutputMessageHandler += (object sender, JsonOutputTemplate e) =>
            {
                this.output.WriteLine(e.MessageContent);
                if (e.MessageType == MessageType.Info)
                {
                    hasInfoMessage = true;
                }

                if (e.MessageType == MessageType.Init)
                {
                    hasInitMessage = true;
                }

                if (e.MessageType == MessageType.EndOfJob)
                {
                    hasEndOfJobMessage = true;
                }
            };

            client.JobStatusMessageHandler += (object sender, ListJobSummaryResponse e) =>
            {
                jobCancelled = e.JobStatus == JobStatus.Cancelled;
            };

            // create cancellation Token
            var cts = new CancellationTokenSource();

            // create random file
            using (var fs = new FileStream(fileName, FileMode.Create, FileAccess.Write))
            {
                fs.SetLength(1024L * 1024); // 1MB
                await fs.FlushAsync();

                localFile.Path = fileName;
            }

            var stopWatch = new Stopwatch();

            stopWatch.Start();
            cts.CancelAfter(5);
            await client.CopyAsync(localFile, sasLocation, option, cts.Token);

            stopWatch.Stop();
            this.output.WriteLine("time elapes: " + stopWatch.ElapsedMilliseconds.ToString());

            Assert.True(hasInfoMessage);
            Assert.True(hasInitMessage);
            Assert.True(hasEndOfJobMessage);
            Assert.True(jobCancelled);
        }
        public async Task TestUploadToNonExistContainerAsync()
        {
            var hasInfoMessage     = false;
            var hasInitMessage     = false;
            var hasProgressMessage = false;
            var hasEndOfJobMessage = false;
            var jobCanceled        = false;
            var errorCode          = 0;

            var localFile = new LocalLocation()
            {
                UseWildCard = false,
            };

            var fileName = this.GetRandomFileName();

            var sasLocation = new RemoteSasLocation()
            {
                ResourceUri = this.resourceUri,
                Container   = "not-exist",
                Path        = fileName,
                SasToken    = this.sasToken,
            };

            var option = new CopyOption();
            var client = new AZCopyClient();

            client.OutputMessageHandler += (object sender, JsonOutputTemplate e) =>
            {
                this.output.WriteLine(e.MessageContent);
                if (e.MessageType == MessageType.Info)
                {
                    hasInfoMessage = true;
                }

                if (e.MessageType == MessageType.Init)
                {
                    hasInitMessage = true;
                }

                if (e.MessageType == MessageType.Progress)
                {
                    hasProgressMessage = true;
                }

                if (e.MessageType == MessageType.EndOfJob)
                {
                    hasEndOfJobMessage = true;
                }
            };

            client.JobStatusMessageHandler += (object sender, ListJobSummaryResponse e) =>
            {
                jobCanceled = e.JobStatus == JobStatus.Failed;
                if (jobCanceled)
                {
                    errorCode = e.FailedTransfers[0].ErrorCode;
                }
            };

            // create random file
            using (var fs = new FileStream(fileName, FileMode.Create, FileAccess.Write))
            {
                fs.SetLength(1024); // 1KB
                await fs.FlushAsync();

                localFile.Path = fileName;
            }

            await client.CopyAsync(localFile, sasLocation, option);

            Assert.True(hasInfoMessage);
            Assert.True(hasInitMessage);
            Assert.True(hasProgressMessage);
            Assert.True(hasEndOfJobMessage);
            Assert.True(jobCanceled);
            Assert.Equal(404, errorCode);
        }
        public async Task TestUploadNonExistLocalFileAsync()
        {
            var hasInfoMessage     = false;
            var hasInitMessage     = false;
            var hasProgressMessage = false;
            var hasEndOfJobMessage = false;
            var jobCompleted       = false;
            var hitError           = false;

            var localFile = new LocalLocation()
            {
                UseWildCard = false,
                Path        = @"not/exist/file.bin",
            };

            var sasLocation = new RemoteSasLocation()
            {
                ResourceUri = this.resourceUri,
                Container   = this.container,
                Path        = @"some-random-file.bin",
                SasToken    = this.sasToken,
            };

            var option = new CopyOption();
            var client = new AZCopyClient();

            client.OutputMessageHandler += (object sender, JsonOutputTemplate e) =>
            {
                this.output.WriteLine(e.MessageContent);
                if (e.MessageType == MessageType.Info)
                {
                    hasInfoMessage = true;
                }

                if (e.MessageType == MessageType.Init)
                {
                    hasInitMessage = true;
                }

                if (e.MessageType == MessageType.Progress)
                {
                    hasProgressMessage = true;
                }

                if (e.MessageType == MessageType.EndOfJob)
                {
                    hasEndOfJobMessage = true;
                }

                if (e.MessageType == MessageType.Error)
                {
                    hitError = true;
                }
            };

            client.JobStatusMessageHandler += (object sender, ListJobSummaryResponse e) =>
            {
                jobCompleted = e.JobStatus == JobStatus.Completed;
            };

            await client.CopyAsync(localFile, sasLocation, option);

            Assert.True(hasInfoMessage);
            Assert.False(hasInitMessage);
            Assert.False(hasProgressMessage);
            Assert.False(hasEndOfJobMessage);
            Assert.False(jobCompleted);
            Assert.True(hitError);
        }
Beispiel #9
0
        public async Task TestUploadAndDeleteLocalFileToSASAsync()
        {
            var hasInfoMessage     = false;
            var hasInitMessage     = false;
            var hasProgressMessage = false;
            var hasEndOfJobMessage = false;
            var jobCompleted       = false;

            var localFile = new LocalLocation()
            {
                UseWildCard = false,
            };

            var fileName = this.GetRandomFileName();

            var sasLocation = new RemoteSasLocation()
            {
                ResourceUri = this.resourceUri,
                Container   = this.container,
                Path        = fileName,
                SasToken    = this.sasToken,
            };

            var option = new AZCopyOption();
            var client = new AZCopyClient();

            client.JobStatusHandler += (object sender, AZCopyMessageBase e) =>
            {
                this.output.WriteLine(e.MessageType + e.MessageContent);
                if (e.MessageType == "Info")
                {
                    hasInfoMessage = true;
                }

                if (e.MessageType == "Init")
                {
                    hasInitMessage = true;
                }

                if (e.MessageType == "Progress")
                {
                    hasProgressMessage = true;
                }

                if (e.MessageType == "EndOfJob")
                {
                    hasEndOfJobMessage = true;
                    jobCompleted       = ((AZCopyEndOfJobMessage)e).JobStatus == "Completed";
                }
            };

            // create random file
            using (var fs = new FileStream(fileName, FileMode.Create, FileAccess.Write))
            {
                fs.SetLength(1024 * 1024); // 1MB
                await fs.FlushAsync();

                localFile.Path = fileName;
            }

            await client.CopyAsync(localFile, sasLocation, option);

            Assert.True(hasInfoMessage);
            Assert.True(hasInitMessage);
            Assert.True(hasProgressMessage);
            Assert.True(hasEndOfJobMessage);
            Assert.True(jobCompleted);

            hasInitMessage     = false;
            hasProgressMessage = false;
            hasEndOfJobMessage = false;
            jobCompleted       = false;

            var deleteOption = new AZDeleteOption();
            await client.DeleteAsync(sasLocation, deleteOption);

            Assert.True(hasInitMessage);
            Assert.True(hasProgressMessage);
            Assert.True(hasEndOfJobMessage);
            Assert.True(jobCompleted);
        }
Beispiel #10
0
        public async Task TestUploadAndDeleteLocalFolderWithPatternToSASAsync()
        {
            var hasInfoMessage     = false;
            var hasInitMessage     = false;
            var hasProgressMessage = false;
            var hasEndOfJobMessage = false;
            var jobCompleted       = false;
            var totalFiles         = 0;

            var localFile = new LocalLocation()
            {
                UseWildCard = true,
                Path        = @"TestData/fruits",
            };

            var sasLocation = new RemoteSasLocation()
            {
                ResourceUri = this.resourceUri,
                Container   = this.container,
                Path        = @"fruits",
                SasToken    = this.sasToken,
            };

            var option = new AZCopyOption()
            {
                Recursive      = string.Empty,
                IncludePattern = "*.jpg;*.png",
            };

            var client = new AZCopyClient();

            client.JobStatusHandler += (object sender, AZCopyMessageBase e) =>
            {
                Console.WriteLine(e.MessageContent);
                if (e.MessageType == "Info")
                {
                    hasInfoMessage = true;
                }

                if (e.MessageType == "Init")
                {
                    hasInitMessage = true;
                }

                if (e.MessageType == "Progress")
                {
                    hasProgressMessage = true;
                }

                if (e.MessageType == "EndOfJob")
                {
                    hasEndOfJobMessage = true;
                    jobCompleted       = ((AZCopyEndOfJobMessage)e).JobStatus == "Completed";
                    totalFiles         = ((AZCopyEndOfJobMessage)e).TotalTransfers;
                }
            };

            await client.CopyAsync(localFile, sasLocation, option);

            Assert.True(hasInfoMessage);
            Assert.True(hasInitMessage);
            Assert.True(hasProgressMessage);
            Assert.True(hasEndOfJobMessage);
            Assert.True(jobCompleted);
            Assert.Equal(6, totalFiles);

            hasInitMessage     = false;
            hasProgressMessage = false;
            hasEndOfJobMessage = false;
            jobCompleted       = false;

            var deleteOption = new AZDeleteOption()
            {
                Recursive = string.Empty,
            };
            await client.DeleteAsync(sasLocation, deleteOption);

            Assert.True(hasInitMessage);
            Assert.True(hasProgressMessage);
            Assert.True(hasEndOfJobMessage);
            Assert.True(jobCompleted);
        }
Beispiel #11
0
        public async Task TestUploadNonExistLocalFileAsync()
        {
            var hasInfoMessage     = false;
            var hasInitMessage     = false;
            var hasProgressMessage = false;
            var hasEndOfJobMessage = false;
            var jobCompleted       = false;
            var hitError           = false;

            var localFile = new LocalLocation()
            {
                UseWildCard = false,
                Path        = @"not/exist/file.bin",
            };

            var sasLocation = new RemoteSasLocation()
            {
                ResourceUri = this.resourceUri,
                Container   = this.container,
                Path        = @"some-random-file.bin",
                SasToken    = this.sasToken,
            };

            var option = new AZCopyOption();
            var client = new AZCopyClient();

            client.JobStatusHandler += (object sender, AZCopyMessageBase e) =>
            {
                this.output.WriteLine(e.MessageContent);
                if (e.MessageType == "Info")
                {
                    hasInfoMessage = true;
                }

                if (e.MessageType == "Init")
                {
                    hasInitMessage = true;
                }

                if (e.MessageType == "Progress")
                {
                    hasProgressMessage = true;
                }

                if (e.MessageType == "EndOfJob")
                {
                    hasEndOfJobMessage = true;
                    jobCompleted       = ((AZCopyEndOfJobMessage)e).JobStatus == "Completed";
                }

                if (e.MessageType == "Error")
                {
                    hitError = true;
                }
            };

            await client.CopyAsync(localFile, sasLocation, option);

            Assert.True(hasInfoMessage);
            Assert.False(hasInitMessage);
            Assert.False(hasProgressMessage);
            Assert.False(hasEndOfJobMessage);
            Assert.False(jobCompleted);
            Assert.True(hitError);
        }
Beispiel #12
0
 public Task CopyRemoteLocalAsync(RemoteSasLocation src, LocalLocation dst, AZCopyOption option, CancellationToken ct)
 {
     return(this.azCopyClient.CopyAsync(src, dst, option, ct));
 }