示例#1
0
        public async Task CopyAsync(LocationBase src, LocationBase dst, CopyOption option, CancellationToken ct = default)
        {
            option.OutputType = "json";
            var args = $"copy {src} {dst} {option} --cancel-from-stdin";

            await this.StartAZCopyAsync(args, ct);
        }
示例#2
0
 /// <summary>
 /// Copies an existing file to a new file. Overwriting a file of the same
 /// name is not allowed.
 /// </summary>
 /// <param name="sourcePath">The file to copy.</param>
 /// <param name="targetPath">The name of the destination file.</param>
 /// <param name="copyOption">Options for overwriting</param>
 public void CopyFile(string sourcePath, string targetPath, CopyOption copyOption)
 {
     if (copyOption == CopyOption.Overwrite && FileExists(targetPath))
     {
         SetAttributes(targetPath, FileAttributes.Normal);
     }
     File.Copy(sourcePath, targetPath, copyOption == CopyOption.Overwrite);
 }
        public void CommandArgsBase_should_build_arguments_if_arg_type_is_float()
        {
            var copyOption = new CopyOption();

            copyOption.ToString().Should().BeEmpty();

            copyOption.CapMbps = 100;
            copyOption.ToString().Should().Be("--cap-mbps=100");
        }
        public void CommandArgsBase_should_build_arguments_if_use_quote_is_true()
        {
            var copyOption = new CopyOption();

            copyOption.ToString().Should().BeEmpty();

            // BlobType is string type.
            copyOption.BlobType = "BlockBlob";
            copyOption.ToString().Should().Be("--blob-type=\"BlockBlob\"");
        }
        public void CommandArgsBase_should_build_arguments_if_is_flag_is_true()
        {
            var copyOption = new CopyOption();

            Assert.Equal(string.Empty, copyOption.ToString());

            copyOption.Recursive = true;
            copyOption.ToString().Should().Be("--recursive=true");

            copyOption.Recursive = false;
            copyOption.ToString().Should().Be("--recursive=false");
        }
        public void CommandArgsBase_should_build_arguments_if_arg_type_is_bool()
        {
            var copyOption = new CopyOption();

            copyOption.ToString().Should().BeEmpty();

            copyOption.CheckLength = false;
            copyOption.ToString().Should().Be("--check-length=false");

            copyOption.CheckLength = true;
            copyOption.ToString().Should().Be("--check-length=true");
        }
示例#7
0
        /// <summary>
        /// Converts the given array of options for moving a file to options suitable
        /// for copying the file when a move is implemented as copy + delete.
        /// </summary>
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
//ORIGINAL LINE: private static CopyOption[] convertMoveToCopyOptions(CopyOption... options) throws AtomicMoveNotSupportedException
        private static CopyOption[] ConvertMoveToCopyOptions(params CopyOption[] options)
        {
            int len = options.Length;

            CopyOption[] newOptions = new CopyOption[len + 2];
            for (int i = 0; i < len; i++)
            {
                CopyOption option = options[i];
                if (option == StandardCopyOption.ATOMIC_MOVE)
                {
                    throw new AtomicMoveNotSupportedException(null, null, "Atomic move between providers is not supported");
                }
                newOptions[i] = option;
            }
            newOptions[len]     = LinkOption.NOFOLLOW_LINKS;
            newOptions[len + 1] = StandardCopyOption.COPY_ATTRIBUTES;
            return(newOptions);
        }
        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);
        }
示例#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 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);
        }
示例#11
0
        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);
        }
示例#12
0
        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);
        }
示例#13
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 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);
        }
示例#14
0
 public CopyAttribute(string outputPropertyName, CopyOption copyOption = CopyOption.CopyAlways)
 {
     OutputPropertyName = outputPropertyName;
     CopyOption         = copyOption;
 }
示例#15
0
 public CopyAttribute(CopyOption copyOption)
 {
     CopyOption = copyOption;
 }
示例#16
0
 public CopyAttribute()
 {
     CopyOption = CopyOption.CopyAlways;
 }