Exemplo n.º 1
0
        /// <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, 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 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;
            }

            var json = JsonSerialize(body);

            return(BaseRequestGenerator.PostRequest(Endpoints.Update, json, options));
        }
Exemplo n.º 2
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));
        }