使用 AWS cli 如何获取具有特定 name/comment 的一条记录的 CloudFront OAI ID?

With AWS cli how do I get the CloudFront OAI ID for one record with a specific name/comment?

在下面的输出中,我只想获取带有注释 Created for Nackle Shared CF in pprd.

的 CloudFront 原始访问 ID 的 ID

因此我希望输出为``E1P6ZIBDB6I6FZ```。

这是我目前用来获取所有内容的命令:

aws cloudfront list-cloud-front-origin-access-identities

我玩过 jq 和 grep,但无法将其拉出我想要的东西。我也尝试使用 --filter 但我认为它不适用于此 CloudFront 命令。

命令的输出如下:

{
    "CloudFrontOriginAccessIdentityList": {
        "Items": [
            {
                "Id": "E3IQ5CFYM436DX",
                "S3CanonicalUserId": "0301338e0173a8fa18a117f8234f7f30ddf87fcf45fcf70412a374414d592bee3eea9697f41d06b284617232122dd",
                "Comment": "Created for Nackle Shared CF in qa"
            },
            {
                "Id": "E1P6ZIBDB6I6FZ",
                "S3CanonicalUserId": "c21bc4e37661a49846db7dd6798b25a71ce81decb4f6403b5c6602da6e49f6a831ca3a0f682f87fc4b15285785b9c",
                "Comment": "Created for Nackle Shared CF in pprd"
            },
            {
                "Id": "EMLFRR2NYMLFT",
                "S3CanonicalUserId": "5123661394e931636d038d3370399d6cb110844505f4058aa2ee0aab21e52d09ced60fffa2fb438d2872366821eb1",
                "Comment": "Created for Nackle Shared CF in prod"
            },
            {
                "Id": "E1EXEX5YNA4N51",
                "S3CanonicalUserId": "e5c969d27e56434c73c3b7509ffa51c8a29cf690eeb1f3c85db8c6f85b09f5efb3c5d3f891ea5b9f6c5729af2f5c3",
                "Comment": "Do Not Want"
            },
            {
                "Id": "E1RDNKSTCZ8CTF",
                "S3CanonicalUserId": "d5a6931f306807e37ec245f87a4f8ef247fbc362ab016f9b02e5136029ce8b7a79e1d1d5e4fe4b751cf0fa66832ad",
                "Comment": "Also do not want"
            }
        ]
    }
}

我也刚刚尝试了 --query,我想我更接近了:

aws cloudfront list-cloud-front-origin-access-identities --query "CloudFrontOriginAccessIdentityList.Items[]"

给我:

[
    {
        "Id": "E3IQ5CFYM436DX",
        "S3CanonicalUserId": "57b0301338e0173a8fa18a117f8234f7f30ddf87fcf45fcf70412a374414d592bee3eea9697f41d06b284617232122dd",
        "Comment": "Created for Nackle Shared CF in qa"
    },
    {
        "Id": "E1P6ZIBDB6I6FZ",
        "S3CanonicalUserId": "7fbc21bc4e37661a49846db7dd6798b25a71ce81decb4f6403b5c6602da6e49f6a831ca3a0f682f87fc4b15285785b9c",
        "Comment": "Created for Nackle Shared CF in pprd"
    },
    {
        "Id": "EMLFRR2NYMLFT",
        "S3CanonicalUserId": "02d5123661394e931636d038d3370399d6cb110844505f4058aa2ee0aab21e52d09ced60fffa2fb438d2872366821eb1",
        "Comment": "Created for Nackle Shared CF in prod"
    },
    {
        "Id": "E1EXEX5YNA4N51",
        "S3CanonicalUserId": "49de5c969d27e56434c73c3b7509ffa51c8a29cf690eeb1f3c85db8c6f85b09f5efb3c5d3f891ea5b9f6c5729af2f5c3",
        "Comment": "Do Not Want"
    },
    {
        "Id": "E1RDNKSTCZ8CTF",
        "S3CanonicalUserId": "0a1d5a6931f306807e37ec245f87a4f8ef247fbc362ab016f9b02e5136029ce8b7a79e1d1d5e4fe4b751cf0fa66832ad",
        "Comment": "Also do not want"
    }
]

使用 .CloudFrontOriginAccessIdentityList.Items[] 导航到项目,然后使用 select 仅选择匹配的项目并输出您感兴趣的部分。使用 -r 标志将输出变为原始文本(而不是 JSON)。

… | jq -r '
    .CloudFrontOriginAccessIdentityList.Items[]
    | select(.Comment == "Created for Nackle Shared CF in pprd").Id
  '
E1P6ZIBDB6I6FZ

Demo

要将查询字符串导入过滤器,请使用 --arg 选项:

… | jq -r --arg q "Created for Nackle Shared CF in pprd" '
    .CloudFrontOriginAccessIdentityList.Items[]
    | select(.Comment == $q).Id
  '