Exemplo n.º 1
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="options"></param>
        /// <returns></returns>
        private async Task <int> UploadFiles(Options options)
        {
            var b2 = new B2();

            b2.Login(options.AccountId, options.ApplicationKey, options.BucketName).Wait();
            b2.OnLargeFileUploadProgress += OnLargeFileUploadProgress;

            var scannedFiles = FilesystemScanner.Scan(options.Source, options.Recursive);
            Progress <StreamProgress> progress = new Progress <StreamProgress>();

            progress.ProgressChanged += Progress_ProgressChanged;

            var tasks = new Dictionary <Task, string>();

            foreach (var file in scannedFiles)
            {
                if (file.Length < 100 * (1000 * 1000))
                {
                    while (tasks.Count >= options.SimultaneousConnections)
                    {
                        var tempTasks = tasks.Keys.ToArray();
                        var i         = Task.WaitAny(tempTasks);
                        if (i >= 0)
                        {
                            var completedTask = tempTasks[i];
                            var path          = tasks[completedTask];
                            if (completedTask.IsFaulted || completedTask.IsCanceled)
                            {
                                var error = "Unknown error";
                                if (completedTask.Exception != null)
                                {
                                    error = completedTask.Exception.Message;
                                    while (completedTask.Exception.InnerException != null)
                                    {
                                        error += "\r\n" + completedTask.Exception.InnerException.Message;
                                    }
                                }

                                Console.Error.WriteLine(path + ": " + error);
                            }
                            else
                            {
                                Console.WriteLine(path);
                            }

                            tasks.Remove(completedTask);
                        }
                    }

                    await b2.UploadFile(file.Info.FullName, file.RelativePath.ToUnixPath(), "application/octet-stream", null, progress);
                }
                else
                {
                    Console.WriteLine($"File too large than 100Mb detected, use UploadLargeFile instead of normal upload - file name => {file.Info.Name}");
                    await UpLargeFile(file, b2);
                }
            }

            return(0);
        }
Exemplo n.º 2
0
        private async Task <int> UploadLargeFile(Options options)
        {
            var b2 = new B2();

            b2.Login(options.AccountId, options.ApplicationKey, options.BucketName).Wait();

            var scannedFiles = FilesystemScanner.Scan(options.Source, options.Recursive);

            foreach (var file in scannedFiles)
            {
                await UpLargeFile(file, b2);
            }

            return(0);
        }
Exemplo n.º 3
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="options"></param>
        /// <returns></returns>
        internal int UploadFiles(Options options)
        {
            var b2 = new B2();

            b2.Login(options.AccountId, options.ApplicationKey, options.BucketName).Wait();

            var scannedFiles = FilesystemScanner.Scan(options.Source, options.Recursive);

            var tasks = new Dictionary <Task, string>();

            foreach (var file in scannedFiles)
            {
                while (tasks.Count >= options.SimultaneousConnections)
                {
                    var tempTasks = tasks.Keys.ToArray();
                    var i         = Task.WaitAny(tempTasks);
                    if (i >= 0)
                    {
                        var completedTask = tempTasks[i];
                        var path          = tasks[completedTask];
                        if (completedTask.IsFaulted || completedTask.IsCanceled)
                        {
                            var error = "Unknown error";
                            if (completedTask.Exception != null)
                            {
                                error = completedTask.Exception.Message;
                                if (completedTask.Exception.InnerException != null)
                                {
                                    error = completedTask.Exception.InnerException.Message;
                                }
                            }
                            Console.Error.WriteLine(path + ": " + error);
                        }
                        else
                        {
                            Console.WriteLine(path);
                        }

                        tasks.Remove(completedTask);
                    }
                }
                var task = b2.UploadFile(file.Info.FullName, file.RelativePath.ToUnixPath());
                tasks[task] = file.Info.FullName;
            }
            return(0);
        }
Exemplo n.º 4
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="options"></param>
        /// <returns></returns>
        private async Task <int> DownloadFiles(Options options)
        {
            var b2 = new B2();

            b2.Login(options.AccountId, options.ApplicationKey, options.BucketName).Wait();

            string downloadUrl = b2.DownloadUrl;
            string bucketName  = options.BucketName;
            string fileName    = options.FileName;

            string         accountAuthorizationToken = b2.AuthorizationToken;
            HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create($"{downloadUrl}/file/{bucketName}/{fileName}");

            webRequest.Method = "GET";
            webRequest.Headers.Add("Authorization", accountAuthorizationToken);
            WebResponse response = (HttpWebResponse)await webRequest.GetResponseAsync();

            Stream responseStream = response.GetResponseStream();

            byte[] fileBytes;
            using (BinaryReader br = new BinaryReader(responseStream))
            {
                fileBytes = br.ReadBytes(500000);
                br.Close();
            }
            responseStream.Close();
            response.Close();
            string       downloadsFolder = options.Destination;
            FileStream   saveFile        = new FileStream(downloadsFolder, FileMode.Create);
            BinaryWriter writeFile       = new BinaryWriter(saveFile);

            try
            {
                writeFile.Write(fileBytes);
            }
            finally
            {
                saveFile.Close();
                writeFile.Close();
            }

            return(0);
        }