/// <summary>
        /// Used to modify the bucket type of the provided bucket.
        /// </summary>
        /// <param name="options"></param>
        /// <returns></returns>
        public static HttpRequestMessage UpdateBucket(B2Options options, string bucketId, string bucketType)
        {
            var body = "{\"accountId\":\"" + options.AccountId + "\", \"bucketId\":\"" + bucketId + "\", \"bucketType\":\"" +
                       bucketType + "\"}";

            return(BaseRequestGenerator.PostRequest(Endpoints.Update, body, options));
        }
Example #2
0
        public static HttpRequestMessage ListVersions(B2Options options, string bucketId, string startFileName = "", string startFileId = "", int?maxFileCount = null, string prefix = "", string delimiter = "")
        {
            var body = "{\"bucketId\":\"" + bucketId + "\"";

            if (!string.IsNullOrEmpty(startFileName))
            {
                body += ", \"startFileName\":" + JsonConvert.ToString(startFileName);
            }
            if (!string.IsNullOrEmpty(startFileId))
            {
                body += ", \"startFileId\":\"" + startFileId + "\"";
            }
            if (maxFileCount.HasValue)
            {
                body += ", \"maxFileCount\":" + maxFileCount.Value.ToString();
            }
            if (!string.IsNullOrEmpty(prefix))
            {
                body += ", \"prefix\":" + JsonConvert.ToString(prefix);
            }
            if (!string.IsNullOrEmpty(delimiter))
            {
                body += ", \"delimiter\":" + JsonConvert.ToString(delimiter);
            }
            body += "}";
            return(BaseRequestGenerator.PostRequest(Endpoints.Versions, body, options));
        }
