/
list_versions_sample.go
264 lines (228 loc) · 7.07 KB
/
list_versions_sample.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
// Copyright 2019 Huawei Technologies Co.,Ltd.
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use
// this file except in compliance with the License. You may obtain a copy of the
// License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software distributed
// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
// CONDITIONS OF ANY KIND, either express or implied. See the License for the
// specific language governing permissions and limitations under the License.
/**
* This sample demonstrates how to list versions under specified bucket
* from OBS using the OBS SDK for Go.
*/
package examples
import (
"fmt"
"obs"
"strconv"
"strings"
)
type ListVersionsSample struct {
bucketName string
location string
obsClient *obs.ObsClient
}
func newListVersionsSample(ak, sk, endpoint, bucketName, location string) *ListVersionsSample {
obsClient, err := obs.New(ak, sk, endpoint)
if err != nil {
panic(err)
}
return &ListVersionsSample{obsClient: obsClient, bucketName: bucketName, location: location}
}
func (sample ListVersionsSample) CreateBucket() {
input := &obs.CreateBucketInput{}
input.Bucket = sample.bucketName
input.Location = sample.location
_, err := sample.obsClient.CreateBucket(input)
if err != nil {
panic(err)
}
setBucketVersioningInput := &obs.SetBucketVersioningInput{}
setBucketVersioningInput.Bucket = sample.bucketName
setBucketVersioningInput.Status = obs.VersioningStatusEnabled
_, err = sample.obsClient.SetBucketVersioning(setBucketVersioningInput)
if err != nil {
panic(err)
}
fmt.Printf("Create bucket:%s successfully!\n", sample.bucketName)
fmt.Println()
}
func (sample ListVersionsSample) preparePutObject(input *obs.PutObjectInput){
_, err := sample.obsClient.PutObject(input)
if err != nil {
panic(err)
}
}
func (sample ListVersionsSample) PrepareFoldersAndObjects() {
keyPrefix := "MyObjectKeyVersions"
folderPrefix := "src"
subFolderPrefix := "test"
input := &obs.PutObjectInput{}
input.Bucket = sample.bucketName
// First prepare folders and sub folders
for i := 0; i < 5; i++ {
key := folderPrefix + strconv.Itoa(i) + "/"
input.Key = key
sample.preparePutObject(input)
for j := 0; j < 3; j++ {
subKey := key + subFolderPrefix + strconv.Itoa(j) + "/"
input.Key = subKey
sample.preparePutObject(input)
}
}
// Insert 2 objects in each folder
input.Body = strings.NewReader("Hello OBS")
listVersionsInput := &obs.ListVersionsInput{}
listVersionsInput.Bucket = sample.bucketName
output, err := sample.obsClient.ListVersions(listVersionsInput)
if err != nil{
panic(err)
}
for _, version := range output.Versions {
for i := 0; i < 2; i++ {
objectKey := version.Key + keyPrefix + strconv.Itoa(i)
input.Key = objectKey
sample.preparePutObject(input)
}
}
// Insert 2 objects in root path
input.Key = keyPrefix + strconv.Itoa(0)
sample.preparePutObject(input)
input.Key = keyPrefix + strconv.Itoa(1)
sample.preparePutObject(input)
fmt.Println("Prepare folders and objects finished")
fmt.Println()
}
func (sample ListVersionsSample) ListVersionsInFolders() {
fmt.Println("List versions in folder src0/")
input := &obs.ListVersionsInput{}
input.Bucket = sample.bucketName
input.Prefix = "src0/"
output, err := sample.obsClient.ListVersions(input)
if err != nil {
panic(err)
}
for index, val := range output.Versions {
fmt.Printf("Version[%d]-ETag:%s, Key:%s, Size:%d, VersionId:%s\n",
index, val.ETag, val.Key, val.Size, val.VersionId)
}
fmt.Println()
fmt.Println("List versions in sub folder src0/test0/")
input.Prefix = "src0/test0/"
output, err = sample.obsClient.ListVersions(input)
if err != nil {
panic(err)
}
for index, val := range output.Versions {
fmt.Printf("Version[%d]-ETag:%s, Key:%s, Size:%d, VersionId:%s\n",
index, val.ETag, val.Key, val.Size, val.VersionId)
}
fmt.Println()
}
func (sample ListVersionsSample) ListVersionsByPage() {
pageSize := 10
pageNum := 1
input := &obs.ListVersionsInput{}
input.Bucket = sample.bucketName
input.MaxKeys = pageSize
for {
output, err := sample.obsClient.ListVersions(input)
if err != nil {
panic(err)
}
fmt.Printf("Page:%d\n", pageNum)
for index, val := range output.Versions {
fmt.Printf("Version[%d]-ETag:%s, Key:%s, Size:%d, VersionId:%s\n",
index, val.ETag, val.Key, val.Size, val.VersionId)
}
if output.IsTruncated {
input.KeyMarker = output.NextKeyMarker
input.VersionIdMarker = output.NextVersionIdMarker
pageNum++
} else {
break
}
}
fmt.Println()
}
func (sample ListVersionsSample) listVersionsByPrefixes(commonPrefixes []string) {
input := &obs.ListVersionsInput{}
input.Bucket = sample.bucketName
input.Delimiter = "/"
for _, prefix := range commonPrefixes {
input.Prefix = prefix
output, err := sample.obsClient.ListVersions(input)
if err != nil {
panic(err)
}
fmt.Printf("Folder %s:\n", prefix)
for index, val := range output.Versions {
fmt.Printf("Version[%d]-ETag:%s, Key:%s, Size:%d, VersionId:%s\n",
index, val.ETag, val.Key, val.Size, val.VersionId)
}
fmt.Println()
sample.listVersionsByPrefixes(output.CommonPrefixes)
}
}
func (sample ListVersionsSample) ListVersionsGroupByFolder() {
fmt.Println("List versions group by folder")
input := &obs.ListVersionsInput{}
input.Bucket = sample.bucketName
input.Delimiter = "/"
output, err := sample.obsClient.ListVersions(input)
if err != nil {
panic(err)
}
fmt.Println("Root path:")
for index, val := range output.Versions {
fmt.Printf("Version[%d]-ETag:%s, Key:%s, Size:%d, VersionId:%s\n",
index, val.ETag, val.Key, val.Size, val.VersionId)
}
fmt.Println()
sample.listVersionsByPrefixes(output.CommonPrefixes)
}
func (sample ListVersionsSample) BatchDeleteVersions() {
input := &obs.ListVersionsInput{}
input.Bucket = sample.bucketName
output, err := sample.obsClient.ListVersions(input)
if err != nil {
panic(err)
}
objects := make([]obs.ObjectToDelete, 0, len(output.Versions))
for _, val := range output.Versions {
objects = append(objects, obs.ObjectToDelete{Key: val.Key, VersionId: val.VersionId})
}
deleteObjectsInput := &obs.DeleteObjectsInput{}
deleteObjectsInput.Bucket = sample.bucketName
deleteObjectsInput.Objects = objects[:]
_, err = sample.obsClient.DeleteObjects(deleteObjectsInput)
if err != nil {
panic(err)
}
fmt.Println("Delete versions successfully!")
fmt.Println()
}
func RunListVersionsSample() {
const (
endpoint = "https://your-endpoint"
ak = "*** Provide your Access Key ***"
sk = "*** Provide your Secret Key ***"
bucketName = "bucket-test"
location = "yourbucketlocation"
)
sample := newListVersionsSample(ak, sk, endpoint, bucketName, location)
fmt.Println("Create a new bucket for demo")
sample.CreateBucket()
// First prepare folders and objects
sample.PrepareFoldersAndObjects()
// List versions in folders
sample.ListVersionsInFolders()
// List versions in way of pagination
sample.ListVersionsByPage()
// List versions group by folder
sample.ListVersionsGroupByFolder()
sample.BatchDeleteVersions()
}