【问题标题】:What is the maximum length of Azure blob metadata values?Azure blob 元数据值的最大长度是多少?
【发布时间】:2013-12-28 22:41:27
【问题描述】:

Windows Azure blob metadata 中值的最大大小是多少?

我可以看到web服务器会强加一个实用的upper limit of about 4k

【问题讨论】:

    标签: azure-blob-storage


    【解决方案1】:

    最大。根据此处的文档,元数据大小为 8K:https://docs.microsoft.com/en-us/rest/api/storageservices/Setting-and-Retrieving-Properties-and-Metadata-for-Blob-Resources

    • 元数据名称必须遵守 C# 标识符的命名规则。
    • 名称不区分大小写,但在设置或读取时不区分大小写。
    • 如果为一个资源提交了两个或多个同名的元数据标头,Blob 服务将返回状态代码 400(错误请求)。
    • 元数据由名称/值对组成。
    • 所有元数据对的总大小最大可达 8KB
    • 元数据名称/值对是有效的 HTTP 标头,因此它们遵守管理 HTTP 标头的所有限制。

    另外:

    这是您可以使用的客户端验证检查:

    static void ValidateMetadata( IEnumerable< KeyValuePair<String,String> > blobMetadata )
    {
        const int AZURE_MD_NAME_PREFIX_LENGTH = 10; // "x-ms-meta-"
    
        Int32 totalLength = 0;
        foreach( KeyValuePair<String,String> md in blobMetadata )
        {
            totalLength += AZURE_MD_NAME_PREFIX_LENGTH + md.Key.Length + m.Value.Length;
            
            if( !IsValidMetadataName( md.Key ) )
            {
                throw new ArgumentException( message: "Metadata name \"" + md.Key + "\" is invalid." );
            }
        
            if( md.Value.Any( c => !IsValidHttpHeaderValueChar( c ) ) || md.Value.Contains("\r\n") )
            {
                throw new ArgumentException( message: "Metadata value \"" + md.Value + "\" is invalid." );
            }
        }
    
        if( totalLength > 8192 )
        {
             throw new ArgumentException( message: "Total length of metadata names and values " + totalLength +" exceeds 8KiB limit." );
        }
    }
    
    private static Boolean IsValidMetadataName( String name )
    {
        // https://stackoverflow.com/questions/47687379/what-characters-are-allowed-in-http-header-values
    
        if( String.IsNullOrWhiteSpace( name ) ) return false;
    
        // The intersection of valid HTTP Header Names and C# Identifiers means:
        // * First character must be a letter.
        // * All other characters must be ASCII letters or digits.
        // * Underscores are technically legal, but many HTTP systems reject them: https://stackoverflow.com/questions/22856136/why-http-servers-forbid-underscores-in-http-header-names - this method disallows underscores to be safe, though in practice it will probably work fine.
    
        if( !Char.IsLetter( name[0] ) ) return false;
        
        foreach( Char c in name )
        {
            bool validChar = Char.IsLetterOrDigit( c ) && c < 127;
            if( !validChar ) return false;
        }
    
        return true;
    }
    
    private static Boolean IsValidHttpHeaderChar( Char c )
    {
        // Technically a quoted-string can contain almost any character ("quoted-string" in the HTTP spec), but it's unclear if Azure Blob storage supports that or not.
    
        bool isCtl  = ( 0 <= c && c <= 31 ) || ( c == 127 );
        if( isCtl ) return false;
        return true; // This method checks individual chars, so it cannot check for \r\n.
    }
    

    【讨论】:

    • 出于好奇,这是否包括名称和值之间的“:”之后的空格,以及标题条目之间的 CRLF 是否计算在内?
    猜你喜欢
    • 2011-10-09
    • 2011-08-12
    • 1970-01-01
    • 2010-11-20
    • 1970-01-01
    • 2011-01-23
    • 1970-01-01
    • 2017-07-07
    相关资源
    最近更新 更多