Example #3
0
        /// <summary>
        /// Create a bucket. Defaults to allPrivate.
        /// </summary>
        /// <param name="options"></param>
        /// <param name="bucketName"></param>
        /// <param name="bucketType"></param>
        /// <returns></returns>
        public static HttpRequestMessage CreateBucket(B2Options options, string bucketName, B2BucketOptions bucketOptions)
        {
            // Check lifecycle rules
            var hasLifecycleRules = bucketOptions.LifecycleRules != null && bucketOptions.LifecycleRules.Count > 0;

            if (hasLifecycleRules)
            {
                foreach (var rule in bucketOptions.LifecycleRules)
                {
                    if (rule.DaysFromHidingToDeleting < 1 || rule.DaysFromUploadingToHiding < 1)
                    {
                        throw new System.Exception("The smallest number of days you can set in a lifecycle rule is 1.");
                    }
                    if (rule.DaysFromHidingToDeleting == null && rule.DaysFromUploadingToHiding == null)
                    {
                        throw new System.Exception("You must set either DaysFromHidingToDeleting or DaysFromUploadingToHiding. Both cannot be null.");
                    }
                }
            }

            var allowed = new Regex("^[a-zA-Z0-9-]+$");

            if (bucketName.Length < 6 || bucketName.Length > 50 || !allowed.IsMatch(bucketName) || bucketName.StartsWith("b2-"))
            {
                throw new Exception(@"The bucket name specified does not match the requirements. 
                            Bucket Name can consist of upper-case letters, lower-case letters, numbers, and "" - "", 
                            must be at least 6 characters long, and can be at most 50 characters long");
            }

            var body = new B2BucketCreateModel()
            {
                accountId  = options.AccountId,
                bucketName = bucketName,
                bucketType = bucketOptions.BucketType.ToString()
            };

            // Add optional options
            if (bucketOptions.CacheControl != 0)
            {
                body.bucketInfo = new Dictionary <string, string>()
                {
                    { "Cache-Control", "max-age=" + bucketOptions.CacheControl }
                };
            }
            if (hasLifecycleRules)
            {
                body.lifecycleRules = bucketOptions.LifecycleRules;
            }

            // Has cors rules
            if (bucketOptions.CORSRules != null && bucketOptions.CORSRules.Count > 0)
            {
                body.corsRules = bucketOptions.CORSRules;
            }

            var json = JsonSerialize(body);

            return(BaseRequestGenerator.PostRequest(Endpoints.Create, json, options));
        }
        /// <summary>
        /// Create a bucket. Defaults to allPrivate.
        /// </summary>
        /// <param name="options"></param>
        /// <param name="bucketName"></param>
        /// <param name="bucketType"></param>
        /// <returns></returns>
        public static HttpRequestMessage CreateBucket(B2Options options, string bucketName, string bucketType = "allPrivate")
        {
            // TODO: Handle naming conventions, check name for invalid characters.
            var body = "{\"accountId\":\"" + options.AccountId + "\", \"bucketName\":\"" + bucketName +
                       "\", \"bucketType\":\"" + bucketType + "\"}";

            return(BaseRequestGenerator.PostRequest(Endpoints.Create, body, options));
        }
Example #5
0
        public static HttpRequestMessage HideFile(B2Options options, string bucketId, string fileName = "", string fileId = "")
        {
            var body = "{\"bucketId\":\"" + bucketId + "\"";

            if (!string.IsNullOrEmpty(fileName) && string.IsNullOrEmpty(fileId))
            {
                body += ", \"fileName\":" + JsonConvert.ToString(fileName);
            }
            if (!string.IsNullOrEmpty(fileId))
            {
                body += ", \"fileId\":\"" + fileId + "\"";
            }
            body += "}";
            return(BaseRequestGenerator.PostRequest(Endpoints.Hide, body, options));
        }
Example #6
0
        /// <summary>
        /// Create a bucket. Defaults to allPrivate.
        /// </summary>
        /// <param name="options"></param>
        /// <param name="bucketName"></param>
        /// <param name="bucketType"></param>
        /// <returns></returns>
        public static HttpRequestMessage CreateBucket(B2Options options, string bucketName, string bucketType = "allPrivate")
        {
            var allowed = new Regex("^[a-zA-Z0-9-]+$");

            if (bucketName.Length < 6 || bucketName.Length > 50 || !allowed.IsMatch(bucketName) || bucketName.StartsWith("b2-"))
            {
                throw new Exception(@"The bucket name specified does not match the requirements. 
                            Bucket Name can consist of upper-case letters, lower-case letters, numbers, and "" - "", 
                            must be at least 6 characters long, and can be at most 50 characters long");
            }

            var json = JsonConvert.SerializeObject(new { accountId = options.AccountId, bucketName, bucketType });

            return(BaseRequestGenerator.PostRequest(Endpoints.Create, json, options));
        }
Example #7
0
        public static HttpRequestMessage GetList(B2Options options, string bucketId, string startFileName = "", int?maxFileCount = null)
        {
            var body = "{\"bucketId\":\"" + bucketId + "\"";

            if (!string.IsNullOrEmpty(startFileName))
            {
                body += ", \"startFileName\":" + JsonConvert.ToString(startFileName);
            }
            if (maxFileCount.HasValue)
            {
                body += ", \"maxFileCount\":" + maxFileCount.Value.ToString();
            }
            body += "}";
            return(BaseRequestGenerator.PostRequest(Endpoints.List, body, options));
        }
        /// <summary>
        /// Create a bucket. Defaults to allPrivate.
        /// </summary>
        /// <param name="options"></param>
        /// <param name="bucketName"></param>
        /// <param name="bucketType"></param>
        /// <returns></returns>
        public static HttpRequestMessage CreateBucket(B2Options options, string bucketName, B2BucketOptions bucketOptions)
        {
            // Check lifecycle rules
            var hasLifecycleRules = bucketOptions.LifecycleRules != null && bucketOptions.LifecycleRules.Count > 0;

            if (hasLifecycleRules)
            {
                foreach (var rule in bucketOptions.LifecycleRules)
                {
                    if (rule.DaysFromHidingToDeleting < 1 || rule.DaysFromUploadingToHiding < 1)
                    {
                        throw new System.Exception("The smallest number of days you can set in a lifecycle rule is 1.");
                    }
                    if (rule.DaysFromHidingToDeleting == null && rule.DaysFromUploadingToHiding == null)
                    {
                        throw new System.Exception("You must set either DaysFromHidingToDeleting or DaysFromUploadingToHiding. Both cannot be null.");
                    }
                }
            }

            // TODO: Handle naming conventions, check name for invalid characters.
            var body = new B2BucketCreateModel()
            {
                accountId  = options.AccountId,
                bucketName = bucketName,
                bucketType = bucketOptions.BucketType.ToString()
            };

            // Add optional options
            if (bucketOptions.CacheControl != 0)
            {
                body.bucketInfo = new Dictionary <string, string>()
                {
                    { "Cache-Control", "max-age=" + bucketOptions.CacheControl }
                };
            }
            if (hasLifecycleRules)
            {
                body.lifecycleRules = bucketOptions.LifecycleRules;
            }

            var json = JsonSerialize(body);

            return(BaseRequestGenerator.PostRequest(Endpoints.Create, json, options));
        }
Example #9
0
        public static HttpRequestMessage DeleteBucket(B2Options options, string bucketId)
        {
            var json = JsonConvert.SerializeObject(new { accountId = options.AccountId, bucketId });

            return(BaseRequestGenerator.PostRequest(Endpoints.Delete, json, options));
        }
Example #10
0
        public static HttpRequestMessage GetBucketList(B2Options options)
        {
            var json = JsonConvert.SerializeObject(new { accountId = options.AccountId });

            return(BaseRequestGenerator.PostRequest(Endpoints.List, json, options));
        }
 public static HttpRequestMessage GetUploadUrl(B2Options options, string bucketId)
 {
     return(BaseRequestGenerator.PostRequest(Endpoints.GetUploadUrl, "{\"bucketId\":\"" + bucketId + "\"}", options));
 }
Example #12
0
        /// <summary>
        /// Used to modify the bucket type of the provided bucket.
        /// </summary>
        /// <param name="revisionNumber">(optional) When set, the update will only happen if the revision number stored in the B2 service matches the one passed in. This can be used to avoid having simultaneous updates make conflicting changes. </param>
        /// <returns></returns>
        public static HttpRequestMessage UpdateBucket(B2Options options, string bucketId, B2BucketOptions bucketOptions, int?revisionNumber = null)
        {
            // Check lifecycle rules
            var hasLifecycleRules = bucketOptions.LifecycleRules != null && bucketOptions.LifecycleRules.Count > 0;

            if (hasLifecycleRules)
            {
                foreach (var rule in bucketOptions.LifecycleRules)
                {
                    if (rule.DaysFromHidingToDeleting < 1 || rule.DaysFromUploadingToHiding < 1)
                    {
                        throw new System.Exception("The smallest number of days you can set in a lifecycle rule is 1.");
                    }
                    if (rule.DaysFromHidingToDeleting == null && rule.DaysFromUploadingToHiding == null)
                    {
                        throw new System.Exception("You must set either DaysFromHidingToDeleting or DaysFromUploadingToHiding. Both cannot be null.");
                    }
                }
            }

            var body = new B2BucketUpdateModel()
            {
                accountId  = options.AccountId,
                bucketId   = bucketId,
                bucketType = bucketOptions.BucketType.ToString()
            };

            // Add optional options
            if (bucketOptions.CacheControl != 0)
            {
                body.bucketInfo = new Dictionary <string, string>()
                {
                    { "Cache-Control", "max-age=" + bucketOptions.CacheControl }
                };
            }
            if (hasLifecycleRules)
            {
                body.lifecycleRules = bucketOptions.LifecycleRules;
            }

            // Has cors rules
            if (bucketOptions.CORSRules != null && bucketOptions.CORSRules.Count > 0)
            {
                if (bucketOptions.CORSRules.Any(x => x.AllowedOperations == null || x.AllowedOperations.Length == 0))
                {
                    throw new System.Exception("You must set allowedOperations on the bucket CORS rules.");
                }
                if (bucketOptions.CORSRules.Any(x => x.AllowedOrigins == null || x.AllowedOrigins.Length == 0))
                {
                    throw new System.Exception("You must set allowedOrigins on the bucket CORS rules.");
                }
                if (bucketOptions.CORSRules.Any(x => string.IsNullOrEmpty(x.CorsRuleName)))
                {
                    throw new System.Exception("You must set corsRuleName on the bucket CORS rules.");
                }
                body.corsRules = bucketOptions.CORSRules;
            }

            if (revisionNumber.HasValue)
            {
                body.ifRevisionIs = revisionNumber.Value;
            }

            var json = JsonSerialize(body);

            return(BaseRequestGenerator.PostRequest(Endpoints.Update, json, options));
        }
Example #13
0
        public static HttpRequestMessage GetUploadUrl(B2Options options, string bucketId)
        {
            var json = JsonConvert.SerializeObject(new { bucketId });

            return(BaseRequestGenerator.PostRequest(Endpoints.GetUploadUrl, json, options));
        }
        public static HttpRequestMessage Delete(B2Options options, string fileId, string fileName)
        {
            var json = JsonConvert.SerializeObject(new { fileId, fileName });

            return(BaseRequestGenerator.PostRequest(Endpoints.Delete, json, options));
        }
Example #15
0
        public static HttpRequestMessage GetInfo(B2Options options, string fileId)
        {
            var json = JsonConvert.SerializeObject(new { fileId });

            return(BaseRequestGenerator.PostRequest(Endpoints.Info, json, options));
        }
 public static HttpRequestMessage GetBucketList(B2Options options)
 {
     return(BaseRequestGenerator.PostRequest(Endpoints.List, "{\"accountId\":\"" + options.AccountId + "\"}", options));
 }
 public static HttpRequestMessage DeleteBucket(B2Options options, string bucketId)
 {
     return(BaseRequestGenerator.PostRequest(Endpoints.Delete, "{\"accountId\":\"" + options.AccountId + "\", \"bucketId\":\"" + bucketId + "\"}", options));
 }
Example #18
0
 public static HttpRequestMessage GetInfo(B2Options options, string fileId)
 {
     return(BaseRequestGenerator.PostRequest(Endpoints.Info, "{\"fileId\":\"" + fileId + "\"}", options));
 }
 public static HttpRequestMessage Delete(B2Options options, string fileId, string fileName)
 {
     return(BaseRequestGenerator.PostRequest(Endpoints.Delete,
                                             "{\"fileId\":\"" + fileId + "\", \"fileName\":" + JsonConvert.ToString(fileName) + "}", options));
 }