Exemplo n.º 1
0
        static void Main(string[] args)
        {
            string accountId      = Console.ReadLine();
            string applicationKey = Console.ReadLine();
            // This is all just testing code, it doesn't really matter what happens here
            B2SDK    sdk       = new B2SDK(accountId, applicationKey);
            B2Bucket newBucket = sdk.b2_create_bucket("ThisIsAnAwesomeBucket", "allPrivate");

            Console.WriteLine(newBucket.accountId + newBucket.bucketId + newBucket.bucketName + newBucket.bucketType);

            B2Bucket deletedBucket = sdk.b2_delete_bucket(newBucket.bucketId);

            if (deletedBucket.bucketId == newBucket.bucketId)
            {
                Console.WriteLine("Delete Successful");
            }

            // fun test code that will delete all files in our buckets!!
            List <B2Bucket> buckets = sdk.b2_list_buckets();

            foreach (B2Bucket bucket in buckets)
            {
                foreach (B2File file in sdk.b2_list_file_versions(bucket.bucketId, "").files)
                {
                    sdk.b2_delete_file_version(file.fileName, file.fileId);
                }
            }
        }
Exemplo n.º 2
0
        public void Initialize()
        {
            Client  = new B2Client(Options);
            Options = Client.Authorize().Result;

            var      buckets        = Client.Buckets.GetList().Result;
            B2Bucket existingBucket = null;

            foreach (B2Bucket b2Bucket in buckets)
            {
                if (b2Bucket.BucketName == "B2NETTestingBucket")
                {
                    existingBucket = b2Bucket;
                }
            }

            if (existingBucket != null)
            {
                TestBucket = existingBucket;
            }
            else
            {
                TestBucket = Client.Buckets.Create("B2NETTestingBucket", BucketTypes.allPrivate).Result;
            }
        }
Exemplo n.º 3
0
        public void Initialize()
        {
            Client     = new B2Client(Options.AccountId, Options.ApplicationKey);
            BucketName = $"B2NETTestingBucket-{Path.GetRandomFileName().Replace(".", "").Substring(0, 6)}";

            var      buckets        = Client.Buckets.GetList().Result;
            B2Bucket existingBucket = null;

            foreach (B2Bucket b2Bucket in buckets)
            {
                if (b2Bucket.BucketName == BucketName)
                {
                    existingBucket = b2Bucket;
                }
            }

            if (existingBucket != null)
            {
                TestBucket = existingBucket;
            }
            else
            {
                TestBucket = Client.Buckets.Create(BucketName, BucketTypes.allPrivate).Result;
            }
        }
Exemplo n.º 4
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="bucketId">ID of bucket to be deleted</param>
        /// <returns>Object for bucket that was deleted</returns>
        public B2Bucket b2_delete_bucket(string bucketId)
        {
            if (!checkStringParamsNotEmpty(new string[] { bucketId }) || !authorized)
            {
                return(null);
            }
            HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(apiUrl + "/b2api/v1/b2_delete_bucket");
            string         body       =
                "{\"accountId\":\"" + accountID + "\",\n" +
                "\"bucketId\":\"" + bucketId + "\"}";
            var data = Encoding.UTF8.GetBytes(body);

            webRequest.Method = "POST";
            webRequest.Headers.Add("Authorization", authorizationToken);
            webRequest.ContentType   = "application/json; charset=utf-8";
            webRequest.ContentLength = data.Length;
            using (var stream = webRequest.GetRequestStream())
            {
                stream.Write(data, 0, data.Length);
                stream.Close();
            }
            try
            {
                HttpWebResponse response       = (HttpWebResponse)webRequest.GetResponse();
                var             responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();
                dynamic         jsonData       = JsonConvert.DeserializeObject(responseString);
                B2Bucket        returnData     = new B2Bucket((String)jsonData.accountId, (String)jsonData.bucketId, (String)jsonData.bucketName, (String)jsonData.bucketType);
                return(returnData);
            }
            // we catch ex in case we have a better way of logging errors to the user in the future
            catch (Exception ex)
            {
                return(null);
            };
        }
Exemplo n.º 5
0
        public void CreateBucketWithCacheControlTest()
        {
            var bucket = Client.Buckets.Create(BucketName, new B2BucketOptions()
            {
                CacheControl = 600
            }).Result;

            // Get bucket to check for info
            var bucketList = Client.Buckets.GetList().Result;

            // Clean up
            if (!string.IsNullOrEmpty(bucket.BucketId))
            {
                Client.Buckets.Delete(bucket.BucketId).Wait();
            }

            B2Bucket savedBucket = bucketList.FirstOrDefault(b => b.BucketName == bucket.BucketName);

            Assert.IsNotNull(savedBucket, "Retreived bucket was null");
            Assert.IsNotNull(savedBucket.BucketInfo, "Bucekt info was null");
            Assert.IsTrue(savedBucket.BucketInfo.ContainsKey("cache-control"), "Bucket info did not contain Cache-Control");
            Assert.AreEqual("max-age=600", savedBucket.BucketInfo["cache-control"], "Cache-Control values were not equal.");
        }
