Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/azure/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何验证Azure存储blob名称_C#_Azure_Azure Storage_Azure Blob Storage - Fatal编程技术网

C# 如何验证Azure存储blob名称

C# 如何验证Azure存储blob名称,c#,azure,azure-storage,azure-blob-storage,C#,Azure,Azure Storage,Azure Blob Storage,我正在实现一个API来将文件存储在Azure Blob存储中 我正在使用Microsoft库验证容器和blob名称 NameValidator.ValidateContainerName(containerName); NameValidator.ValidateBlobName(blobFullName); 然而,它返回的一些名称是有效的,尽管我知道它们不是有效的,当我尝试保存它们时,blob存储返回了一个400错误的请求,正如预期的那样。因此,除了为什么MS验证库不完整的问题之外,我如何在

我正在实现一个API来将文件存储在Azure Blob存储中

我正在使用Microsoft库验证容器和blob名称

NameValidator.ValidateContainerName(containerName);
NameValidator.ValidateBlobName(blobFullName);
然而,它返回的一些名称是有效的,尽管我知道它们不是有效的,当我尝试保存它们时,blob存储返回了一个400错误的请求,正如预期的那样。因此,除了为什么MS验证库不完整的问题之外,我如何在C#中执行其余的验证?具体地说,我现在在这方面失败了

一些ASCII或Unicode字符,如控制字符(0x00到 0x1F、\u0081等)

我有一个文件名为\u0081的文件。剩余的无效字符是什么。他们指给我们看ietf文档,然后说“某些”字符是不允许的?哪一个?只是所有的控制字符

为了清楚起见,这里是返回400的部分

CloudBlockBlob blob = documentContainer.GetBlockBlobReference(blobFullName);
                await blob.UploadFromStreamAsync(fileStream, token).ConfigureAwait(false);
谢谢你的帮助

更新:我添加了一些逻辑来检查控制字符。如果我能得到一些健壮的东西,我将发布一份针对微软验证代码的PR

if (blobFullName.ToCharArray().Any(c => Char.IsControl(c))) {
    throw new HissyFitException();  // or do other stuff to fail validation
}
一些ASCII或Unicode字符,如控制字符(0x00到0x1F、u0081等)


文档中没有“some”一词,您可以在站点上提出文档问题,并要求他们通过提供这些问题的完整列表来更新文档。

我现在编写了自己的实现,用于检查存储帐户名称、容器名称和blob名称的有效性。在我的实现中,必须以以下形式将整个名称传递给方法: 存储\帐户\名称/容器\名称/Blob \名称,例如。Gmystorageaccount/containername/this/is_a/blob.jpg blob名称可能包含“/”

//像这样使用:
公共静态bool IsBlobFileName有效(字符串blobFilename)
{
//例如,blobFilename=“mystorageaccount/containername/this/is\u a/blob.jpg
字符串storageAccount=GetStorageAccount(blobFilename);
string container=GetContainer(blobFilename);
字符串blobName=GetBlobFilename(blobFilename);
if(string.IsNullOrWhiteSpace(storageAccount)
||string.IsNullOrWhiteSpace(容器)
||string.IsNullOrWhiteSpace(blobName)
||!IsazureBlobFileName有效(blobFilename))
{
返回false;
}
返回true;
}
私有静态bool IsAzureBlobFilenameValid(字符串文件名)
{
if(filename.Count(c=>c=='/'))<2)
{
返回false;
}
var storageAccount=GetStorageAccount(文件名);
var container=GetContainer(文件名);
var blob=GetBlobFilename(文件名);
字符串模式帐户=@“^[a-z0-9]{3,24}$”;
字符串模式容器=@“^[a-z0-9-]{3,63}$”;
字符串patternBlob=@“^.{11024}$”;
if(!Regex.IsMatch(container,patternCainer)
||!Regex.IsMatch(storageAccount、PatternCount)
||!Regex.IsMatch(blob,patternBlob))
{
返回false;
}
返回true;
}
私有静态字符串GetStorageAccount(字符串文件)
{
int charLocation=file.IndexOf('/',StringComparison.Ordinal);
如果(位置>0)
{
返回file.Substring(0,charLocation);
}
返回字符串。空;
}
私有静态字符串GetContainer(字符串文件)
{
int charLocation=IndexOfSecond(文件“/”;
如果(位置>0)
{
返回file.Substring(0,charLocation);
}
返回字符串。空;
}
私有静态字符串GetBlobFilename(字符串文件)
{
int charLocation=IndexOfSecond(文件“/”;
如果(位置>0)
{
返回file.Substring(charLocation+1,file.Length-(charLocation+1));
}
返回字符串。空;
}
私有静态int IndexOfSecond(字符串theString,字符串toFind)
{
int first=字符串索引of(toFind);
if(first==-1)返回-1;
//通过刚过第一个事件开始查找“下一个”事件
返回字符串索引of(toFind,first+1);
}

谢谢!发布!对于那些在家玩游戏的人:
     // Use like this:

           public static bool IsBlobFilenameValid(string blobFilename)
           {
                // e. g. blobFilename = "mystorageaccount/containername/this/is_a/blob.jpg
                string storageAccount = GetStorageAccount(blobFilename);
                string container = GetContainer(blobFilename);
                string blobName = GetBlobFilename(blobFilename);

                if(string.IsNullOrWhiteSpace(storageAccount)
                    || string.IsNullOrWhiteSpace(container)
                    || string.IsNullOrWhiteSpace(blobName)
                    || !IsAzureBlobFilenameValid(blobFilename))
                {
                    return false;
                }
                return true;
        }

        private static bool IsAzureBlobFilenameValid(string filename)
        {
            if (filename.Count(c => c == '/') < 2)
            {
                return false;
            }
            var storageAccount = GetStorageAccount(filename);
            var container = GetContainer(filename);
            var blob = GetBlobFilename(filename);

            string patternAccount = @"^[a-z0-9]{3,24}$";
            string patternContainer = @"^[a-z0-9-]{3,63}$";
            string patternBlob = @"^.{1,1024}$";

            if (!Regex.IsMatch(container, patternContainer)
                || !Regex.IsMatch(storageAccount, patternAccount)
                || !Regex.IsMatch(blob, patternBlob))
            {
                return false;
            }
            return true;
        }

        private static string GetStorageAccount(string file)
        {

            int charLocation = file.IndexOf('/', StringComparison.Ordinal);
            if (charLocation > 0)
            {
                return file.Substring(0, charLocation);
            }
            return string.Empty;

        }

        private static string GetContainer(string file)
        {
            int charLocation = IndexOfSecond(file, "/");
            if (charLocation > 0)
            {
                return file.Substring(0, charLocation);
            }
            return string.Empty;
        }

        private static string GetBlobFilename(string file)
        {
            int charLocation = IndexOfSecond(file, "/");
            if (charLocation > 0)
            {
                return file.Substring(charLocation + 1, file.Length - (charLocation + 1));
            }
            return string.Empty;
        }

        private static int IndexOfSecond(string theString, string toFind)
        {
            int first = theString.IndexOf(toFind);

            if (first == -1) return -1;

            // Find the "next" occurrence by starting just past the first
            return theString.IndexOf(toFind, first + 1);
        }