1 // Package sample examples
12 "github.com/aliyun/aliyun-oss-go-sdk/oss"
15 // AppendObjectSample shows the append file's usage
16 func AppendObjectSample() {
18 bucket, err := GetTestBucket(bucketName)
23 err = bucket.DeleteObject(objectKey)
25 var str = "弃我去者,昨日之日不可留。 乱我心者,今日之日多烦忧!"
28 // Case 1: Append a string to the object
29 // The first append position is 0 and the return value is for the next append's position.
30 nextPos, err = bucket.AppendObject(objectKey, strings.NewReader(str), nextPos)
36 nextPos, err = bucket.AppendObject(objectKey, strings.NewReader(str), nextPos)
42 body, err := bucket.GetObject(objectKey)
46 data, err := ioutil.ReadAll(body)
51 fmt.Println(objectKey, ":", string(data))
53 err = bucket.DeleteObject(objectKey)
58 // Case 2: Append byte array to the object
60 // The first append position is 0, and the return value is for the next append's position.
61 nextPos, err = bucket.AppendObject(objectKey, bytes.NewReader([]byte(str)), nextPos)
67 nextPos, err = bucket.AppendObject(objectKey, bytes.NewReader([]byte(str)), nextPos)
73 body, err = bucket.GetObject(objectKey)
77 data, err = ioutil.ReadAll(body)
82 fmt.Println(objectKey, ":", string(data))
84 err = bucket.DeleteObject(objectKey)
89 // Case 3: Append a local file to the object
90 fd, err := os.Open(localFile)
97 nextPos, err = bucket.AppendObject(objectKey, fd, nextPos)
102 // Case 4: Get the next append position by GetObjectDetailedMeta
103 props, err := bucket.GetObjectDetailedMeta(objectKey)
104 nextPos, err = strconv.ParseInt(props.Get(oss.HTTPHeaderOssNextAppendPosition), 10, 64)
109 nextPos, err = bucket.AppendObject(objectKey, strings.NewReader(str), nextPos)
114 err = bucket.DeleteObject(objectKey)
119 // Case 5: Specify the object properties for the first append, including the "x-oss-meta"'s custom metadata.
120 options := []oss.Option{
121 oss.Expires(futureDate),
122 oss.ObjectACL(oss.ACLPublicRead),
123 oss.Meta("myprop", "mypropval")}
125 fd.Seek(0, os.SEEK_SET)
126 nextPos, err = bucket.AppendObject(objectKey, strings.NewReader(str), nextPos, options...)
131 fd.Seek(0, os.SEEK_SET)
132 nextPos, err = bucket.AppendObject(objectKey, strings.NewReader(str), nextPos)
137 props, err = bucket.GetObjectDetailedMeta(objectKey)
141 fmt.Println("myprop:", props.Get("x-oss-meta-myprop"))
143 goar, err := bucket.GetObjectACL(objectKey)
147 fmt.Println("Object ACL:", goar.ACL)
149 // Case 6: Set the storage classes.OSS provides three storage classes: Standard, Infrequent Access, and Archive.
150 // Upload a strings, and you can append some strings in the behind of object. but the object is 'Archive' storange class.
151 // An object created with the AppendObject operation is an appendable object. set the object storange-class to 'Archive'.
152 nextPos, err = bucket.AppendObject(appendObjectKey, strings.NewReader("昨夜雨疏风骤,浓睡不消残酒。试问卷帘人,"), nextPos, oss.ObjectStorageClass("Archive"))
157 // Delete the object and bucket
158 err = DeleteTestBucketAndObject(bucketName)
163 fmt.Println("AppendObjectSample completed")