Example #1
0
        /// <summary>
        /// Creates a new bucket. A bucket belongs to the account used to create it. If BucketType is not set allPrivate will be used by default.
        /// Use this method to set Cache-Control.
        /// </summary>
        /// <param name="bucketName"></param>
        /// <param name="bucketType"></param>
        /// <param name="cancelToken"></param>
        /// <returns></returns>
        public async Task <B2Bucket> Create(string bucketName, B2BucketOptions options, CancellationToken cancelToken = default(CancellationToken))
        {
            var requestMessage = BucketRequestGenerators.CreateBucket(_options, bucketName, options);
            var response       = await _client.SendAsync(requestMessage, cancelToken);

            return(await ResponseParser.ParseResponse <B2Bucket>(response));
        }
Example #2
0
        /// <summary>
        /// Update an existing bucket. bucketId is only optional if you are persisting a bucket for this client.
        /// Use this method to set Cache-Control.
        /// </summary>
        /// <param name="bucketType"></param>
        /// <param name="bucketId"></param>
        /// <param name="cancelToken"></param>
        /// <returns></returns>
        public async Task <B2Bucket> Update(B2BucketOptions options, string bucketId = "", CancellationToken cancelToken = default(CancellationToken))
        {
            var operationalBucketId = Utilities.DetermineBucketId(_options, bucketId);

            var requestMessage = BucketRequestGenerators.UpdateBucket(_options, operationalBucketId, options);
            var response       = await _client.SendAsync(requestMessage, cancelToken);

            return(await ResponseParser.ParseResponse <B2Bucket>(response));
        }
Example #3
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));
        }
Example #4
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;
            }

            var json = JsonSerialize(body);

            return(BaseRequestGenerator.PostRequest(Endpoints.Create, json, options));
        }
Example #5
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));
        }