-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathupload.go
59 lines (55 loc) · 1.64 KB
/
upload.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package upload
import (
"crypto/hmac"
"crypto/sha1"
"encoding/base64"
"fmt"
"io"
"io/ioutil"
"net/http"
"time"
)
const (
timeFormat = "Mon, 02 Jan 2006 15:04:05 GMT"
)
func sign(msg, secret string) string {
mac := hmac.New(sha1.New, []byte(secret))
mac.Write([]byte(msg))
return base64.StdEncoding.EncodeToString(mac.Sum(nil))
}
//accessKey:阿里云key
//secret:阿里云秘钥
//baseUrl:http://oss-cn-beijing.aliyuncs.com
//bucket:bucket
//objectName:test.html
//dir:test
//mType:text/html
//fileBuffer:io.Reader
func AliUploadWithMtype(accessKey, secret, baseUrl, bucket, objectName, dir, mType string, fileBuffer io.Reader) (err error) {
client := &http.Client{}
var canonicalizedResource, canonicalizedOSSHeaders, verb string
canonicalizedResource += "/" + bucket + "/" + dir + "/" + objectName
canonicalizedOSSHeaders += "x-oss-object-acl:public-read"
verb = "PUT"
str := verb + "\n" + "\n" + mType + "\n" + time.Now().UTC().Format(timeFormat) + "\n" + canonicalizedOSSHeaders + "\n" + canonicalizedResource
sign := sign(str, secret)
authorization := "OSS " + accessKey + ":" + sign
req, err := http.NewRequest("PUT", baseUrl+canonicalizedResource, fileBuffer)
req.Header.Add("x-oss-object-acl", "public-read")
req.Header.Add("Authorization", authorization)
req.Header.Add("Content-Type", mType)
req.Header.Add("Date", time.Now().UTC().Format(timeFormat))
resp, err := client.Do(req)
if err != nil {
return
}
defer resp.Body.Close()
_, err = ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
if resp.StatusCode != http.StatusOK && resp.StatusCode >= 300 {
return fmt.Errorf("status err %d", resp.StatusCode)
}
return nil
}