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;
}