Azure Table Storage RowKey restricted Character Patterns?

社会主义新天地 提交于 2019-11-30 02:49:56

The following characters are not allowed in PartitionKey and RowKey fields:

  • The forward slash (/) character
  • The backslash (\) character
  • The number sign (#) character
  • The question mark (?) character

Please refer to the following article for further information: http://msdn.microsoft.com/en-us/library/windowsazure/dd179338.aspx

Dogu Arslan

public static readonly Regex DisallowedCharsInTableKeys = new Regex(@"[\\\\#%+/?\u0000-\u001F\u007F-\u009F]");

Detection of Invalid Table Partition and Row Keys:

bool invalidKey = DisallowedCharsInTableKeys.IsMatch(tableKey);

Sanitizing the Invalid Partition or Row Key:

string sanitizedKey = DisallowedCharsInTableKeys.Replace(tableKey, disallowedCharReplacement);

At this stage you may also want to prefix the sanitized key (Partition Key or Row Key) with the hash of the original key to avoid false collisions of different invalid keys having the same sanitized value.

Do not use the string.GetHashCode() though since it may produce different hash code for the same string and shall not be used to identify uniqueness and shall not be persisted.

I use SHA256: https://msdn.microsoft.com/en-us/library/s02tk69a(v=vs.110).aspx

to create the byte array hash of the invalid key, convert the byte array to hex string and prefix the sanitized table key with that.

Also see related MSDN Documentation: https://msdn.microsoft.com/en-us/library/azure/dd179338.aspx

Related Section from the link: Characters Disallowed in Key Fields

The following characters are not allowed in values for the PartitionKey and RowKey properties:

The forward slash (/) character

The backslash (\) character

The number sign (#) character

The question mark (?) character

Control characters from U+0000 to U+001F, including:

  • The horizontal tab (\t) character

  • The linefeed (\n) character

  • The carriage return (\r) character

Control characters from U+007F to U+009F

Note that in addition to the mentioned chars in the MSDN article, I also added the % char to the pattern since I saw in a few places where people mention it being problematic. I guess some of this also depends on the language and the tech you are using to access the table storage.

If you detect additional problematic chars in your case, then you can add those to the regex pattern, nothing else needs to change.

I found that in addition to the characters listed in Igorek's answer, these also can cause problems (e.g. inserts will fail):

  • |
  • []
  • {}
  • <>
  • $^&

Tested with the Azure Node.js SDK.

I just found out (the hard way) that the '+' sign is allowed, but not possible to query in PartitionKey.

In addition to the above you can't use an underscore _ at the start of a partition key either, an error is returned that a key starting with an underscore is not a valid partition key.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!