【问题标题】:Problem to get a connectionString for CosmosDB in Azure在 Azure 中获取 CosmosDB 的连接字符串的问题
【发布时间】:2020-03-24 15:18:23
【问题描述】:

我正在按照示例进行操作: https://github.com/Azure-Samples/java-functions-eventhub-cosmosdb

但是当我尝试使用 Azure CLI 获取 connectionString 时,我收到以下错误:

COSMOS_DB_CONNECTION_STRING=$( \
    az cosmosdb keys list \
        --resource-group $RESOURCE_GROUP \
        --name $COSMOS_DB_ACCOUNT \
        --type connection-strings \
        --query connectionStrings[0].connectionString \
        --output tsv)

来自az的错误信息:

no matches found: connectionStrings[0].connectionString

有什么帮助吗?

在此先感谢

az --version
azure-cli                          2.2.0

command-modules-nspkg              2.0.3
core                               2.2.0
nspkg                              3.0.4
telemetry                          1.0.4

Extensions:
azure-cli-iot-ext                  0.8.9
application-insights               0.1.4

Python location '/usr/local/Cellar/azure-cli/2.2.0_1/libexec/bin/python'
Extensions directory '/Users/juan.brena/.azure/cliextensions'

Python (Darwin) 3.8.2 (default, Mar 11 2020, 00:29:50) 
[Clang 11.0.0 (clang-1100.0.33.17)]

Legal docs and information: aka.ms/AzureCliLegal



Your CLI is up-to-date.

显示当前连接字符串的示例:

az cosmosdb keys list \                              
  --resource-group $RESOURCE_GROUP \
  --name $COSMOS_DB_ACCOUNT \   
  --type connection-strings  

输出:

{
  "connectionStrings": [
    {
      "connectionString": "AccountEndpoint=xxx",
      "description": "Primary SQL Connection String"
    },
    {
      "connectionString": "AccountEndpoint=xxx",
      "description": "Secondary SQL Connection String"
    },
    {
      "connectionString": "AccountEndpoint=xxx",
      "description": "Primary Read-Only SQL Connection String"
    },
    {
      "connectionString": "AccountEndpoint=xxx",
      "description": "Secondary Read-Only SQL Connection String"
    }
  ]
}

思路是获取第一个connectionString:

{
  "connectionString": "AccountEndpoint=xxx",
  "description": "Primary SQL Connection String"
}

【问题讨论】:

  • 我能够毫无问题地执行命令。您可以尝试在没有--query 参数的情况下运行您的命令,看看您是否得到任何输出?尝试运行以下命令:az cosmosdb keys list \ --resource-group $RESOURCE_GROUP \ --name $COSMOS_DB_ACCOUNT \ --type connection-strings
  • 它对我不起作用。您的命令运行良好,但问题出在查询上。
  • 你能编辑你的问题并包含输出吗?显然混淆了帐户名称/密钥:)。另外,您使用的是什么版本的 CLI?
  • 感谢您分享详细信息。这太奇怪了。我得到了相同的输出,并且查询对我来说很好。
  • 我完全同意你的看法,但我在 Azure CLI 安装中没有做任何不同的事情,但我的查询处理器返回了该错误。我明天会尝试重新安装 Azure CLI。

标签: azure azure-cosmosdb azure-cli


【解决方案1】:

试试这个。

RESOURCE_GROUP='myResourceGroup'
COSMOS_DB_ACCOUNT='my-cosmos-account'

COSMOS_DB_CONNECTION_STRING=$(az cosmosdb keys list \
        -g $RESOURCE_GROUP \
        -n $COSMOS_DB_ACCOUNT \
        --type connection-strings \
        --query connectionStrings[0].connectionString \
        --output tsv)

echo $COSMOS_DB_CONNECTION_STRING

【讨论】:

  • COSMOS_DB_CONNECTION_STRING=$(az cosmosdb 密钥列表\cmdsubst> -g $RESOURCE_GROUP\cmdsubst> -n $COSMOS_DB_ACCOUNT \cmdsubst> --type connection-strings\cmdsubst> --query connectionStrings[0]。 connectionString \ cmdsubst> --output tsv) zsh: 未找到匹配项:connectionStrings[0].connectionString
  • 可能与 Azure CLI 有关,就任何依赖项而言?
  • 嗯,不确定。我在 Microsoft Terminal 中使用了 CloudShell,它运行良好。
  • 是的,不确定。在 Ubuntu 18.04 中尝试过同样的事情并且在那里也可以正常工作。
  • 我刚刚使用 Azure CLI Docker 镜像进行了测试,行为正常。结论:我的 OSX 有问题。感谢支持
【解决方案2】:

我在我的 ubuntu 开发容器中遇到了同样的问题,并且能够通过将查询包装在 单引号 中来解决它:

az cosmosdb keys list \
  --name $ACCOUNTNAME \
  --resource-group $RESOURCEGROUP \
  --type connection-strings \
  --query 'connectionStrings[0].connectionString' \
  --output tsv

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-07-30
    • 1970-01-01
    • 2022-01-14
    • 2017-09-29
    • 2017-08-22
    • 2011-08-23
    相关资源
    最近更新 更多