如何编码Azure存储表行键和分区键? [英] How can I encode Azure storage table row keys and partition keys?

查看:79
本文介绍了如何编码Azure存储表行键和分区键?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Azure存储表,并且有数据输入到其中带有斜杠的RowKey中.根据此MSDN页面,PartitionKey和RowKey中均不允许使用以下字符:

I'm using Azure storage tables and I have data going in to the RowKey that has slashes in it. According to this MSDN page, the following characters are disallowed in both the PartitionKey and RowKey:

  • 正斜杠(/)字符

  • The forward slash (/) character

反斜杠()字符

数字符号(#)

问号(?)字符

从U + 0000到U + 001F的控制字符,包括:

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

水平制表符(\ t)字符

The horizontal tab (\t) character

换行(\ n)字符

回车符(\ r)

从U + 007F到U + 009F的控制字符

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

我已经看到一些人使用URL编码来解决此问题.不幸的是,由此可能会产生一些故障,例如能够插入但无法删除某些实体.我还看到有些人使用base64编码,但这也可能包含不允许的字符.

I've seen some people use URL encoding to get around this. Unfortunately there's a few glitches that can arise from this, such as being able to insert but unable to delete certain entities. I've also seen some people use base64 encoding, however this also can contain disallowed characters.

如何有效地对RowKey进行编码,而不用碰到不允许的字符或滚动自己的编码?

How can I encode my RowKey efficiently without running in to disallowed characters, or rolling my own encoding?

推荐答案

已在2020年8月18日更新(新?)问题,在Azure搜索中带有"+"字符.有关背景,请参见下面来自@mladenb的评论.值得注意的是,所引用的文档页面并不排除'+'字符.

Updated 18-Aug-2020 for (new?) issue with '+' character in Azure Search. See comments from @mladenb below for background. Of note, the documentation page referenced does not exclude the '+' character.

当URL经过Base64编码时,Azure表存储键列中唯一无效的字符是正斜杠('/').若要解决此问题,只需将正斜杠字符替换为另一个字符,该字符既(1)在Azure Table Storage键列中有效,又(2)不是Base64字符.我发现的最常见示例(在其他答案中被引用)是用下划线(_)代替正斜杠('/').

When a URL is Base64 encoded, the only character that is invalid in an Azure Table Storage key column is the forward slash ('/'). To address this, simply replace the forward slash character with another character that is both (1) valid in an Azure Table Storage key column and (2) not a Base64 character. The most common example I have found (which is cited in other answers) is to replace the forward slash ('/') with the underscore ('_').

private static String EncodeUrlInKey(String url)
{
    var keyBytes = System.Text.Encoding.UTF8.GetBytes(url);
    var base64 = System.Convert.ToBase64String(keyBytes);
    return base64.Replace('/','_').Replace('+','-');
}

解码时,只需撤消替换的字符(首先!),然后Base64解码结果字符串.这就是全部.

When decoding, simply undo the replaced character (first!) and then Base64 decode the resulting string. That's all there is to it.

private static String DecodeUrlInKey(String encodedKey)
{
    var base64 = encodedKey.Replace('-','+').Replace('_', '/');
    byte[] bytes = System.Convert.FromBase64String(base64);
    return System.Text.Encoding.UTF8.GetString(bytes);
}

有人建议其他Base64字符也需要编码.根据 Azure表存储文档并非如此

Some people have suggested that other Base64 characters also need encoding. According to the Azure Table Storage docs this is not the case.

这篇关于如何编码Azure存储表行键和分区键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