Example of Alibaba cloud storage OSS Golang language upload file.

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

Since Ali side does not offer Golang example only one writes itself, if found the problem wants to give correct.

The configuration information is replaced by itself.


Calling methods

Putobject ("4.txt", []byte ("123123FASDFASDFADFADFASFASDFSADF"))



Package Ossimport ("bytes" "Crypto/hmac" "Crypto/sha1" "Encoding/base64" "FMT" "Io/ioutil" "Mime" "Net/http" "path/ FilePath "" Time ") func Authorization (Accesskeysecret, method, filemd5, filetype, date, canonicalizedossheaders, Canonicalizedresource string) string {h: = HMAC. New (SHA1. New, []byte (Accesskeysecret)) s: = Fmt. Sprintf ("%v\n%v\n%v\n%v\n%v%v", method, Filemd5, filetype, date, canonicalizedossheaders, Canonicalizedresource) H. Write ([]byte (s)) return base64. Stdencoding.encodetostring (H.sum (nil))}var client = &http. CLIENT{TIMEOUT:20 * time. Second}func Putobject (key string, b []byte) bool {URL: = fmt. Sprintf ("http://%v.%v/%v", config. Cfg.Oss.Bucket, CONFIG. Cfg.Oss.Server, key) req, err: = http. Newrequest ("PUT", url, bytes. Newreader (b)) if err! = Nil {logger. Debug ("Putobject.newrequest", err) return false} else {defer req. Body.close ()}dt: = time. Now (). UTC (). Format ("Mon, Jan 2006 15:04:05 GMT") ct: = mime. Typebyextension (filepath. EXT (key)) req. Header.add ("Content-length", String (len (b))) Req. HEader. ADD ("Content-type", CT) req. Header.add ("Date", DT) req. Header.add ("Authorization", "OSS" +config. Cfg.oss.keyid+ ":" +authorization (config. Cfg.Oss.Keysecret, "PUT", "", CT, DT, "", "/" +config. cfg.oss.bucket+ "/" +key)) If resp, err: = client. Do (req); Err = = Nil {if resp_body, err: = Ioutil. ReadAll (resp. Body); Err = = Nil {if resp. StatusCode = =//for k, V: = Range resp. Header {//fmt. Printf ("%s = =%s\n", K, V)//}return true} else {logger. Debug ("OSS Error return |", URL, ":", String (resp. StatusCode), ":", String (Resp_body)) return false}} else {logger. Debug ("Putobject.ioutil.ReadAll", err) return false}} else {logger. Debug ("Putobject.client.Do", err) return false}}

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.