首页 > 解决方案 > 如何将 blob 文件发布到 GCP Pub/Sub?

问题描述

目前,我正在使用 java 将消息和数据发布到 pub/sub。我想知道如何将 blob 文件发布到发布/订阅服务?

 @PostMapping(value = "/publish")
    public String publishMessage(@RequestBody MyAppGCPMessage message) {

        var attributes = singleFormatAttributes(message);
        var messagePub = PubsubMessage.newBuilder().putAllAttributes(attributes).build();
        repository.publish(messagePub);
        return "message published to google Pub/Sub successfully";
    }

    private Map<String, String> singleFormatAttributes(MyAppGCPMessage entity) {
        return Map.of(
            "id", entity.getId(),
            "name", entity.getName(),
            "email",entity.getEmailId()
        );
    }

标签: javagoogle-cloud-platformgoogle-cloud-pubsub

解决方案


您不能将 blob 文件发布到 pub/sub。您可以做的是将文件上传到云存储并设置通知到您选择的发布/订阅主题。设置一个云函数以响应事件而被触发。您将在事件数据中获得上传文件的位置。在 Cloud Function 中执行您想要执行的任何处理。


推荐阅读