【问题标题】:How can I parse Azure Blob URI in nodejs/javascript?如何在 nodejs/javascript 中解析 A​​zure Blob URI?
【发布时间】:2018-11-10 19:33:43
【问题描述】:

我需要在 nodejs 中解析 A​​zure Blob URI 并提取存储帐户名称、容器名称和 blob 名称。

我调查了azure-sdk-for-nodeazure-storage-node,但我没有找到这样做的方法。

如果 Blob URI 无效,我也想检测一下,所以正则表达式(如果可能)可能是一个好方法。

Blob URI 的一些示例:

  1. https://myaccount.blob.core.windows.net/mycontainer/myblob
  2. http://myaccount.blob.core.windows.net/myblob
  3. https://myaccount.blob.core.windows.net/$root/myblob

【问题讨论】:

    标签: javascript node.js azure azure-storage azure-blob-storage


    【解决方案1】:

    您可以使用url.parse

    我的主要原因是避免使用正则表达式,它也更容易理解、阅读和修改。

    这是一个示例代码:

    const url = require('url')
    
    const parseAzureBlobUri = (blobUrl) => {
        let uri = url.parse(blobUrl)
    
        // Extract the storage account name
        let storageAccountName = uri.hostname.split('.')[0]        
    
        // Remove the 1st trailing slash then extract segments
        let segments = uri.pathname.substring(1).split('/')
    
        // If only one segment, this is the blob name
        if(segments.length === 1){
            return {
                storageAccountName,
                containerName: '$root',
                blobName: segments[0]
            }
        }
    
        // get the container name
        let containerName = segments[0]
    
        // Remove the containername from the segments
        segments.shift()
    
        return {
            storageAccountName,
            containerName,
            blobName: segments.join('/')
        }
    }
    

    【讨论】:

    • 感谢不使用正则表达式的解决方案类型!不幸的是,这种解决方案对验证没有多大作用,我猜你会再次使用正则表达式。此外,就我个人而言,我发现使用正则表达式的解决方案更容易阅读和理解,但这是由于个人喜好以及我猜想使用正则表达式的舒适程度。
    • 是的,我想这是一种不同的方法,两者都是有效的
    【解决方案2】:

    通过关注specification from Azure,我想出了以下函数 (gist),它使用正则表达式来解析 blob uri,如果 blob uri 无效,它也会引发错误。

    存储帐户名称和容器名称应该完全正确/准确,只有 Blob 名称我留下了一些松散的名称,因为它定义起来更复杂。

    /**
     * Validates and parses given blob uri and returns storage account, 
     * container and blob names.
     * @param {string} blobUri - Valid Azure storage blob uri.
     * @returns {Object} With following properties:
     *   - {string} storageAccountName
     *   - {string} containerName
     *   - {string} blobName
     * @throws {Error} If blobUri is not valid blob uri.
     */
    const parseAzureBlobUri = (blobUri) => {
      const ERROR_MSG_GENERIC = 'Invalid blob uri.'
    
      const storageAccountRegex = new RegExp('[a-z0-9]{3,24}')
      const containerRegex = new RegExp('[a-z0-9](?!.*--)[a-z0-9-]{1,61}[a-z0-9]')
      const blobRegex = new RegExp('.{1,1024}')  // TODO: Consider making this one more precise.
      const blobUriRegex = new RegExp(
        `^http[s]?:\/\/(${ storageAccountRegex.source })\.blob.core.windows.net\/`
        + `(?:(\$root|(?:${ containerRegex.source }))\/)?(${ blobRegex.source })$`
      )
      const match = blobUriRegex.exec(blobUri)
      if (!match) throw Error(ERROR_MSG_GENERIC)
    
      return {
        storageAccountName: match[1],
        // If not specified, then it is implicitly root container with name $root.
        containerName: match[2] || '$root',
        blobName: match[3]
      }
    }
    

    【讨论】:

    • @Thomas 你介意详细说明一下吗?哪个 url 解析器,如何更好地解决这个问题?
    • nodejs 有默认的 url 解析器 (nodejs.org/docs/latest/api/url.html),因此您可以轻松获取主机名、路径等。您不必维护自己的代码。正则表达式也不是解析器,正则表达式的复杂性可能会产生成本(性能)
    • @Thomas 谢谢,我不知道 nodejs 的 url 解析器,但它似乎不符合我的需要。我想 1. 验证 blob uri 和 2. 从中提取存储帐户、容器和 blob 名称。我怎么能用 nodejs url 解析器做到这一点?也许我遗漏了一些东西 - 你介意提供一个例子吗?
    • 我已经添加了一个答案,希望它会有所帮助
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-10-27
    • 2012-11-17
    • 2016-02-25
    • 1970-01-01
    • 2021-06-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多