各大图床 API

百度

//百度識圖的接口
func UploadToBaidu(img []byte, imgInfo string) string {
    body := new(bytes.Buffer)
    w := multipart.NewWriter(body)
    contentType := w.FormDataContentType()
    name := utils.GetFileNameByMimeType(imgInfo)
    file, _ := w.CreateFormFile("Filedata", name)
    _, _ = file.Write(img)
    _ = w.WriteField("file", "multipart")
    _ = w.Close()
    req, _ := http.NewRequest("POST", "https://api.uomg.com/api/image.baidu", body)
    req.Header.Set("Content-Type", contentType)
    resp, _ := http.DefaultClient.Do(req)
    defer resp.Body.Close()
    data, _ := ioutil.ReadAll(resp.Body)
    baidu := bed.BaiduResp{}
    err := json.Unmarshal([]byte(string(data)), &baidu)
    if err != nil {
        logging.AppLogger.Error("Upload To Baidu fail", zap.Error(err))
        return ""
    }
    return string(baidu.ImgUrl)
}

奇虎360(Qihoo)

func UploadToQihoo(img []byte, imgInfo string, imgType string) string {
    url := "http://st.so.com/stu"
    name := utils.GetFileNameByMimeType(imgInfo)

    file := &utils.FormFile{
        Name:  name,
        Key:   "upload",
        Value: img,
        Type:  imgType,
    }
    var header map[string]string
    data := utils.FormPost(file, url, header)
    var re = regexp.MustCompile(`(?m)data-imgkey="(.*)"`)
    imgKey := re.FindAllStringSubmatch(data, -1)[0][1]
    url = "https://ps.ssl.qhmsg.com/" + imgKey
    return url
}

网易严选(NetEasy)

func UploadToNetEasy(img []byte, imgInfo string, imgType string) string {
    url := "http://you.163.com/xhr/file/upload.json"
    name := utils.GetFileNameByMimeType(imgInfo)

    file := &utils.FormFile{
        Name:  name,
        Key:   "file",
        Value: img,
        Type:  imgType,
    }
    var header map[string]string
    data := utils.FormPost(file, url, header)
    netEasy := bed.NetEasyResp{}

    _ = json.Unmarshal([]byte(data), &netEasy)
    return netEasy.Data[0]
}

京东(JD)

func UploadToJd(img []byte, imgInfo string, imgType string) string {
    url := "https://search.jd.com/image?op=upload"
    name := utils.GetFileNameByMimeType(imgInfo)

    file := &utils.FormFile{
        Name:  name,
        Key:   "file",
        Value: img,
        Type:  imgType,
    }
    var header map[string]string
    data := utils.FormPost(file, url, header)
    var pre = regexp.MustCompile(`(?m)ERROR`)

    if !pre.MatchString(data) {
        var re = regexp.MustCompile(`(?m)\("(.*)"\)`)
        imgFix := re.FindAllStringSubmatch(data, -1)[0][1]
        url = "https://img" + strconv.Itoa(rand.Intn(3)+11) + ".360buyimg.com/img/" + imgFix
        return url
    } else {
        return ""
    }

}

掘金(JueJin)

func UploadToJueJin(img []byte, imgInfo string, imgType string) string {
    url := "https://cdn-ms.juejin.im/v1/upload?bucket=gold-user-assets"
    name := utils.GetFileNameByMimeType(imgInfo)

    file := &utils.FormFile{
        Name:  name,
        Key:   "file",
        Value: img,
        Type:  imgType,
    }
    var header map[string]string
    data := utils.FormPost(file, url, header)
    juejin := bed.JueJinResp{}
    _ = json.Unmarshal([]byte(data), &juejin)

    //神奇三斷言 : )
    reJ, _ := juejin.D.(map[string]interface{})
    urls, _ := reJ["url"].(map[string]interface{})
    httpUrl, _ := urls["https"].(string)
    return httpUrl
}

阿里(Ali)

func UploadToAli(img []byte, imgInfo string, imgType string) string {
    url := "https://kfupload.alibaba.com/mupload"
    name := utils.GetFileNameByMimeType(imgInfo)

    file := &utils.FormFile{
        Name:  name,
        Key:   "file",
        Value: img,
        Type:  imgType,
    }
    //var header map[string]string
    data := utils.AliFormPost(file, url)
    ali := bed.AliResp{}
    _ = json.Unmarshal([]byte(data), &ali)
    return ali.Url
}

如无特别声明,该文章均为 现在网NowTime.cc)原创,转载请遵循 署名-非商业性使用 4.0 国际(CC BY-NC 4.0) 协议,即转载请注明文章来源。


标签: none

赞 (261)

添加新评论