无法解析aws cli中的json字符串以将通知配置添加到s3存储桶

时间:2018-06-06 15:54:34

标签: json aws-cli

我正在尝试将SNS主题添加到s3存储桶中,并且我使用aws cli命令将通知配置应用于名为“test”的s3bucket

我将SNS主题配置作为json字符串传递,当我尝试打印json字符串时,它正确地打印json值,但不知何故aws cli为json字符串添加逗号。

inputevent.sh:

#!/bin/bash
bucketName=test
jsonInput=file:///Users/ish/GitLabProject/validator-cf/inputevent.json
QueueArn="arn:aws:sns:us-east-1:255353535355:SNSTopic"
template='{ "TopicConfigurations": { "Event": "s3:ObjectCreated:*", "Queue": "%s" } }'


TopicConfiguration=$(printf "$template" "$QueueArn")

echo "$TopicConfiguration"

aws s3api put-bucket-notification-configuration --bucket $bucketName --notification-configuration $TopicConfiguration

错误:

{ "TopicConfigurations": { "Event": "s3:ObjectCreated:*", "Queue": "arn:aws:sns:us-east-1:255353535355:SNSTopic" } }
usage: aws [options] <command> <subcommand> [<subcommand> ...] [parameters]
To see help text, you can run:

  aws help
  aws <command> help
  aws <command> <subcommand> help

Unknown options: {, "Event":, "s3:ObjectCreated:*",, "Queue":, "arn:aws:sns:us-east-1:255353535355:SNSTopic", }, }, "TopicConfigurations":

1 个答案:

答案 0 :(得分:1)

检查您的template变量。

根据docsTopicConfigurations中的--notification-configuration应该是一个数组(因为您可以有多个通知)

尝试将bash脚本中的template变量更新为

template='{ "TopicConfigurations": [{ "Event": "s3:ObjectCreated:*", "Queue": "%s" }] }'

可以查看examples以获得更好的主意