Exemplo n.º 6
0
        /// <summary>
        /// <param name="bucketName"> Name for the new bucket(unique)</param>
        /// <param name="bucketType">"allPrivate" - or - "allpublic"</param>
        /// </summary>
        /// <returns> B2Bucket object with created bucket. Returns null for failure.</returns>
        public B2Bucket b2_create_bucket(string bucketName, string bucketType)
        {
            if (!checkStringParamsNotEmpty(new string[] { bucketName, bucketType }) || !authorized)
            {
                return(null);
            }
            HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(apiUrl + "/b2api/v1/b2_create_bucket");
            string         body       =
                "{\"accountId\":\"" + accountID + "\",\n" +
                "\"bucketName\":\"" + bucketName + "\",\n" +
                "\"bucketType\":\"" + bucketType + "\"}";
            var data = Encoding.UTF8.GetBytes(body);

            webRequest.Method = "POST";
            webRequest.Headers.Add("Authorization", authorizationToken);
            webRequest.ContentType = "application/json; charset=utf-8";
            using (var stream = webRequest.GetRequestStream())
            {
                stream.Write(data, 0, data.Length);
                stream.Close();
            }
            HttpWebResponse response = (HttpWebResponse)webRequest.GetResponse();

            if (response.StatusCode == HttpStatusCode.OK)
            {
                var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();
                response.Close();
                dynamic  jsonData   = JsonConvert.DeserializeObject(responseString);
                B2Bucket returnData = new B2Bucket((String)jsonData.accountId, (String)jsonData.bucketId, (String)jsonData.bucketName, (String)jsonData.bucketType);
                return(returnData);
            }
            else
            {
                return(null);
            }
        }
Exemplo n.º 7
0
 /// <summary>
 /// <param name="bucketName"> Name for the new bucket(unique)</param>
 /// <param name="bucketType">"allPrivate" - or - "allpublic"</param>
 /// </summary>
 /// <returns> B2Bucket object with created bucket. Returns null for failure.</returns>
 public B2Bucket b2_create_bucket(string bucketName, string bucketType)
 {
     if (!checkStringParamsNotEmpty(new string[] { bucketName, bucketType }) || !authorized)
     {
         return null;
     }
     HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(apiUrl + "/b2api/v1/b2_create_bucket");
     string body =
             "{\"accountId\":\"" + accountID + "\",\n" +
             "\"bucketName\":\"" + bucketName + "\",\n" +
             "\"bucketType\":\"" + bucketType + "\"}";
     var data = Encoding.UTF8.GetBytes(body);
     webRequest.Method = "POST";
     webRequest.Headers.Add("Authorization", authorizationToken);
     webRequest.ContentType = "application/json; charset=utf-8";
     using (var stream = webRequest.GetRequestStream())
     {
         stream.Write(data, 0, data.Length);
         stream.Close();
     }
     HttpWebResponse response = (HttpWebResponse)webRequest.GetResponse();
     if (response.StatusCode == HttpStatusCode.OK)
     {
         var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();
         response.Close();
         dynamic jsonData = JsonConvert.DeserializeObject(responseString);
         B2Bucket returnData = new B2Bucket((String)jsonData.accountId, (String)jsonData.bucketId, (String)jsonData.bucketName, (String)jsonData.bucketType);
         return returnData;
     } else
     {
         return null;
     }
 }
Exemplo n.º 8
0
 /// <summary>
 /// Updates bucket information
 /// </summary>
 /// <param name="bucketId"> Id of bucket to update</param>
 /// <param name="bucketType"> "allPrivate" or "allPublic" for private or public bucket</param>
 /// <returns> B2Bucket -- the updated bucket after applying the changes</returns>
 public B2Bucket b2_update_bucket(string bucketId, string bucketType)
 {
     if (!checkStringParamsNotEmpty(new string[] { bucketType, bucketId }) || !authorized)
     {
         return null;
     }
     HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(apiUrl + "/b2api/v1/b2_update_bucket");
     string body =
     "{\"accountId\":\"" + accountID + "\",\n" +
     "\"bucketId\":\"" + bucketId + "\",\n" +
     "\"bucketType\":\"" + bucketType + "\"}";
     var data = Encoding.UTF8.GetBytes(body);
     webRequest.Method = "POST";
     webRequest.Headers.Add("Authorization", authorizationToken);
     webRequest.ContentType = "application/json; charset=utf-8";
     webRequest.ContentLength = data.Length;
     using (var stream = webRequest.GetRequestStream())
     {
         stream.Write(data, 0, data.Length);
         stream.Close();
     }
     try
     {
         HttpWebResponse response = (HttpWebResponse)webRequest.GetResponse();
         var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();
         dynamic jsonData = JsonConvert.DeserializeObject(responseString);
         B2Bucket returnData = new B2Bucket((String)jsonData.accountId, (String)jsonData.bucketId, (String)jsonData.bucketName, (String)jsonData.bucketType);
         return returnData;
     }
     // we catch ex in case we have a better way of logging errors to the user in the future
     catch (Exception ex)
     {
         return null;
     };
 }