github.com/aliyun/aliyun-oss-go-sdk@v3.0.2+incompatible/sample/bucket_lifecycle.go (about) 1 package sample 2 3 import ( 4 "fmt" 5 6 "github.com/aliyun/aliyun-oss-go-sdk/oss" 7 ) 8 9 // BucketLifecycleSample shows how to set, get and delete bucket's lifecycle. 10 func BucketLifecycleSample() { 11 // New client 12 client, err := oss.New(endpoint, accessID, accessKey) 13 if err != nil { 14 HandleError(err) 15 } 16 17 // Create the bucket with default parameters 18 err = client.CreateBucket(bucketName) 19 if err != nil { 20 HandleError(err) 21 } 22 23 // Case 1: Set the lifecycle. The rule ID is rule1 and the applied objects' prefix is one and the last modified Date is before 2015/11/11 24 expiration := oss.LifecycleExpiration{ 25 CreatedBeforeDate: "2015-11-11T00:00:00.000Z", 26 } 27 rule1 := oss.LifecycleRule{ 28 ID: "rule1", 29 Prefix: "one", 30 Status: "Enabled", 31 Expiration: &expiration, 32 } 33 var rules = []oss.LifecycleRule{rule1} 34 err = client.SetBucketLifecycle(bucketName, rules) 35 if err != nil { 36 HandleError(err) 37 } 38 39 // Case 2: Get the bucket's lifecycle 40 lc, err := client.GetBucketLifecycle(bucketName) 41 if err != nil { 42 HandleError(err) 43 } 44 fmt.Printf("Bucket Lifecycle:%v, %v\n", lc.Rules, *lc.Rules[0].Expiration) 45 46 // Case 3: Set the lifecycle, The rule ID is rule2 and the applied objects' prefix is two. The object start with the prefix will be transited to IA storage Type 3 days latter, and to archive storage type 30 days latter 47 transitionIA := oss.LifecycleTransition{ 48 Days: 3, 49 StorageClass: oss.StorageIA, 50 } 51 transitionArch := oss.LifecycleTransition{ 52 Days: 30, 53 StorageClass: oss.StorageArchive, 54 } 55 rule2 := oss.LifecycleRule{ 56 ID: "rule2", 57 Prefix: "two", 58 Status: "Enabled", 59 Transitions: []oss.LifecycleTransition{transitionIA, transitionArch}, 60 } 61 rules = []oss.LifecycleRule{rule2} 62 err = client.SetBucketLifecycle(bucketName, rules) 63 if err != nil { 64 HandleError(err) 65 } 66 67 // Case 4: Set the lifecycle, The rule ID is rule3 and the applied objects' prefix is three. The object start with the prefix will be transited to IA storage Type 3 days latter, and to archive storage type 30 days latter, the uncompleted multipart upload will be abort 3 days latter. 68 abortMPU := oss.LifecycleAbortMultipartUpload{ 69 Days: 3, 70 } 71 rule3 := oss.LifecycleRule{ 72 ID: "rule3", 73 Prefix: "three", 74 Status: "Enabled", 75 AbortMultipartUpload: &abortMPU, 76 } 77 rules = append(lc.Rules, rule3) 78 err = client.SetBucketLifecycle(bucketName, rules) 79 if err != nil { 80 HandleError(err) 81 } 82 83 // Case 5: Set the lifecycle. The rule ID is rule4 and the applied objects' has the tagging which prefix is four and the last modified Date is before 2015/11/11 84 expiration = oss.LifecycleExpiration{ 85 CreatedBeforeDate: "2015-11-11T00:00:00.000Z", 86 } 87 tag1 := oss.Tag{ 88 Key: "key1", 89 Value: "value1", 90 } 91 tag2 := oss.Tag{ 92 Key: "key2", 93 Value: "value2", 94 } 95 rule4 := oss.LifecycleRule{ 96 ID: "rule4", 97 Prefix: "four", 98 Status: "Enabled", 99 Tags: []oss.Tag{tag1, tag2}, 100 Expiration: &expiration, 101 } 102 rules = []oss.LifecycleRule{rule4} 103 err = client.SetBucketLifecycle(bucketName, rules) 104 if err != nil { 105 HandleError(err) 106 } 107 108 // Case 6: Set the lifecycle. The rule ID is filter one and Include Not exclusion conditions 109 expiration = oss.LifecycleExpiration{ 110 CreatedBeforeDate: "2015-11-11T00:00:00.000Z", 111 } 112 tag := oss.Tag{ 113 Key: "key1", 114 Value: "value1", 115 } 116 greater := int64(500) 117 less := int64(645000) 118 filter := oss.LifecycleFilter{ 119 ObjectSizeLessThan: &greater, 120 ObjectSizeGreaterThan: &less, 121 Not: []oss.LifecycleFilterNot{ 122 { 123 Prefix: "logs/log2", 124 Tag: &tag, 125 }, 126 }, 127 } 128 filterRule := oss.LifecycleRule{ 129 ID: "filter one", 130 Prefix: "logs", 131 Status: "Enabled", 132 Expiration: &expiration, 133 Transitions: []oss.LifecycleTransition{ 134 { 135 Days: 10, 136 StorageClass: oss.StorageIA, 137 }, 138 }, 139 Filter: &filter, 140 } 141 rules = []oss.LifecycleRule{filterRule} 142 err = client.SetBucketLifecycle(bucketName, rules) 143 if err != nil { 144 HandleError(err) 145 } 146 147 // Case 7: Set the lifecycle. The rules with amtime and return to std when visit 148 isTrue := true 149 isFalse := false 150 rule1 = oss.LifecycleRule{ 151 ID: "mtime transition1", 152 Prefix: "logs1", 153 Status: "Enabled", 154 Transitions: []oss.LifecycleTransition{ 155 { 156 Days: 30, 157 StorageClass: oss.StorageIA, 158 }, 159 }, 160 } 161 rule2 = oss.LifecycleRule{ 162 ID: "mtime transition2", 163 Prefix: "logs2", 164 Status: "Enabled", 165 Transitions: []oss.LifecycleTransition{ 166 { 167 Days: 30, 168 StorageClass: oss.StorageIA, 169 IsAccessTime: &isFalse, 170 }, 171 }, 172 } 173 rule3 = oss.LifecycleRule{ 174 ID: "amtime transition1", 175 Prefix: "logs3", 176 Status: "Enabled", 177 Transitions: []oss.LifecycleTransition{ 178 { 179 Days: 30, 180 StorageClass: oss.StorageIA, 181 IsAccessTime: &isTrue, 182 ReturnToStdWhenVisit: &isFalse, 183 }, 184 }, 185 } 186 rule4 = oss.LifecycleRule{ 187 ID: "amtime transition2", 188 Prefix: "logs4", 189 Status: "Enabled", 190 Transitions: []oss.LifecycleTransition{ 191 { 192 Days: 30, 193 StorageClass: oss.StorageIA, 194 IsAccessTime: &isTrue, 195 ReturnToStdWhenVisit: &isTrue, 196 AllowSmallFile: &isFalse, 197 }, 198 }, 199 } 200 rule5 := oss.LifecycleRule{ 201 ID: "amtime transition3", 202 Prefix: "logs5", 203 Status: "Enabled", 204 NonVersionTransitions: []oss.LifecycleVersionTransition{ 205 { 206 NoncurrentDays: 10, 207 StorageClass: oss.StorageIA, 208 IsAccessTime: &isTrue, 209 ReturnToStdWhenVisit: &isFalse, 210 AllowSmallFile: &isTrue, 211 }, 212 }, 213 } 214 rules = []oss.LifecycleRule{rule1, rule2, rule3, rule4, rule5} 215 err = client.SetBucketLifecycle(bucketName, rules) 216 if err != nil { 217 HandleError(err) 218 } 219 220 // case 8: Set bucket's Lifecycle with xml 221 xmlData := `<?xml version="1.0" encoding="UTF-8"?> 222 <LifecycleConfiguration> 223 <Rule> 224 <ID>mtime transition1</ID> 225 <Prefix>logs1/</Prefix> 226 <Status>Enabled</Status> 227 <Transition> 228 <Days>30</Days> 229 <StorageClass>IA</StorageClass> 230 </Transition> 231 </Rule> 232 <Rule> 233 <ID>mtime transition2</ID> 234 <Prefix>logs2/</Prefix> 235 <Status>Enabled</Status> 236 <Transition> 237 <Days>30</Days> 238 <StorageClass>IA</StorageClass> 239 <IsAccessTime>false</IsAccessTime> 240 </Transition> 241 </Rule> 242 <Rule> 243 <ID>atime transition1</ID> 244 <Prefix>logs3/</Prefix> 245 <Status>Enabled</Status> 246 <Transition> 247 <Days>30</Days> 248 <StorageClass>IA</StorageClass> 249 <IsAccessTime>true</IsAccessTime> 250 <ReturnToStdWhenVisit>false</ReturnToStdWhenVisit> 251 </Transition> 252 </Rule> 253 <Rule> 254 <ID>atime transition2</ID> 255 <Prefix>logs4/</Prefix> 256 <Status>Enabled</Status> 257 <Transition> 258 <Days>30</Days> 259 <StorageClass>IA</StorageClass> 260 <IsAccessTime>true</IsAccessTime> 261 <ReturnToStdWhenVisit>true</ReturnToStdWhenVisit> 262 <AllowSmallFile>false</AllowSmallFile> 263 </Transition> 264 </Rule> 265 <Rule> 266 <ID>atime transition3</ID> 267 <Prefix>logs5/</Prefix> 268 <Status>Enabled</Status> 269 <NoncurrentVersionTransition> 270 <NoncurrentDays>10</NoncurrentDays> 271 <StorageClass>IA</StorageClass> 272 <IsAccessTime>true</IsAccessTime> 273 <ReturnToStdWhenVisit>false</ReturnToStdWhenVisit> 274 <AllowSmallFile>true</AllowSmallFile> 275 </NoncurrentVersionTransition> 276 </Rule> 277 <Rule> 278 <ID>r1</ID> 279 <Prefix>abc/</Prefix> 280 <Filter> 281 <ObjectSizeGreaterThan>500</ObjectSizeGreaterThan> 282 <ObjectSizeLessThan>64000</ObjectSizeLessThan> 283 <Not> 284 <Prefix>abc/not1/</Prefix> 285 <Tag> 286 <Key>notkey1</Key> 287 <Value>notvalue1</Value> 288 </Tag> 289 </Not> 290 <Not> 291 <Prefix>abc/not2/</Prefix> 292 <Tag> 293 <Key>notkey2</Key> 294 <Value>notvalue2</Value> 295 </Tag> 296 </Not> 297 </Filter> 298 </Rule> 299 </LifecycleConfiguration> 300 ` 301 err = client.SetBucketLifecycleXml(bucketName, xmlData) 302 if err != nil { 303 HandleError(err) 304 } 305 306 // case 9: Get bucket's Lifecycle print info 307 lcRes, err := client.GetBucketLifecycle(bucketName) 308 if err != nil { 309 HandleError(err) 310 } 311 for _, rule := range lcRes.Rules { 312 fmt.Println("Lifecycle Rule Id:", rule.ID) 313 fmt.Println("Lifecycle Rule Prefix:", rule.Prefix) 314 fmt.Println("Lifecycle Rule Status:", rule.Status) 315 if rule.Expiration != nil { 316 fmt.Println("Lifecycle Rule Expiration Days:", rule.Expiration.Days) 317 fmt.Println("Lifecycle Rule Expiration Date:", rule.Expiration.Date) 318 fmt.Println("Lifecycle Rule Expiration Created Before Date:", rule.Expiration.CreatedBeforeDate) 319 if rule.Expiration.ExpiredObjectDeleteMarker != nil { 320 fmt.Println("Lifecycle Rule Expiration Expired Object DeleteMarker:", *rule.Expiration.ExpiredObjectDeleteMarker) 321 } 322 } 323 324 for _, tag := range rule.Tags { 325 fmt.Println("Lifecycle Rule Tag Key:", tag.Key) 326 fmt.Println("Lifecycle Rule Tag Value:", tag.Value) 327 } 328 329 for _, transition := range rule.Transitions { 330 fmt.Println("Lifecycle Rule Transition Days:", transition.Days) 331 fmt.Println("Lifecycle Rule Transition Created Before Date:", transition.CreatedBeforeDate) 332 fmt.Println("Lifecycle Rule Transition Storage Class:", transition.StorageClass) 333 if transition.IsAccessTime != nil { 334 fmt.Println("Lifecycle Rule Transition Is Access Time:", *transition.IsAccessTime) 335 } 336 if transition.ReturnToStdWhenVisit != nil { 337 fmt.Println("Lifecycle Rule Transition Return To Std When Visit:", *transition.ReturnToStdWhenVisit) 338 } 339 340 if transition.AllowSmallFile != nil { 341 fmt.Println("Lifecycle Rule Transition Allow Small File:", *transition.AllowSmallFile) 342 } 343 344 } 345 if rule.AbortMultipartUpload != nil { 346 fmt.Println("Lifecycle Rule Abort Multipart Upload Days:", rule.AbortMultipartUpload.Days) 347 fmt.Println("Lifecycle Rule Abort Multipart Upload Created Before Date:", rule.AbortMultipartUpload.CreatedBeforeDate) 348 } 349 350 if rule.NonVersionExpiration != nil { 351 fmt.Println("Lifecycle Non Version Expiration Non Current Days:", rule.NonVersionExpiration.NoncurrentDays) 352 } 353 354 for _, nonVersionTransition := range rule.NonVersionTransitions { 355 fmt.Println("Lifecycle Rule Non Version Transitions Non current Days:", nonVersionTransition.NoncurrentDays) 356 fmt.Println("Lifecycle Rule Non Version Transition Storage Class:", nonVersionTransition.StorageClass) 357 if nonVersionTransition.IsAccessTime != nil { 358 fmt.Println("Lifecycle Rule Non Version Transition Is Access Time:", *nonVersionTransition.IsAccessTime) 359 } 360 361 if nonVersionTransition.ReturnToStdWhenVisit != nil { 362 fmt.Println("Lifecycle Rule Non Version Transition Return To Std When Visit:", *nonVersionTransition.ReturnToStdWhenVisit) 363 } 364 365 if nonVersionTransition.AllowSmallFile != nil { 366 fmt.Println("Lifecycle Rule Non Version Allow Small File:", *nonVersionTransition.AllowSmallFile) 367 } 368 369 if rule.Filter != nil { 370 if rule.Filter.ObjectSizeGreaterThan != nil { 371 fmt.Println("Lifecycle Rule Filter Object Size Greater Than:", *rule.Filter.ObjectSizeGreaterThan) 372 } 373 if rule.Filter.ObjectSizeLessThan != nil { 374 fmt.Println("Lifecycle Rule Filter Object Size Less Than:", *rule.Filter.ObjectSizeLessThan) 375 } 376 for _, filterNot := range rule.Filter.Not { 377 fmt.Println("Lifecycle Rule Filter Not Prefix:", filterNot.Prefix) 378 if filterNot.Tag != nil { 379 fmt.Println("Lifecycle Rule Filter Not Tag Key:", filterNot.Tag.Key) 380 fmt.Println("Lifecycle Rule Filter Not Tag Value:", filterNot.Tag.Value) 381 } 382 } 383 } 384 } 385 } 386 387 // Case 10: Delete bucket's Lifecycle 388 err = client.DeleteBucketLifecycle(bucketName) 389 if err != nil { 390 HandleError(err) 391 } 392 393 // Delete bucket 394 err = client.DeleteBucket(bucketName) 395 if err != nil { 396 HandleError(err) 397 } 398 399 fmt.Println("BucketLifecycleSample completed") 400 }