using System.Runtime.Serialization;
using System.Security.Cryptography;
using System.Text.RegularExpressions;
public static void Main()
var url = "https://i-ngoisao.vnecdn.net/2020/02/06/quynh-nga-1580947647-6340-1580948393_r_460x0.jpg";
Console.WriteLine(KeyForUrl(url));
Console.WriteLine(PartitionKeyForId(KeyForUrl(url)));
private static readonly HashAlgorithm HASH_PROVIDER = MD5.Create();
public static string PartitionKeyForId(string id)
byte[] hash = HASH_PROVIDER.ComputeHash(Encoding.UTF8.GetBytes(id));
return string.Join("", hash.Take(2).Select(x => x.ToString("x2")));
private static Regex KeyValidator = new Regex(@"[^a-zA-Z0-9\-_\.]");
public static string KeyForUrl(string url)
var key = uri.Host + KeyValidator.Replace(uri.PathAndQuery, "_");
return key.Substring(0, Math.Min(key.Length, 254));