Golang uploading files to seven cow cloud

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed. # # # Get first to get is seven cow Cloud official SDK ' go get-u github.com/qiniu/api.v7 ' # # # seven Qiniu-[seven Qiniu] (https://www.qiniu.com/) no account to register an account. -[Personal Center] (Https://portal.qiniu.com/user/key) has an account directly into the personal center, to see the key accesskey/secretkey### code some constant variable "" Const (// The locally saved folder name Upload_path string = "/files/") var (//bucket is your name in the storage space Access_key = "******ea09vcy5efn_*******************" Secret_key = "******-yvwcywimn6f*******************" bucket = "bucket") ' ##### ' WEB ' side code * * Get uploaded file * * "func Uploadhandle (w http. Responsewriter, R *http. Request) {//The method is judged from the requested if R.method = = "GET" {tmp, err: = template. Parsefiles ("templates/upload.html") if err! = Nil {fmt. Println ("template render Failed")}tmp. Execute (W, Nil)} else {//Get the contents of the file to get files, head, err: = R.formfile ("file") if err! = Nil {fmt. PRINTLN (Err) return}defer file. Close ()//Create folder pwd, _: = OS. GETWD ()//folder will return an error, can be thrown out with ' _ ' err = os. Mkdir (Pwd+upload_path, OS. MODEPERM) If err! = Nil {fmt. Println ("dir is create Error")}FW, err: = OS. Create (pwd + Upload_path + head. Filename) If err! = Nil {fmt. PrinTLN ("File creation failed") return}fmt. PRINTLN (*FW) defer fw.close ()//Copy file, save to Local _, err = Io. Copy (FW, file) if err! = Nil {fmt. Println ("File Save Failed") return}//call seven kn upload function upload_qiniu (pwd + Upload_path + head. Filename) http. Redirect (W, R, "/", HTTP. Statusfound)}} ' * * Seven cow cloud upload function * * ' func upload_qiniu (FilePath string) {key: = ' github-x.png '//upload voucher, About the voucher this piece you can go to see the official document Putpolicy: = storage. Putpolicy{scope:bucket,}mac: = QBox. Newmac (Access_key, secret_key) Uptoken: = Putpolicy.uploadtoken (Mac) cfg: = storage. config{}//space corresponding Room//the relationship between the zone object and the room is as follows://Room Zone object//east China storage. zonehuadong//North China Storage. zonehuabei//South China Storage. zonehuanan//North American Storage. zonebeimei//Seven Cow cloud storage settings The first page has a storage area of CFG. Zone = &storage. zonehuanan//do not enable HTTPS domain name CFG.USEHTTPS = false//do not use CND acceleration cfg.usecdndomains = false//Build Upload Form object Formuploader: = Storage. Newformuploader (&cfg) RET: = storage. putret{}//Optional Putextra: = storage. putextra{params:map[string]string{"x:name": "GitHub logo",},}err: = Formuploader.putfile (context. Background (), &ret, Uptoken, Key, FilePath, &putextra) If err ! = Nil {fmt. PRINTLN (Err) return}fmt. PRINTLN (ret. Key, ret. Hash)} ' + * * The entire code implementation is mainly stored in the local path, and then in the upload function to read the path to upload the file **+ * * There may be other convenient, fast method, I am still groping, this is a summary of these days of learning, the following will also modify the code, do some expansion **+ [Full code Address] (https://github.com/ma1ive/Go/blob/master/qiniu/main.go) 213 reads  ∙  1 likes  
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.