1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

GCP Pub/Subのメッセージに「属性」を追加してみた

Last updated at Posted at 2021-09-04

概要

GCP Pub/Subのメッセージに「属性」を追加して Cloud Functions でそれを読み込めるかを試してみました。

Cloud Functionsのソース

import base64

def hello_pubsub(event, context):
    pubsub_message = base64.b64decode(event['data']).decode('utf-8')
    print("pubsub_message : ", pubsub_message)
    print(event)
    
    # 属性(test_val1)がセットされているかチェック
    if 'attributes' in event and event.get('attributes') is not None and 'test_val1' in event['attributes']:
        print("attribute.test_val1 : ", event['attributes']['test_val1'])
    else:
        print("test_val1 is not set.")

    # 属性(test_val2)がセットされているかチェック
    if 'attributes' in event and event.get('attributes') is not None and 'test_val2' in event['attributes']:
        print("attribute.test_val2 : ", event['attributes']['test_val2'])
    else:
        print("test_val2 is not set.")

pub/subのメッセージのパブリッシュ

image.png

実行結果

image.png

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?