将字节数组传递到Azure存储队列



我使用Go包github.com/golang/protobuf/proto,需要将Protobuff排入存储队列。但根据库文档,Enqueue函数只将消息作为字符串。

  • 从go库:
*func (m MessagesURL) Enqueue(ctx context.Context, messageText string, visibilityTimeout time.Duration, timeToLive time.Duration) (*EnqueueMessageResponse, error)*

有没有办法将protobuff字节数组排入Azure存储队列?

库链接:https://pkg.go.dev/github.com/Azure/azure-storage-queue-go/azqueue#MessagesURL.Enqueue

Azure存储队列Go库似乎没有提供任何对字节数组(或Protobuf消息(的内置支持。

但是,您可以应用自己的序列化,并使用protojson包将Protobuf消息转换为例如JSON,然后将结果字符串传递给Enqueue方法:

import "google.golang.org/protobuf/encoding/protojson"
[...]
jsonBytes, _ := protojson.Marshal(protoMessage)
json := string(jsonBytes)
[...]
messagesURL.Enqueue(ctx, json, time.Second*0, time.Minute)

您可以使用Marshal函数将proto消息序列化到缓冲区中。https://pkg.go.dev/google.golang.org/protobuf/proto#Marshal

之后,您可以使用base64之类的东西创建消息的文本表示。

最新更新