/// <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, 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)); }