英文:
How to convert Image URL (image stored in GCS) to binary for uploading image to facebook graph API
问题
我正在尝试使用Go语言中的Facebook Graph API更新WhatsApp的个人资料图片。由于Graph API只接受二进制格式的图像文件,并且我的图像文件已经上传到Google Bucket中,所以我需要将文件转换为二进制格式。
当我尝试通过创建MediaURL从GCS中读取文件时,下面的代码返回错误。它说"没有这样的文件或目录"。该URL在浏览器中是可以打开的。
bytes, err := ioutil.ReadFile(gcsUrl)
英文:
I'm trying to update the WhatsApp profile picture using facebook graph API in Go. As the graph API only accepts the image file as binary, and my image file is already uploaded in google bucket, I need to convert the file to binary.
When I try to read file from GCS by creating MediaURL and below code returns error. It says "no such file or directory". The url is opened in the browser.
bytes, err := ioutil.ReadFile(gcsUrl)
答案1
得分: 2
你之所以出现错误,是因为它试图解析本地路径,而不是远程路径。
首先,你需要拉取镜像:
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
是兼容的。
英文:
You got an error because it tries to resolve the path on your local, not remote path
first, you need to pull the image
resp, err := http.Get(gcsUrl)
if err != nil {
return "", fmt.Errorf("GET error: %v", err)
}
defer resp.Body.Close()
then you can read that data data, err := ioutil.ReadAll(resp.Body)
ReadAll takes io.Reader
type data and resp.Body
is compatible to that
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论