亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

將文件從 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 關注
  • 181 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號