forked from aliyun/aliyun-oss-go-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
append_object.go
164 lines (140 loc) · 4.09 KB
/
append_object.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
// Package sample examples
package sample
import (
"bytes"
"fmt"
"io/ioutil"
"os"
"strconv"
"strings"
"github.com/aliyun/aliyun-oss-go-sdk/oss"
)
// AppendObjectSample shows the append file's usage
func AppendObjectSample() {
// Create bucket
bucket, err := GetTestBucket(bucketName)
if err != nil {
HandleError(err)
}
err = bucket.DeleteObject(objectKey)
var str = "弃我去者,昨日之日不可留。 乱我心者,今日之日多烦忧!"
var nextPos int64
// Case 1: Append a string to the object
// The first append position is 0 and the return value is for the next append's position.
nextPos, err = bucket.AppendObject(objectKey, strings.NewReader(str), nextPos)
if err != nil {
HandleError(err)
}
// Second append
nextPos, err = bucket.AppendObject(objectKey, strings.NewReader(str), nextPos)
if err != nil {
HandleError(err)
}
// Download
body, err := bucket.GetObject(objectKey)
if err != nil {
HandleError(err)
}
data, err := ioutil.ReadAll(body)
body.Close()
if err != nil {
HandleError(err)
}
fmt.Println(objectKey, ":", string(data))
err = bucket.DeleteObject(objectKey)
if err != nil {
HandleError(err)
}
// Case 2: Append byte array to the object
nextPos = 0
// The first append position is 0, and the return value is for the next append's position.
nextPos, err = bucket.AppendObject(objectKey, bytes.NewReader([]byte(str)), nextPos)
if err != nil {
HandleError(err)
}
// Second append
nextPos, err = bucket.AppendObject(objectKey, bytes.NewReader([]byte(str)), nextPos)
if err != nil {
HandleError(err)
}
// Download
body, err = bucket.GetObject(objectKey)
if err != nil {
HandleError(err)
}
data, err = ioutil.ReadAll(body)
body.Close()
if err != nil {
HandleError(err)
}
fmt.Println(objectKey, ":", string(data))
err = bucket.DeleteObject(objectKey)
if err != nil {
HandleError(err)
}
// Case 3: Append a local file to the object
fd, err := os.Open(localFile)
if err != nil {
HandleError(err)
}
defer fd.Close()
nextPos = 0
nextPos, err = bucket.AppendObject(objectKey, fd, nextPos)
if err != nil {
HandleError(err)
}
// Case 4: Get the next append position by GetObjectDetailedMeta
props, err := bucket.GetObjectDetailedMeta(objectKey)
nextPos, err = strconv.ParseInt(props.Get(oss.HTTPHeaderOssNextAppendPosition), 10, 64)
if err != nil {
HandleError(err)
}
nextPos, err = bucket.AppendObject(objectKey, strings.NewReader(str), nextPos)
if err != nil {
HandleError(err)
}
err = bucket.DeleteObject(objectKey)
if err != nil {
HandleError(err)
}
// Case 5: Specify the object properties for the first append, including the "x-oss-meta"'s custom metadata.
options := []oss.Option{
oss.Expires(futureDate),
oss.ObjectACL(oss.ACLPublicRead),
oss.Meta("myprop", "mypropval")}
nextPos = 0
fd.Seek(0, os.SEEK_SET)
nextPos, err = bucket.AppendObject(objectKey, strings.NewReader(str), nextPos, options...)
if err != nil {
HandleError(err)
}
// Second append
fd.Seek(0, os.SEEK_SET)
nextPos, err = bucket.AppendObject(objectKey, strings.NewReader(str), nextPos)
if err != nil {
HandleError(err)
}
props, err = bucket.GetObjectDetailedMeta(objectKey)
if err != nil {
HandleError(err)
}
fmt.Println("myprop:", props.Get("x-oss-meta-myprop"))
goar, err := bucket.GetObjectACL(objectKey)
if err != nil {
HandleError(err)
}
fmt.Println("Object ACL:", goar.ACL)
// Case 6: Set the storage classes.OSS provides three storage classes: Standard, Infrequent Access, and Archive.
// Upload a strings, and you can append some strings in the behind of object. but the object is 'Archive' storange class.
// An object created with the AppendObject operation is an appendable object. set the object storange-class to 'Archive'.
nextPos, err = bucket.AppendObject(appendObjectKey, strings.NewReader("昨夜雨疏风骤,浓睡不消残酒。试问卷帘人,"), nextPos, oss.ObjectStorageClass("Archive"))
if err != nil {
HandleError(err)
}
// Delete the object and bucket
err = DeleteTestBucketAndObject(bucketName)
if err != nil {
HandleError(err)
}
fmt.Println("AppendObjectSample completed")
}