如何接收发布时间
How to receive publishTime
我在 Go 中有一个 GC 函数,它是由 pubsub 推送订阅主题触发的。我正在尝试接收 publishTime,但它似乎没有填充。
规格说字符串(时间戳格式),但是,我的模型中的该字段是空的。除 publishTime 外,其他所有信息都已填充。
type PubSubMessageModel struct {
Data []byte `json:"data"`
Attributes map[string]string `json:"attributes"`
PublishTime string `json:"publishTime"`
}
只是尝试记录 属性;
log.Info().Msgf("Publish Time is - %s\n", m.PublishTime)
结果为空字符串:
Publish Time is -
有什么建议吗?
它没有很好的记录,但确实如此。
event structure is different 当您直接推送功能时,与具有 HTTP 触发功能的推送订阅相比。
Instead, you can access publishTime and messageId via the event ID and timestamp properties of the event metadata. This metadata is accessible via the context object that is passed to your function when it is invoked.
在background context description中,在Go中,有对使用(functions/metadata)获取上下文值的包的解释
这里是如何实现的
- 执行
go get cloud.google.com/go
- 在您的代码中使用此依赖项并从上下文中获取消息元数据
import (
"cloud.google.com/go/functions/metadata"
"context"
"log"
)
type PubSubMessage struct {
Data []byte `json:"data"`
}
// HelloPubSub consumes a Pub/Sub message.
func HelloPubSub(ctx context.Context, m PubSubMessage) error {
meta,err := metadata.FromContext(ctx)
if err != nil {
log.Println(err)
return err
}
log.Printf("publishTime, %s", meta.Timestamp)
log.Printf("messageId, %s", meta.EventID)
log.Printf("Type, %s", meta.EventType)
log.Printf("Resource, %s", meta.Resource)
return nil
}
我在 Go 中有一个 GC 函数,它是由 pubsub 推送订阅主题触发的。我正在尝试接收 publishTime,但它似乎没有填充。
规格说字符串(时间戳格式),但是,我的模型中的该字段是空的。除 publishTime 外,其他所有信息都已填充。
type PubSubMessageModel struct {
Data []byte `json:"data"`
Attributes map[string]string `json:"attributes"`
PublishTime string `json:"publishTime"`
}
只是尝试记录 属性;
log.Info().Msgf("Publish Time is - %s\n", m.PublishTime)
结果为空字符串:
Publish Time is -
有什么建议吗?
它没有很好的记录,但确实如此。
event structure is different 当您直接推送功能时,与具有 HTTP 触发功能的推送订阅相比。
Instead, you can access publishTime and messageId via the event ID and timestamp properties of the event metadata. This metadata is accessible via the context object that is passed to your function when it is invoked.
在background context description中,在Go中,有对使用(functions/metadata)获取上下文值的包的解释
这里是如何实现的
- 执行
go get cloud.google.com/go
- 在您的代码中使用此依赖项并从上下文中获取消息元数据
import (
"cloud.google.com/go/functions/metadata"
"context"
"log"
)
type PubSubMessage struct {
Data []byte `json:"data"`
}
// HelloPubSub consumes a Pub/Sub message.
func HelloPubSub(ctx context.Context, m PubSubMessage) error {
meta,err := metadata.FromContext(ctx)
if err != nil {
log.Println(err)
return err
}
log.Printf("publishTime, %s", meta.Timestamp)
log.Printf("messageId, %s", meta.EventID)
log.Printf("Type, %s", meta.EventType)
log.Printf("Resource, %s", meta.Resource)
return nil
}