用于用户输入验证的 Azure Blob 存储正则表达式模式

Azure blob storage regex pattern for user input validation

我在 PowerShell (.NET Framework) 中开发了一个函数来从任何给定的 Azure Blob 存储中检索数据:到目前为止,一切顺利。

当涉及到验证用户输入时,不幸的是,我不能依赖外部模块或库,例如 Azure SDK for .NET 的 NameValidator Class

尽管如此,文章 Naming and Referencing Containers, Blobs, and Metadata 详细介绍了命名规则,因此正则表达式模式可能会派上用场。

对于容器名称,我想出了这个,它似乎适合:

(?=^.{3,63}$)(?!.*--)[^-][a-z0-9-]*[^-]

容器名称

A container name must be a valid DNS name, conforming to the following naming rules:

  • Container names must start or end with a letter or number, and can contain only letters, numbers, and the dash (-) character.
  • Every dash (-) character must be immediately preceded and followed by a letter or number; consecutive dashes are not permitted in container names.
  • All letters in a container name must be lowercase.
  • Container names must be from 3 through 63 characters long.

对于 Blob 名称 但是我无法解决路径段的计数问题:

(?=^.{1,1024}$)(?<=^|\/)(\S*?)[^\.] (?=\/|$)

注意:Azure 存储模拟器已被弃用,因此超出范围。

Blob 名称

A blob name must conforming to the following naming rules:

  • A blob name can contain any combination of characters.
  • A blob name must be at least one character long and cannot be more than 1,024 characters long, for blobs in Azure Storage.
  • The Azure Storage emulator supports blob names up to 256 characters long. For more information, see Use the Azure storage emulator for development and testing.
  • Blob names are case-sensitive.
  • Reserved URL characters must be properly escaped.
  • The number of path segments comprising the blob name cannot exceed 254. A path segment is the string between consecutive delimiter characters (e.g., the forward slash '/') that corresponds to the name of a virtual directory.
  • Note Avoid blob names that end with a dot (.), a forward slash (/), or a sequence or combination of the two. No path segments should end with a dot (.).

The Blob service is based on a flat storage scheme, not a hierarchical scheme. However, you may specify a character or string delimiter within a blob name to create a virtual hierarchy. For example, the following list shows valid and unique blob names. Notice that a string can be valid as both a blob name and as a virtual directory name in the same container:

/a
/a.txt
/a/b
/a/b.txt

You can take advantage of the delimiter character when enumerating blobs.

注意:就在问这个问题之前,我发现这些可以回答我自己已经解决的问题或使用上述 class:

顺便问一下,有人知道 PowerShell 使用哪种风格的正则表达式吗?

你需要使用

^(?!.{1025})/?[^/]*[^/.](?:/[^/]*[^/.]){0,253}$
^(?=.{1,1024}$)/?[^/]*[^/.](?:/[^/]*[^/.]){0,253}$

参见regex demo

详情:

  • ^ - 字符串开头
  • (?=.{1,1024}$) - 字符串应包含 1 到 1024 个字符
  • (?!.{1025}) - 字符串不能超过 1025 个字符
  • /? - 一个可选的 /
  • [^/]*[^/.] - / 以外的零个或多个字符,然后是 /.
  • 以外的字符
  • (?:/[^/]*[^/.]){0,253} - / 出现 0 到 253 次,后跟 / 以外的零个或多个字符,然后是 / 和 [=19= 以外的字符]
  • $ - 字符串结尾。