如何从云函数中提取 pub/sub 消息?
How can I pull pub/sub messages from a cloud function?
我想使用 Cloud Functions 从 pub/sub 中提取消息。所以我设置了一个主题的拉式订阅。然后我每小时用 Cloud Scheduler 触发 Cloud Function。
- 如果我需要使用pull,有没有更好的方法?
我从文档中复制了一个示例,但它引发了错误。
TypeError: match error: could not instantiate a path template from
projects/projectname/subscriptions/projects/projectname/subscriptions/test at PathTemplate.match
(/srv/node_modules/google-gax/build/src/pathTemplate.js:82:19) at PathTemplate.render
(/srv/node_modules/google-gax/build/src/pathTemplate.js:118:14) at SubscriberClient.subscriptionPath
(/srv/node_modules/@google-cloud/pubsub/build/src/v1/subscriber_client.js:1838:57) at
exports.helloPubSub (/srv/index.js:22:45) at /worker/worker.js:825:24 at <anonymous> at
process._tickDomainCallback (internal/process/next_tick.js:229:7)
- 我错过了什么导致了错误?
我复制的代码如下所示:
const pubsub = require('@google-cloud/pubsub');
const subClient = new pubsub.v1.SubscriberClient();
const subscriptionName = 'name_of_my_subscription';
const projectId = 'my_project_id';
const timeout = 60;
exports.helloPubSub = async (event, context) => {
const formattedSubscription = subClient.subscriptionPath(projectId, subscriptionName);
const request = {
subscription: formattedSubscription,
maxMessages: 10,
};
const [response] = await subClient.pull(request);
const ackIds = [];
for (const message of response.receivedMessages) {
console.log(`Received message: ${message.message.data}`);
ackIds.push(message.ackId);
}
const ackRequest = {
subscription: formattedSubscription,
ackIds: ackIds,
};
await subClient.acknowledge(ackRequest);
};
谢谢!
此行出现错误消息:
const formattedSubscription = subClient.subscriptionPath(projectId, subscriptionName);
因为你的 'name_of_my_subscription' 是 projects/your-project/subscriptions/subname
而不是 subname
:
const subscriptionName = 'name_of_my_subscription';
这就是你得到双名的原因 projects/projectname/subscriptions/
:
could not instantiate a path template from
projects/projectname/subscriptions/projects/projectname/subscriptions/test
我想使用 Cloud Functions 从 pub/sub 中提取消息。所以我设置了一个主题的拉式订阅。然后我每小时用 Cloud Scheduler 触发 Cloud Function。
- 如果我需要使用pull,有没有更好的方法?
我从文档中复制了一个示例,但它引发了错误。
TypeError: match error: could not instantiate a path template from
projects/projectname/subscriptions/projects/projectname/subscriptions/test at PathTemplate.match
(/srv/node_modules/google-gax/build/src/pathTemplate.js:82:19) at PathTemplate.render
(/srv/node_modules/google-gax/build/src/pathTemplate.js:118:14) at SubscriberClient.subscriptionPath
(/srv/node_modules/@google-cloud/pubsub/build/src/v1/subscriber_client.js:1838:57) at
exports.helloPubSub (/srv/index.js:22:45) at /worker/worker.js:825:24 at <anonymous> at
process._tickDomainCallback (internal/process/next_tick.js:229:7)
- 我错过了什么导致了错误?
我复制的代码如下所示:
const pubsub = require('@google-cloud/pubsub');
const subClient = new pubsub.v1.SubscriberClient();
const subscriptionName = 'name_of_my_subscription';
const projectId = 'my_project_id';
const timeout = 60;
exports.helloPubSub = async (event, context) => {
const formattedSubscription = subClient.subscriptionPath(projectId, subscriptionName);
const request = {
subscription: formattedSubscription,
maxMessages: 10,
};
const [response] = await subClient.pull(request);
const ackIds = [];
for (const message of response.receivedMessages) {
console.log(`Received message: ${message.message.data}`);
ackIds.push(message.ackId);
}
const ackRequest = {
subscription: formattedSubscription,
ackIds: ackIds,
};
await subClient.acknowledge(ackRequest);
};
谢谢!
此行出现错误消息:
const formattedSubscription = subClient.subscriptionPath(projectId, subscriptionName);
因为你的 'name_of_my_subscription' 是 projects/your-project/subscriptions/subname
而不是 subname
:
const subscriptionName = 'name_of_my_subscription';
这就是你得到双名的原因 projects/projectname/subscriptions/
:
could not instantiate a path template from
projects/projectname/subscriptions/projects/projectname/subscriptions/test