Amazon.S3.Util.AmazonS3Util.ValidateV2Bucket C# (CSharp) Метод

ValidateV2Bucket() публичный статический Метод

Version2 S3 buckets adhere to RFC 1035: Less than 255 characters, with each label less than 63 characters. Label must start with a letter Label must end with a letter or digit Label can have a string of letter, digits and hyphens in the middle. Although names can be case-sensitive, no significance is attached to the case. RFC 1123: Allow label to start with letter or digit (e.g. 3ware.com works) RFC 2181: No restrictions apart from the length restrictions. S3 V2 will start with RFCs 1035 and 1123 and impose the following additional restrictions: Length between 3 and 63 characters (to allow headroom for upper-level domains, as well as to avoid separate length restrictions for bucket-name and its labels Only lower-case to avoid user confusion. No dotted-decimal IPv4-like strings
public static ValidateV2Bucket ( string bucketName ) : bool
bucketName string The BucketName to validate if V2 addressing should be used
Результат bool
        public static bool ValidateV2Bucket(string bucketName)
        {
            if (String.IsNullOrEmpty(bucketName))
            {
                throw new ArgumentNullException("bucketName", "Please specify a bucket name");
            }

            if (bucketName.StartsWith("s3.amazonaws.com", StringComparison.Ordinal))
            {
                return false;
            }

            // If the entire S3 URL is passed instead of just the bucketName, 
            // strip out the Amazon S3 part of the URL
            int idx = bucketName.IndexOf(".s3.amazonaws.com", StringComparison.Ordinal);
            if (idx > 0)
            {
                bucketName = bucketName.Substring(0, idx);
            }

            if (bucketName.Length < S3Constants.MinBucketLength ||
                 bucketName.Length > S3Constants.MaxBucketLength ||
                 bucketName.StartsWith(".", StringComparison.Ordinal) ||
                 bucketName.EndsWith(".", StringComparison.Ordinal))
            {
                return false;
            }

            // Check not IPv4-like
            Regex ipv4 = new Regex("^[0-9]+\\.[0-9]+\\.[0-9]+\\.[0-9]+$");
            if (ipv4.IsMatch(bucketName))
            {
                return false;
            }

            // Check each label
            Regex v2Regex = new Regex("^[a-z0-9]([a-z0-9\\-]*[a-z0-9])?$");
            string[] labels = bucketName.Split("\\.".ToCharArray());
            foreach (string label in labels)
            {
                if (!v2Regex.IsMatch(label))
                {
                    return false;
                }
            }
            return true;
        }