1package main
2
3import (
4	"context"
5	"errors"
6	"net/http"
7	"os"
8	"strings"
9
10	"github.com/mozillazg/go-cos"
11	"github.com/mozillazg/go-cos/debug"
12)
13
14func main() {
15	b, _ := cos.NewBaseURL(os.Getenv("COS_BUCKET_URL"))
16	c := cos.NewClient(b, &http.Client{
17		Transport: &cos.AuthorizationTransport{
18			SecretID:  os.Getenv("COS_SECRETID"),
19			SecretKey: os.Getenv("COS_SECRETKEY"),
20			Transport: &debug.DebugRequestTransport{
21				RequestHeader:  true,
22				RequestBody:    true,
23				ResponseHeader: true,
24				ResponseBody:   true,
25			},
26		},
27	})
28
29	name := "test/objectPut.go"
30	f := strings.NewReader("test")
31
32	_, err := c.Object.Put(context.Background(), name, f, nil)
33	if err != nil {
34		panic(err)
35	}
36
37	// 测试上传以及特殊字符
38	name = "test/put_ + !'()* option.go"
39	contentDisposition := "attachment; filename=Hello - world!(+)'*.go"
40	f = strings.NewReader("test xxx")
41	opt := &cos.ObjectPutOptions{
42		ObjectPutHeaderOptions: &cos.ObjectPutHeaderOptions{
43			ContentType:        "text/html",
44			ContentDisposition: contentDisposition,
45		},
46		ACLHeaderOptions: &cos.ACLHeaderOptions{
47			// XCosACL: "public-read",
48			XCosACL: "private",
49		},
50	}
51	resp, err := c.Object.Put(context.Background(), name, f, opt)
52	if err != nil {
53		panic(err)
54	}
55	resp.Body.Close()
56
57	// 测试特殊字符
58	resp, err = c.Object.Get(context.Background(), name, nil)
59	if err != nil {
60		panic(err)
61	}
62	resp.Body.Close()
63	if resp.Header.Get("Content-Disposition") != contentDisposition {
64		panic(errors.New("wong Content-Disposition"))
65	}
66}
67