我可以在不同的Google Cloud项目中使用Google Cloud Pub / Sub吗?

时间:2018-06-29 03:19:36

标签: google-cloud-platform google-cloud-functions google-cloud-pubsub

假设我在Google Cloud中拥有项目A,B和C,每个项目中都有多个云功能。

我想在项目C中设置发布/订阅,以便A和B中的功能可以订阅。

这可能吗?我需要设置具有自定义权限的某种服务帐户吗?

谢谢

2 个答案:

答案 0 :(得分:3)

在IAM页面上的订阅项目中,添加一个具有“发布/订阅管理员”角色的新成员,新成员的名称是来自发布项目的serviceaccount-email。 然后在发布项目中创建一个云功能,并在页面底部(在更多下方)为该功能分配相同的服务帐户。

这是云功能的节点示例:

const PubSub = require('@google-cloud/pubsub');
const pubsub = new PubSub();
const topic = pubsub.topic('projects/subscribing-project-name/topics/topic-to- 
publish-to');
const publisher = topic.publisher();

exports.helloWorld = (req, res) => {
const customAttributes = {
message: 'Hello'
};
publisher.publish(Buffer.from("Hello from another project"), customAttributes, 
(err) => {
if (err) {
  res.status(500).send(JSON.stringify({ success: false, error: err.message }));
  return;
}
res.status(200).send(JSON.stringify({ success: true, message: "Message sent to 
pubsub topic" }));
});
};

答案 1 :(得分:0)

如评论中所述,如果您设置了push subscription,则可以从另一个Cloud Functions中预订

对于publish to a Pub/Sub topic from another project,您应该在目标项目中为其服务帐户授予正确的权限。