Пример #1
0
        public void setParams(string connstr)
        {
            var parts = BUCommon.FileSvcBase.ParseConnStr(connstr);
            var opts  = new B2Net.Models.B2Options()
            {
                KeyId            = parts[0].Trim()
                , ApplicationKey = parts[1].Trim()
            };

            //opts.AuthorizationToken = account.auth["AuthorizationToken"];
            //opts.DownloadUrl = account.auth["DownloadUrl"];
            //opts.ApiUrl = account.auth["ApiUrl"];

            /*
             * opts.AuthorizationToken = "<token>";
             * opts.DownloadUrl = "https://f001.backblazeb2.com";
             * opts.ApiUrl = "https://api001.backblazeb2.com";
             */
            _opts = opts;
            account.auth["AuthorizationToken"] = opts.AuthorizationToken;
            account.auth["DownloadUrl"]        = opts.DownloadUrl;
            account.auth["ApiUrl"]             = opts.ApiUrl;

            _client = new B2Client(opts);

            /*
             * var blst = x.Buckets.GetList().Result;
             *
             * var bkt = blst.FirstOrDefault();
             * var flst = x.Files.GetList(bkt.BucketId);
             */
        }
        //https://github.com/coryrwest/B2.NET
        public static bool TestB2Cloud(CloudDrive value, out string message)
        {
            bool   result     = false;
            string errMessage = string.Empty;

            try
            {
                System.Threading.Tasks.Task.Run(() =>
                {
                    try
                    {
                        // the B2Client will default to the bucketId provided here
                        // for all subsequent calls if you set PersistBucket to true.
                        var options = new B2Net.Models.B2Options()
                        {
                            AccountId      = value.Uid,
                            ApplicationKey = value.Password
                                             //,BucketId = "OPTIONAL BUCKET ID",
                                             //PersistBucket = true / false
                        };

                        var client = new B2Net.B2Client(options);
                        // the returned options object will contain the authorizationToken
                        // necessary for subsequent calls to the B2 API.
                        var canConnect = client.Authorize().Result;

                        //canConnect.
                        //List Buckets
                        //var client = new B2Client(options);
                        //options = client.Authorize().Result;
                        var bucketList   = client.Buckets.GetList().Result;
                        var uploadBasked = bucketList.FirstOrDefault(s => s.BucketName.ToUpper() == value.Location.ToUpper());
                        if (uploadBasked == null)
                        {
                            result     = false;
                            errMessage = "Не найдена корзина на обласном сервисе!";
                        }
                        else
                        {
                            result     = true;
                            errMessage = string.Empty;
                        }
                    }
                    catch (Exception e)
                    {
                        result     = false;
                        errMessage = e.Message;
                    }
                }).GetAwaiter().GetResult();

                message = errMessage;
                return(result);
            }
            catch (Exception e)
            {
                message = e.Message;
                return(false);
            }
        }
Пример #3
0
        private void _getDLAuth(TransferData data)
        {
            /* setup a new 'authorize' */
            var opts = new B2Net.Models.B2Options
            {
                KeyId            = _opts.AccountId
                , ApplicationKey = _opts.ApplicationKey
            };

            data.client = new B2Client(opts);
            var res = data.client.Authorize().Result;
        }
Пример #4
0
        /// <summary>
        /// connect to b2.
        /// </summary>
        public void authorize()
        {
            var opts = new B2Net.Models.B2Options
            {
                KeyId            = _opts.AccountId
                , ApplicationKey = _opts.ApplicationKey
            };

            _client = new B2Client(opts);

            _opts = _client.Authorize().Result;
            account.auth["AuthorizationToken"]  = _opts.AuthorizationToken;
            account.auth["DownloadUrl"]         = _opts.DownloadUrl;
            account.auth["ApiUrl"]              = _opts.ApiUrl;
            account.auth["MinPartSize"]         = string.Format("{0:n}", _opts.AbsoluteMinimumPartSize);
            account.auth["RecommendedPartSize"] = string.Format("{0:n}", _opts.RecommendedPartSize);
        }
        public static bool UploadToB2Cloud(CloudDrive value, string file, out string message)
        {
            bool   result     = false;
            string uploadLink = string.Empty;

            try
            {
                System.Threading.Tasks.Task.Run(() =>
                {
                    // the B2Client will default to the bucketId provided here
                    // for all subsequent calls if you set PersistBucket to true.
                    var options = new B2Net.Models.B2Options()
                    {
                        AccountId      = value.Uid,
                        ApplicationKey = value.Password
                                         //,BucketId = "OPTIONAL BUCKET ID",
                                         //PersistBucket = true / false
                    };
                    var client            = new B2Net.B2Client(options);
                    options               = client.Authorize().Result;
                    string uploadFileName = System.IO.Path.GetFileName(file);
                    var bFileData         = File.ReadAllBytes(file);
                    var bucketList        = client.Buckets.GetList().Result;
                    var uploadBasked      = bucketList.FirstOrDefault(s => s.BucketName.ToUpper() == value.Location.ToUpper());
                    try
                    {
                        var fileResult = client.Files.Upload(bFileData, uploadFileName, uploadBasked.BucketId).Result;
                        Console.WriteLine(fileResult.FileId);
                    }
                    catch (Exception uploadErr)
                    {
                        Console.WriteLine(uploadErr);
                        throw;
                    }


                    // { FileId: "",
                    //   FileName: "",
                    //   ContentLength: "",
                    //   ContentSHA1: "",
                    //   ContentType: "",
                    //   FileInfo: Dictionary<string,string> }

                    //var client = pCloud.NET.pCloudClient.CreateClientAsync(value.Uid, value.Password).Result;
                    //CancellationToken cancellationToken = new CancellationToken();
                    //var fileStream = System.IO.File.Open(file, FileMode.Open, FileAccess.Read);
                    //string uploadFileName = System.IO.Path.GetFileName(file);
                    //var folder = client.ListFolderAsync(0).Result;
                    //var res = client.UploadFileAsync(fileStream, 0, uploadFileName, cancellationToken).Result;
                    result = true;
                }).GetAwaiter().GetResult();

                message = string.Empty;
                return(result);
            }
            catch (Exception e)
            {
                message = e.Message;
                return(false);
            }
        }