简体   繁体   English

base64 aws 命令的编码和解码以检索某些字段

[英]base64 encode and decode of aws command to retrieve some fields

Could someone explain the below code please?有人可以解释下面的代码吗?

The below code is producing the expected results with username.下面的代码使用用户名产生了预期的结果。

for row in $(echo "${es_eh}" | jq -r '.Events[] | @base64'); do
    echo "${row}" | base64 --decode | jq -r '.Username'

I didn't understand the purpose of doing base64 encode and then doing decode of the same string inside loop to retrieve username.我不明白进行 base64 编码然后在循环内对相同字符串进行解码以检索用户名的目的。

This is not working when I remove base64 encode and decode.当我删除 base64 编码和解码时,这不起作用。

for row in $(echo "${es_eh}" | jq -r '.Events[]'); do
    echo "${row}"| jq -r '.Username'

es_eh contains the json output of the below aws command, es_eh 包含以下 aws 命令的 json output,

es_eh="$(aws cloudtrail --region us-east-1 lookup-events --lookup-attributes AttributeKey=EventSource,AttributeValue=route53.amazonaws.com --max-items 50 --start-time "${start_date}" --end-time  "${end_date}" --output json)"

Without the encoding, the output of the first jq is more than one row.没有编码,第一个jq的output不止一行。 The loop iterates over the lines and fails, as none of them contains a valid JSON. With the | @base64循环遍历这些行并失败,因为它们都不包含有效的 JSON | @base64 | @base64 , each subobject is encoded to a single row, inflated back to a full JSON object by base64 --decode . | @base64 ,每个子对象都被编码为一行,通过 base64 --decode 膨胀回完整的 JSON base64 --decode

To see the rows, try outputting $row before processing it.要查看行,请在处理之前尝试输出 $row。

When you use $( ) without quotes around it, the result gets split into "words", but the shell's definition of a "word" is almost never what you want (and certainly has nothing to do with the json entries you want it split into).当你使用不带引号的$( )时,结果会被拆分成“单词”,但 shell 对“单词”的定义几乎从来不是你想要的(当然与你想要它拆分的 json 条目无关进入)。 This sort of thing is why you should almost never use unquoted expansions.这种事情就是为什么你几乎不应该使用不带引号的扩展。

Converting the output entries to base64 makes them wordlike enough that shell word splitting actually does the right thing.将 output 条目转换为 base64 使它们足够像单词,以至于 shell 单词拆分实际上做了正确的事情。 But note: some base64 encoders split their output into lines, which would make each line be treated as a separate "word".但请注意:一些 base64 编码器将其 output 分成几行,这将使每一行都被视为一个单独的“单词”。 If jq 's base64 encoding did this, this code would fail catastrophically on large events.如果jq的 base64 编码执行此操作,则此代码将在大型事件上灾难性地失败。

Transforming the for loop into a while loop should fix the problem:for循环转换为while循环应该可以解决问题:

while read -r row; do
    echo "${row}" | jq -r '.Username' 
done < <(echo "${es_eh}" | jq -c -r '.Events[]')

Note that in the outer jq , I used option -c to put output in a single ine.请注意,在外部jq中,我使用选项-c将 output 放入单个 ine 中。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 base64 编码 io.Reader - base64 encode io.Reader Microsoft azure IoT Hub:在消息路由查询中将消息从 base64 格式解码为 8-utf - Microsoft azure IoT Hub: Decode a message from base64 format to 8-utf in message route query 如何使用 AWS Lambda [NodeJS] 将 base64 编码字符串作为图像上传到 S3 - How to upload a base64 encoding string as an image to S3 using AWS Lambda [NodeJS] 如何通过AWS api-gateway返回一个Base64编码的zip文件供下载 - How do I return a Base64 encoded zip file through AWS api-gateway for download 如何将 base64 图像 URL 上传到 Firebase 并检索其访问令牌? - How to upload a base64 image URL to Firebase and retrieve its access token? Django / Python - 将图像从 AWS S3 存储桶转换为 Base64? - Django / Python - Convert image from AWS S3 bucket to Base64? AWS API 网关始终生成由 base64 编码为 Lambda 的正文 - AWS API Gateway always generate body encoded by base64 into Lambda 通过 ApiGateway 下载二进制文件 (nosql db) 由于配置错误导致执行失败:无法对正文进行 base64 解码 - Download binary (nosql db) through ApiGateway gives Execution failed due to configuration error: Unable to base64 decode the body 如何使用 AWS SES 在 email 中将 base64 字符串显示为图像 - How to display base64 string as image in email, using AWS SES SSL:错误:0906D064:PEM 例程:PEM_read_bio:坏 base64 解码 - SSL: error:0906D064:PEM routines:PEM_read_bio:bad base64 decode
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM