【问题标题】:How to convert Image URL (image stored in GCS) to binary for uploading image to facebook graph API如何将图像 URL(存储在 GCS 中的图像)转换为二进制文件以将图像上传到 facebook 图形 API
【发布时间】:2022-01-22 16:01:37
【问题描述】:

我正在尝试在 Go 中使用 facebook graph API 更新 WhatsApp 个人资料图片。由于图形 API 只接受二进制的图像文件,而我的图像文件已经上传到 google 存储桶中,我需要将文件转换为二进制。

当我尝试通过创建 MediaURL 从 GCS 读取文件时,下面的代码返回错误。它说“没有这样的文件或目录”。网址在浏览器中打开。

bytes, err := ioutil.ReadFile(gcsUrl)

【问题讨论】:

    标签: go facebook-graph-api file-upload google-cloud-storage whatsapp


    【解决方案1】:

    您遇到了错误,因为它尝试解析本地路径,而不是远程路径

    首先,你需要拉取图片

    resp, err := http.Get(gcsUrl)
       if err != nil {
            return "", fmt.Errorf("GET error: %v", err)
        }
    defer resp.Body.Close()
    

    那么你可以读取该数据data, err := ioutil.ReadAll(resp.Body)

    ReadAll 接受io.Reader 类型的数据,resp.Body 与之兼容

    【讨论】:

    猜你喜欢
    • 2013-08-13
    • 2011-08-05
    • 2012-05-26
    • 2012-12-15
    • 2017-08-21
    • 1970-01-01
    • 2014-11-12
    • 2019-07-24
    • 2020-03-06
    相关资源
    最近更新 更多