我需要在 Nodejs 中解析 Azure Blob URI 并提取存储帐户名称、容器名称和 Blob 名称。
我调查了 azure-sdk-for-node和 azure-storage-node但我没有找到这样做的方法。
如果 Blob URI 无效,我也想检测到这一点,所以正则表达式(如果可能的话)可能是一个好方法。
Blob URI 的一些示例:
https://myaccount.blob.core.windows.net/mycontainer/myblob
http://myaccount.blob.core.windows.net/myblob
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/