google-cloud-platformpublish-subscribegcloudbetasubscriptions

gcloud beta pubsub subscriptions pull format


Quick question: When I pull message from pubsub subscription via command line tool

gcloud beta pubsub subscriptions pull MY_SUB

I am getting a table with (all details and) data as string (already decoded) But i want to use it so i did:

gcloud beta pubsub subscriptions pull MY_SUB --format=json

Than i receive a json with (all details) but the data is encoded.

There is a way to get it parsed with formatting?

Example of publishing message:

gcloud beta  pubsub topics publish myTopic "Topic Message" --attribute=Ai=A,Bee=B

NO-FORMATTING_RETURN

  ─────────────┬─────────────────┬────────────────┬─────────────────────────

  ──────────────────────────────────────────────────────────────────────────
    ─────────────────────────────────────────────────────────────┐
    │     DATA    │    MESSAGE_ID   │   ATTRIBUTES   │                                                                             
    ACK_ID                                                                             
    │

  ├─────────────┼─────────────────┼────────────────┼────────────────────────

  ──────────────────────────────────────────────────────────────────────────
     ──────────────────────────────────────────────────────────────┤
     │ Topic Message │ 122122177601805 │ Ai=A Bee=B  │ ACK_ID... │

  └─────────────┴─────────────────┴────────────────┴────────────────────────

  ──────────────────────────────────────────────────────────────────────────
    ──────────────────────────────────────────────────────────────┘

FORMATTING

[
{
"ackId": "ACK_ID..",
"message": {
  "attributes": {
    "Ai": "A",
      "Bee": "B"
    },
     "data": "SGVsbG8gVG9waWM=",
     "messageId": "122121955409996",
     "publishTime": "2017-05-11T10:26:54.143Z"
    }
}
]

Solution

  • You are on the right track with the use of the --format argument, but you need to use projections in order to decode the data. In this case, you need to use the decode() projection. Here's how you can perform the same command with the same data except the message's data is base64 decoded.

    gcloud beta pubsub subscriptions pull MY_SUB --format="json(ackId, message.attributes, message.data.decode(\"base64\").decode(\"utf-8\"), message.messageId, message.publishTime)"
    
    [
      {
        "ackId": "QV5AEkw...D5-NTlF",
        "message": {
          "attributes": {
            "Ai": "A",
            "Bee": "B"
          },
          "data": "Topic Message",
          "messageId": "127236468931635",
          "publishTime": "2017-05-29T23:15:04.637Z"
        }
      }
    ]