javascript - 如何解析 Nodejs/javascript 中的 Azure Blob URI?

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

我需要在 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

最佳答案

您可以使用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('/')
    }
}

关于javascript - 如何解析 Nodejs/javascript 中的 Azure Blob URI?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50630199/

相关文章:

node.js - AWS Lambda 找不到我的模块

c# - 从 C# 运行时 Get-AzureAccount 未找到帐户

javascript - 将数据从 PHP 应用程序传递到 JS 的正确方法是什么?

javascript - moment.js 如何知道它的对象何时被序列化?

node.js - 如何使用 sequelize ORM 在 postgres 中定义外键约束?

node.js - 按 id 删除记录?

javascript - 是否可以仅使用 jqGrid 对来自服务器的数据进行编码?

javascript - 动态地向 JSON 子数组添加值?

powershell - 使用 powershell 将 Azure 网络安全组规则导出到 CSV

使用托管标识而不是客户端 key 进行 Azure 应用程序注册