简体   繁体   English

Google Cloud Storage 创建发布订阅通知

[英]Google Cloud Storage create pub sub notification

I am trying to create a pub/sub notification using PHP.我正在尝试使用 PHP 创建发布/订阅通知。 I have a project and a service account.我有一个项目和一个服务帐户。 My code looks like:我的代码看起来像:

use Google\Cloud\Core\Iam\PolicyBuilder;
use Google\Cloud\PubSub\PubSubClient;
use Google\Cloud\Storage\StorageClient;

self::$storage_client = new StorageClient(
                                    [
            'projectId' => "MY PROJECT ID",
            'keyFile'   => json_decode(file_get_contents("PATH TO MY KEYFILE", true)
                                    ]
                                 );

$pubSub = new PubSubClient(
   [ 'projectId' => "MY PROJECT ID"
);

$serviceAccountEmail = self::$storage_client->getServiceAccount();

$topicName = 'projects/MY PROJECT ID/topics/thumbnail-service-1';
$topic = $pubSub->topic( $topicName );

$iam = $topic->iam();

// --> Error happens here:
$updatedPolicy = (new PolicyBuilder( $iam->policy() ))
    ->addBinding('roles/pubsub.publisher', [ "serviceAccount:$serviceAccountEmail" ])
    ->result();

$iam->setPolicy( $updatedPolicy );

$notification = $bucket->createNotification( $topicName,
    ['event_types' => [
        'OBJECT_FINALIZE',
        'OBJECT_DELETE',
        ]
    ]
    );

I think I"m trying to create the topic named, but I get this error:我想我正在尝试创建名为的主题,但出现此错误:

exception 'Exception' with message 'exception 'Google\Cloud\Core\Exception\NotFoundException' with message '{
  "error": {
    "code": 404,
    "message": "Resource not found (resource=thumbnail-service-1).",
    "status": "NOT_FOUND"
  }
}

It shouldn't be trying to find the topic, I want to create it.它不应该试图找到主题,我想创建它。 What am I missing?我错过了什么?

The code you have presented isn't trying to create the Pub/Sub topic, it is trying to get an existing topic.您提供的代码不是尝试创建 Pub/Sub 主题,而是尝试获取现有主题。 If you want to create a topic, you need to call createTopic :如果要创建主题,则需要调用createTopic

use Google\Cloud\PubSub\PubSubClient;

$pubsub = new PubSubClient(['projectId' => $projectId]);
$topic = $pubsub->createTopic($topicName);

Once the topic is created, you should be able to call createNotification with the topic name.创建主题后,您应该能够使用主题名称调用createNotification

You are not authorizing the PubSub client:您未授权 PubSub 客户端:

Change this section of your code:更改代码的这一部分:

$pubSub = new PubSubClient(
   [ 'projectId' => "MY PROJECT ID" ]
);

To:至:

$pubSub = new PubSubClient(
   [ 'projectId' => "MY PROJECT ID",
   'keyFile'     => json_decode(file_get_contents("PATH TO MY KEYFILE", true) ]
);

Your code is missing closing array brackets ] .您的代码缺少右数组括号] I assume that this is a copy-paste typo as PHP would print an error in this case.我认为这是一个复制粘贴错字,因为在这种情况下 PHP 会打印错误。

Note: I prefer to use KeyFilePath instead of KeyFile .注意:我更喜欢使用KeyFilePath而不是KeyFile The code is easier to read.代码更容易阅读。

$pubSub = new PubSubClient([
   'projectId'   => "MY PROJECT ID",
   'keyFilePath' => "/path/to/service-account.json"
]);

Next, verify that the Topic Name actually exists.接下来,验证主题名称是否确实存在。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM