Exemple #1
0
func (vc *VideoCloud) Upload(userid string, filename string, title string, desc string, magicContext string) (info VideoUrlInfo, err error) {
	if "" == filename {
		err = errors.New("invliad filename")
		return
	}
	reqUrl := fmt.Sprintf("http://%s/%d/%s", QCLOUD_VIDEO_DOMAIN, vc.Appid, userid)
	boundary := "-------------------------abcdefg1234567"
	expire := uint(3600)

	sign, err := sign.AppSign(vc.Appid, vc.SecretId, vc.SecretKey, expire, userid)
	if nil != err {
		return
	}

	bodyBuf := &bytes.Buffer{}
	bodyWriter := multipart.NewWriter(bodyBuf)
	bodyWriter.SetBoundary(boundary)
	fileWriter, err := bodyWriter.CreateFormFile("FileContent", filename)
	if nil != err {
		return
	}
	fh, err := os.Open(filename)
	if nil != err {
		return
	}
	_, err = io.Copy(fileWriter, fh)
	if nil != err {
		return
	}
	//write desc
	bodyWriter.WriteField("desc", desc)
	bodyWriter.WriteField("magicContext", magicContext)
	bodyWriter.WriteField("title", title)

	bodyWriter.Close()

	req, err := http.NewRequest("POST", reqUrl, bodyBuf)
	if nil != err {
		return
	}
	req.Header.Set("HOST", "web.video.myqcloud.com")
	req.Header.Set("user-agent", "qcloud-go-sdk")
	req.Header.Set("Authorization", sign)
	req.Header.Set("Content-Type", "multipart/form-data; boundary="+boundary)

	var client http.Client
	resp, err := client.Do(req)
	defer resp.Body.Close()
	if nil != err {
		return
	}

	data, err := ioutil.ReadAll(resp.Body)
	if nil != err {
		return
	}

	code, message, js, err := vc.parseRsp(data)
	if nil != err {
		return
	}
	if code != 0 {
		desc := fmt.Sprintf("rsp error, code=%d, message=%s", code, message)
		err = errors.New(desc)
		return
	}

	info.Url, _ = js.Get("data").Get("url").String()
	info.DownloadUrl, _ = js.Get("data").Get("download_url").String()
	info.Fileid, _ = js.Get("data").Get("fileid").String()
	return
}
Exemple #2
0
func (vc *VideoCloud) Sign(userid string, expire uint) (string, error) {
	return sign.AppSign(vc.Appid, vc.SecretId, vc.SecretKey, expire, userid)
}