我正在使用 Google App 引擎,这意味着只允许通过云存储写入文件。当 API 被点击时,我可以毫无问题地获取文件并将其存储在谷歌云存储中。该函数只返回保存它的 URL。我想获取该图像 URL,然后将其发送到 Cloudflare 图像,因为它们允许您创建变体。
type ImageResult struct {
Result struct {
ID string `json:"id"`
Filename string `json:"filename"`
Uploaded time.Time `json:"uploaded"`
RequireSignedURLs bool `json:"requireSignedURLs"`
Variants []string `json:"variants"`
} `json:"result"`
ResultInfo interface{} `json:"result_info"`
Success bool `json:"success"`
Errors []interface{} `json:"errors"`
Messages []interface{} `json:"messages"`
}
以上是表示 Cloudflare 响应的结构。下面是直接获取谷歌云存储 URL 并在将其发送到 Cloudflare 之前“下载”它的函数。
func CloudFlareURL(url, filename string) (*ImageResult, error) {
cloudFlareUrl := "https://api.cloudflare.com/client/v4/accounts/" + konsts.CloudFlareAcc + "/images/v1"
cloudFlareAuth := "Bearer " + konsts.CloudFlareApi
r, err := http.Get(url)
if err != nil {
return nil, errors.Wrap(err, "Couldn't get the file")
}
if r.StatusCode != 200 {
return nil, errors.New("Couldn't get the file")
}
defer r.Body.Close()
buff := make([]byte, 4096)
_, err = r.Body.Read(buff)
req, err := http.NewRequest("POST", cloudFlareUrl, bytes.NewReader(buff))
if err != nil {
return nil, errors.Wrap(err, "Couldn't create the request")
}
req.Header.Set("Content-Type", "multipart/form-data")
req.Header.Set("Authorization", cloudFlareAuth)
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return nil, errors.Wrap(err, "Couldn't send the request")
}
这是错误信息; 寻找值开头的无效字符“E”无法解组响应正文
现在在我的笔记本电脑上,如果我在发送文件后运行 api 服务器,我可以将它保存在磁盘上,打开它并毫无问题地发送到 cloudflare。
慕森王
相关分类