-
-
Notifications
You must be signed in to change notification settings - Fork 26
/
put.go
66 lines (59 loc) · 1.48 KB
/
put.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
60
61
62
63
64
65
66
package main
import (
"context"
"errors"
"net/http"
"os"
"strings"
"github.com/mozillazg/go-cos"
"github.com/mozillazg/go-cos/debug"
)
func main() {
b, _ := cos.NewBaseURL(os.Getenv("COS_BUCKET_URL"))
c := cos.NewClient(b, &http.Client{
Transport: &cos.AuthorizationTransport{
SecretID: os.Getenv("COS_SECRETID"),
SecretKey: os.Getenv("COS_SECRETKEY"),
Transport: &debug.DebugRequestTransport{
RequestHeader: true,
RequestBody: true,
ResponseHeader: true,
ResponseBody: true,
},
},
})
name := "test/objectPut.go"
f := strings.NewReader("test")
_, err := c.Object.Put(context.Background(), name, f, nil)
if err != nil {
panic(err)
}
// 测试上传以及特殊字符
name = "test/put_ + !'()* option.go"
contentDisposition := "attachment; filename=Hello - world!(+)'*.go"
f = strings.NewReader("test xxx")
opt := &cos.ObjectPutOptions{
ObjectPutHeaderOptions: &cos.ObjectPutHeaderOptions{
ContentType: "text/html",
ContentDisposition: contentDisposition,
},
ACLHeaderOptions: &cos.ACLHeaderOptions{
// XCosACL: "public-read",
XCosACL: "private",
},
}
resp, err := c.Object.Put(context.Background(), name, f, opt)
if err != nil {
panic(err)
}
resp.Body.Close()
// 测试特殊字符
resp, err = c.Object.Get(context.Background(), name, nil)
if err != nil {
panic(err)
}
resp.Body.Close()
if resp.Header.Get("Content-Disposition") != contentDisposition {
panic(errors.New("wong Content-Disposition"))
}
}