我们有一个场景,我们需要在json文件的分发中替换CacheBehavior的TargetOriginID。我们需要将现有的 TargetOriginID 替换为新值。我已经尝试过使用以下jq命令,但没有接近
for targetoriginID in $(jq '(.CacheBehaviors.Items[].TargetOriginId)' distconfig.json);
do
echo "#######fetch the new value from change behaviour json file"#######
NewValue=$(jq -r "map(select(.targetoriginid == ""$targetoriginID""))[].targetorigindr" changebehaviour.json)
echo "#########replace value in dist config json file with new value from change behaviour###########"
jq -r '(.CacheBehaviors.Items[].TargetOriginId | select(. == "$targetoriginID")) = "$NewValue"' distconfig.json > "tmp" && mv "tmp" distconfig.json
{
"CachedMethods": {
"Quantity": 3,
"Items": [
"HEAD",
"GET",
"OPTIONS"
]
}
},
"SmoothStreaming": false,
"Compress": false,
"LambdaFunctionAssociations": {
"Quantity": 0
},
"FunctionAssociations": {
"Quantity": 0
},
"FieldLevelEncryptionId": "",
"ForwardedValues": {
"QueryString": true,
"Cookies": {
"Forward": "none"
},
"Headers": {
"Quantity": 9,
"Items": [
"Authorization",
"Origin",
"access-control-allow-credentials",
"expires",
"access-control-max-age",
"access-control-allow-headers",
"cache-control",
"access-control-allow-methods",
"pragma"
]
},
"QueryStringCacheKeys": {
"Quantity": 1,
"Items": [
"*"
]
}
},
"MinTTL": 0,
"DefaultTTL": 86400,
"MaxTTL": 31536000
},
"CacheBehaviors": {
"Quantity": 2,
"Items": [
{
"PathPattern": "jkl/*",
"TargetOriginId": "nkl/Prod",
"TrustedSigners": {
"Enabled": false,
"Quantity": 0
},
"TrustedKeyGroups": {
"Enabled": false,
"Quantity": 0
},
"ViewerProtocolPolicy": "redirect-to-https",
"AllowedMethods": {
"Quantity": 7,
"Items": [
"HEAD",
"DELETE",
"POST",
"GET",
"OPTIONS",
"PUT",
"PATCH"
],
"CachedMethods": {
"Quantity": 3,
"Items": [
"HEAD",
"GET",
"OPTIONS"
]
}
},
"SmoothStreaming": false,
"Compress": false,
"LambdaFunctionAssociations": {
"Quantity": 0
},
"FunctionAssociations": {
"Quantity": 0
},
"FieldLevelEncryptionId": "",
"ForwardedValues": {
"QueryString": true,
"Cookies": {
"Forward": "all"
},
"Headers": {
"Quantity": 9,
"Items": [
"Authorization",
"Origin",
"access-control-allow-credentials",
"access-control-max-age",
"access-control-allow-headers",
"cache-control",
"access-control-allow-methods",
"expirers",
"pragma"
]
},
"QueryStringCacheKeys": {
"Quantity": 1,
"Items": [
"*"
]
}
},
"MinTTL": 0,
"DefaultTTL": 86400,
"MaxTTL": 31536000
},
{
"PathPattern": "fgh/*",
"TargetOriginId":"xyz/Prod",
"TrustedSigners": {
"Enabled": false,
"Quantity": 0
},
"TrustedKeyGroups": {
"Enabled": false,
"Quantity": 0
},
"ViewerProtocolPolicy": "redirect-to-https",
"AllowedMethods": {
"Quantity": 7,
"Items": [
"HEAD",
"DELETE",
"POST",
"GET",
"OPTIONS",
"PUT",
"PATCH"
],
"CachedMethods": {
"Quantity": 3,
"Items": [
"HEAD",
"GET",
"OPTIONS"
]
}
},
"SmoothStreaming": false,
"Compress": false,
"LambdaFunctionAssociations": {
"Quantity": 0
},
"FunctionAssociations": {
"Quantity": 0
},
"FieldLevelEncryptionId": "",
"ForwardedValues": {
"QueryString": true,
"Cookies": {
"Forward": "none"
},
"Headers": {
"Quantity": 10,
"Items": [
"access-control-allow-origin",
"authorization",
"Origin",
"access-control-allow-credentials",
"access-control-max-age",
"access-control-allow-headers",
"cache-control",
"access-control-allow-methods",
"expirers",
"pragma"
]
},
"QueryStringCacheKeys": {
"Quantity": 1,
"Items": [
"*"
]
}
},
"MinTTL": 0,
"DefaultTTL": 0,
"MaxTTL": 0
}
]
}
寻找一种解决方案,以便在所有TargetOriginID的CacheBehavior中进行批量更改
提供带有一些假设的第二个答案。让我们从一个最小的示例输入开始:
{
"CacheBehaviors": {
"Quantity": 2,
"Items": [
{
"PathPattern": "jkl/*",
"TargetOriginId": [
"nkl",
"something else"
]
},
{
"PathPattern": "fgh/*",
"TargetOriginId": [
"xyz",
"abc"
]
}
]
},
"IsIPV6Enabled": true
}
假设您要定义一个映射"{old1: new1, old2: new2, old3: new3, ...}",以下程序可以工作并且易于扩展:
(.CacheBehaviors.Items[].TargetOriginId[]) |= ({
nkl: "wkl",
xyz: "123",
"something else": "is changed too",
"not found": "never replaced"
}[.] // .)
如果您希望将映射放在程序的顶部/开头,请将其绑定到变量:
{
nkl: "wkl",
xyz: "123",
"something else": "is changed too",
"not found": "rever replaced"
} as $mapping
| (.CacheBehaviors.Items[].TargetOriginId[]) |= ($mapping[.] // .)
运行上述程序后的输出:
{
"CacheBehaviors": {
"Quantity": 2,
"Items": [
{
"PathPattern": "jkl/*",
"TargetOriginId": [
"wkl",
"is changed too"
]
},
{
"PathPattern": "fgh/*",
"TargetOriginId": [
"123",
"abc"
]
}
]
},
"IsIPV6Enabled": true
}
这就是你所追求的吗?这将按键从对象/字典/映射中选择新值,如果未找到映射,则回退到当前值。
… |= ({ old: "new" }[.] // .)
映射本身可以作为参数从单独的文件提供给 jq:
jq --slurpfile mapping old_to_new_ids.json
'(.CacheBehaviors.Items[].TargetOriginId[]) |= ($mapping[0][.] // .)'
文件old_to_new_ids.json
的内容只是:
{
"nkl": "wkl",
"xyz": "123",
"something else": "is changed too",
"not found": "never replaced"
}
在撰写本文时,发布的 JSON 无效,因此尚不清楚确切需要什么,但以下内容是说明性的:
jq --arg newvalue xyz '
(.. | objects | select(has("TargetOriginId")) | .TargetOriginId) |= $newvalue
'
固定(即有效)并将 JSON 输入减少到必要的最小值(MRE 中的 M),即 50 行。
{
"CacheBehaviors": {
"Quantity": 2,
"Items": [
{
"PathPattern": "jkl/*",
"TargetOriginId": [
"nkl"
],
"TrustedSigners": {
"Enabled": false,
"Quantity": 0
},
"TrustedKeyGroups": {
"Enabled": false,
"Quantity": 0
},
"ViewerProtocolPolicy": "redirect-to-https"
},
{
"PathPattern": "fgh/*",
"TargetOriginId": [
"xyz"
],
"TrustedSigners": {
"Enabled": false,
"Quantity": 0
},
"TrustedKeyGroups": {
"Enabled": false,
"Quantity": 0
},
"ViewerProtocolPolicy": "redirect-to-https"
}
]
},
"CustomErrorResponses": {
"Quantity": 0
},
"Comment": "vvvv",
"Logging": {
"Enabled": true,
"IncludeCookies": false,
"Bucket": "abc.s3.amazonaws.com",
"Prefix": "std"
},
"WebACLId": "",
"HttpVersion": "http2",
"IsIPV6Enabled": true
}
您可能希望运行类似于以下内容的 jq 程序:
(.CacheBehaviors.Items[].TargetOriginId | select(. as $id | "nkl" | IN($id[]))) = ["wkl"]
它选择包含值"nkl"
的所有TargetOriginId
,并将列表更改为仅包含"wkl"
。
输出:
{
"CacheBehaviors": {
"Quantity": 2,
"Items": [
{
"PathPattern": "jkl/*",
"TargetOriginId": [
"wkl"
],
"TrustedSigners": {
"Enabled": false,
"Quantity": 0
},
"TrustedKeyGroups": {
"Enabled": false,
"Quantity": 0
},
"ViewerProtocolPolicy": "redirect-to-https"
},
{
"PathPattern": "fgh/*",
"TargetOriginId": [
"xyz"
],
"TrustedSigners": {
"Enabled": false,
"Quantity": 0
},
"TrustedKeyGroups": {
"Enabled": false,
"Quantity": 0
},
"ViewerProtocolPolicy": "redirect-to-https"
}
]
},
"CustomErrorResponses": {
"Quantity": 0
},
"Comment": "vvvv",
"Logging": {
"Enabled": true,
"IncludeCookies": false,
"Bucket": "abc.s3.amazonaws.com",
"Prefix": "std"
},
"WebACLId": "",
"HttpVersion": "http2",
"IsIPV6Enabled": true
}
从这个问题来看,目前还不清楚"TargetOriginId": ["nkl", "xyz"]
应该变得["wkl"]
还是["wkl", "xyz"]
。或者["nkl", "xyz"]
是否是匹配项,因为它也包含不同的值。也许您只想选择与完整TargetOriginId
数组匹配的那些?
如果您只想匹配具有单值目标源 ID 的项目,程序会变得简单一些:
(.CacheBehaviors.Items[].TargetOriginId | select(. == ["nkl"])) = ["wkl"]
如果您的目标源 ID 始终是一个数组,并且您想将"[abc,nkl,xyz]"更改为"[abc,REPLACEMENT,xyz]",则仅选择这些数组元素并为其分配新值。
新输入(据我了解,Q 对此非常模糊):
{
"CacheBehaviors": {
"Quantity": 2,
"Items": [
{
"PathPattern": "jkl/*",
"TargetOriginId": [
"nkl",
"something else"
]
},
{
"PathPattern": "fgh/*",
"TargetOriginId": [
"xyz",
"abc"
]
}
]
},
"IsIPV6Enabled": true
}
JQ:
(.CacheBehaviors.Items[].TargetOriginId[] | select(. == "nkl")) = "wkl"
输出:
{
"CacheBehaviors": {
"Quantity": 2,
"Items": [
{
"PathPattern": "jkl/*",
"TargetOriginId": [
"wkl",
"something else"
]
},
{
"PathPattern": "fgh/*",
"TargetOriginId": [
"xyz",
"abc"
]
}
]
},
"IsIPV6Enabled": true
}