我在 PowerShell (.NET Framework) 中开发了一个函数,用于从任何给定的 Azure Blob 存储中检索数据:到目前为止,一切顺利。
不幸的是,当涉及到验证用户的输入时,我不能依赖外部模块或库,例如 NameValidator Class适用于 .NET 的 Azure SDK。
尽管如此,文章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.
注意:就在问这个问题之前,我发现这个问题可以回答我自己已经解决的问题或使用上述类:
How to validate Azure storage blob names
顺便问一下,有人知道 PowerShell 使用哪种风格的正则表达式吗?
最佳答案
您需要使用
^(?!.{1025})/?[^/]*[^/.](?:/[^/]*[^/.]){0,253}$
^(?=.{1,1024}$)/?[^/]*[^/.](?:/[^/]*[^/.]){0,253}$
请参阅regex demo .
详细信息:
^
- 字符串开头(?=.{1,1024}$)
- 字符串应包含 1 到 1024 个字符(?!.{1025})
- 字符串包含的字符不能超过 1025 个/?
- 可选的/
[^/]*[^/.]
- 除/
之外的零个或多个字符,然后是/
以外的字符以及。
(?:/[^/]*[^/.]){0,253}
- 出现 0 到 253 次/
,后跟零个或多个除/
然后是/
和之外的字符。
$
- 字符串结尾。
关于regex - 用于用户输入验证的 Azure Blob 存储正则表达式模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72040722/