为了账号安全,请及时绑定邮箱和手机立即绑定

将文件从 url 发送到 cloudflare 图像失败

将文件从 url 发送到 cloudflare 图像失败

Go
慕斯709654 2022-11-28 10:28:11
我正在使用 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。
查看完整描述

1 回答

?
慕森王

TA贡献1777条经验 获得超3个赞

好的,对于遇到此问题的其他人。我解决了。


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()

    b := &bytes.Buffer{}

    a := make([]byte, 4096)

    wr := multipart.NewWriter(b)

    part, err := wr.CreateFormFile("file", filename)

    if err != nil {

        return nil, errors.Wrap(err, "Couldn't create the form file")

    }

    _, err = io.CopyBuffer(part, r.Body, a)

    wr.Close()


    req, err := http.NewRequest("POST", cloudFlareUrl, bytes.NewReader(b.Bytes()))

    if err != nil {

        return nil, errors.Wrap(err, "Couldn't create the request")

    }

    // req.Header.Set("Content-Type", "multipart/form-data")

    req.Header.Set("Content-Type", wr.FormDataContentType())

    req.Header.Set("Authorization", cloudFlareAuth)


查看完整回答
反对 回复 2022-11-28
  • 1 回答
  • 0 关注
  • 214 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号