forked from mozillazg/go-cos
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathappend.go
75 lines (64 loc) · 1.43 KB
/
append.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
67
68
69
70
71
72
73
74
75
package main
import (
"bytes"
"context"
"fmt"
"math/rand"
"net/url"
"os"
"time"
"net/http"
"github.com/lewzylu/go-cos"
"github.com/lewzylu/go-cos/debug"
)
func genBigData(blockSize int) []byte {
b := make([]byte, blockSize)
if _, err := rand.Read(b); err != nil {
panic(err)
}
return b
}
func main() {
u, _ := url.Parse("https://test-1253846586.cos.ap-guangzhou.myqcloud.com")
b := &cos.BaseURL{
BucketURL: u,
}
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: false,
ResponseHeader: true,
ResponseBody: true,
},
},
})
startTime := time.Now()
name := fmt.Sprintf("test/test_object_append_%s", startTime.Format(time.RFC3339))
data := genBigData(1024 * 1024 * 1)
length := len(data)
r := bytes.NewReader(data)
ctx := context.Background()
// 第一次就必须 append
resp, err := c.Object.Append(ctx, name, 0, r, nil)
if err != nil {
panic(err)
return
}
fmt.Printf("%s\n", resp.Status)
// head
if _, err = c.Object.Head(ctx, name, nil); err != nil {
panic(err)
return
}
// 再次 append
data = genBigData(1024 * 1024 * 5)
r = bytes.NewReader(data)
resp, err = c.Object.Append(context.Background(), name, length, r, nil)
if err != nil {
panic(err)
}
fmt.Printf("%s\n", resp.Status)
}