# #google-cloud-platform #google-cloud-functions #google-cloud-pubsub #google-cloud-python
Вопрос:
Я написал облачную функцию Python и хотел бы получить атрибут «delivery_attempt«.
Основываясь на документации, Облачная функция получает только 2 параметра: событие (типа PubsubMessage) и контекст.
def hello_pubsub(event, context):
"""Background Cloud Function to be triggered by Pub/Sub.
Args:
event (dict): The dictionary with data specific to this type of
event. The `@type` field maps to
`type.googleapis.com/google.pubsub.v1.PubsubMessage`.
The `data` field maps to the PubsubMessage data
in a base64-encoded string. The `attributes` field maps
to the PubsubMessage attributes if any is present.
context (google.cloud.functions.Context): Metadata of triggering event
including `event_id` which maps to the PubsubMessage
messageId, `timestamp` which maps to the PubsubMessage
publishTime, `event_type` which maps to
`google.pubsub.topic.publish`, and `resource` which is
a dictionary that describes the service API endpoint
pubsub.googleapis.com, the triggering topic's name, and
the triggering event type
`type.googleapis.com/google.pubsub.v1.PubsubMessage`.
Returns:
None. The output is written to Cloud Logging.
"""
Как я могу получить delivery_attempt для сообщения?
Ответ №1:
При запуске облачной функции через Pub/Sub у пользователя нет доступа к полю «Попытка доставки». Вместо этого способ получить доступ-настроить облачную функцию на основе HTTP и использовать URL-адрес триггера в качестве конечной точки push в подписке, которую вы создаете отдельно.
Затем вы можете получить доступ к попытке доставки следующим образом:
def hello_world(request):
"""Responds to any HTTP request.
Args:
request (flask.Request): HTTP request object.
Returns:
The response text or any set of values that can be turned into a
Response object using
`make_response <http://flask.pocoo.org/docs/1.0/api/#flask.Flask.make_response>`.
"""
request_json = request.get_json()
print(request_json["deliveryAttempt"])
Комментарии:
1. Есть ли какое-либо намерение добавить эту возможность в функции, запускаемые PubSub? На самом деле не очень удобно преобразовывать функции в http-запросы, чтобы иметь возможность извлекать это поле.