简体   繁体   English

AWT IoT 规则到 S3

[英]AWT IoT rule to S3

I have created a rule to send the incoming IoT messages to a S3 bucket.我创建了一个规则来将传入的 IoT 消息发送到 S3 存储桶。

The problem is that any time IoT recieves a messages is sended and stored in a new file (with the same name) in S3.问题是,任何时候 IoT 收到消息都会发送并存储在 S3 中的新文件(同名)中。

I want this S3 file to keep all the data from before and not truncate each time a new message is stored.我希望这个 S3 文件保留之前的所有数据,而不是在每次存储新消息时都被截断。

How can I do that?我怎样才能做到这一点?

You can't do this with the S3 IoT Rule Action.您无法使用 S3 IoT 规则操作执行此操作。 You can get similar results using AWS Firehose, which will batch up several messages and write to one file.您可以使用 AWS Firehose 获得类似的结果,它将多条消息批处理并写入一个文件。 You will still end up with multiple files though.尽管如此,您仍然会得到多个文件。

You can do it using AWS IoT SQL variable expressions.您可以使用 AWS IoT SQL 变量表达式来实现。 For example use following as a key ${newuuid()} .例如使用以下作为键${newuuid()} This will create new s3 object for each message received.这将为收到的每条消息创建新的 s3 对象。

See more about SQL Functions https://docs.aws.amazon.com/iot/latest/developerguide/iot-sql-functions.html查看更多关于 SQL 函数的信息https://docs.aws.amazon.com/iot/latest/developerguide/iot-sql-functions.html

示例键

When you set up IoT S3 rule action , you need to specify a bucket and a key.当您设置IoT S3 规则操作时,您需要指定一个存储桶和一个密钥。 The key is what we might think of as a "path and file name".关键是我们可能认为的“路径和文件名”。 As the docs say , can specify the key string by using a substitution template , which is just a fancy way of saying "build a path out of these pieces of information". 正如文档所说,可以使用替换模板指定密钥字符串,这只是“从这些信息中构建路径”的一种奇特说法。 When you are building your substitution template, you can reference fields inside the message as well as use use a bunch of other functions在构建替换模板时,您可以引用消息中的字段以及使用一系列其他函数

Especially look at the functions topic , timestamp , as well as some of the string manipulator functions.特别是查看函数topictimestamp以及一些字符串操纵器函数。

Let's say your topic names are something like things/thing-id-xyz/location and you just want to store each incoming JSON message in a "folder" for the thing-id it came in from.假设您的主题名称类似于things/thing-id-xyz/location ,您只想将每个传入的 JSON 消息存储在一个“文件夹”中,以获取它来自的 thing-id。 You might specify a key like:您可以指定一个键,例如:

${topic(2)}/${timestamp()).json

it would evaluate to something like:它会评估为:

thing-id-xyz/1481825251155.json

where the timestamp part is the time the message came in. That will be different for each message, and then the messages would not overwrite each other.其中时间戳部分是消息传入的时间。每条消息都不同,这样消息就不会相互覆盖。

You can also specify parts of the message itself.您还可以指定消息本身的部分内容。 Let's imagine our incoming messages look something like this:让我们想象一下我们的传入消息看起来像这样:

{
  "time": "2022-01-13T10:04:03Z",
  "latitude": 40.803274,
  "longitude": -74.237926,
  "note": "Great view!"
}

Let's say you want to use the nice ISO date value you have in your data instead of the timestamp of the file.假设您想使用数据中的 ISO 日期值而不是文件的时间戳。 You could reference the time field no problem, like:您可以毫无问题地引用time字段,例如:

${topic(2)//${time}.json

Now the file would be written as the key:现在文件将被写为密钥:

thing-id-xyz/2022-01-13T10:04:03Z.json

You should be able to find some combination of values that works for your needs, and that most importantly, is UNIQUE for each message so they don't overwrite each other in S3.您应该能够找到适合您需要的一些值组合,最重要的是,对于每条消息都是唯一的,因此它们不会在 S3 中相互覆盖。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM