pubsub publisher retry settings failing with: TypeError: All attributes being published to Pub/Sub must be sent as text strings

pubsub publisher retry settings failing with: TypeError: All attributes being published to Pub/Sub must be sent as text strings

我有一个云函数,在发布者客户端上使用 pubsub 发布者重试设置或发布请求时出错。 https://cloud.google.com/pubsub/docs/samples/pubsub-publisher-retry-settings#pubsub_publisher_retry_settings-python

当我 运行 我在 JupterLab 中的代码时 python 代码 运行 成功但是当我将代码移动到 Cloud Functions 时我得到一个 TypeError: All attributes being published至 Pub/Sub 必须作为文本字符串发送。

我现在已经尝试了一个新的简单的 Cloud Function,直接从上面的例子中复制代码 link 但仍然得到同样的错误,非常感谢任何建议。

from google.cloud import pubsub_v1

# TODO(developer)
GCP_PROJECT_ID='test_project'
SIT_EVENT_TOPIC = ('test_project1')
topic_id=SIT_EVENT_TOPIC
project_id=GCP_PROJECT_ID
# project_id = "your-project-id"
# topic_id = "your-topic-id"

# Configure the retry settings. Defaults shown in comments are values applied
# by the library by default, instead of default values in the Retry object.
def test():
    custom_retry = api_core.retry.Retry(
        initial=0.250,  # seconds (default: 0.1)
        maximum=90.0,  # seconds (default: 60.0)
        multiplier=1.45,  # default: 1.3
        deadline=300.0,  # seconds (default: 60.0)
        predicate=api_core.retry.if_exception_type(
            api_core.exceptions.Aborted,
            api_core.exceptions.DeadlineExceeded,
            api_core.exceptions.InternalServerError,
            api_core.exceptions.ResourceExhausted,
            api_core.exceptions.ServiceUnavailable,
            api_core.exceptions.Unknown,
            api_core.exceptions.Cancelled,
        ),
    )

    publisher = pubsub_v1.PublisherClient()
    topic_path = publisher.topic_path(project_id, topic_id)

    # for n in range(1, 10):
    #     data = "Message number {}".format(n)
    data="test message"
    # Data must be a bytestring
    data = data.encode("utf-8")
    print(type(data))
    future = publisher.publish(topic=topic_path, data=data, retry=custom_retry)
    print(future.result())

    print(f"Published messages with retry settings to {topic_path}.")


def main(event, context):
    """
    Call the main function, sets the order in which to run functions.
    """
    test()

    return 'Script has run without errors !!'

if (__name__ == "__main__"):
    main()

输出

ine 40, in test future = publisher.publish(topic=topic_path, data=data, retry=custom_retry) File "/layers/google.python.pip/pip/lib/python3.8/site-packages/google/cloud/pubsub_v1/publisher/client.py", line 195, in publish raise TypeError( TypeError: All attributes being published to Pub/Sub must be sent as text strings.

此问题是由 运行 在 requirements.txt 文件中使用过时的 python 包引起的。

原始要求列表

google-cloud-pubsub==0.34.0

google-云存储==1.13.1

google-cloud-bigquery==1.8.1

google-云核心==0.29.1

ndjson==0.3.1

新要求列表

google-cloud-pubsub==2.2.0

google-cloud-core==1.5.0

google-api-核心==1.24.1

google-云存储==1.35.0

google-cloud-bigquery==2.6.2

ndjson==0.3.1

更新列表允许云函数运行,我在示例代码中发现的另一个问题是您需要将数据转换为字符串,我在发布之前使用了以下内容:

数据=海峡(数据)