forked from aliyun/aliyun-oss-go-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bucket_inventory.go
102 lines (91 loc) · 2.46 KB
/
bucket_inventory.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
package sample
import (
"fmt"
"github.com/aliyun/aliyun-oss-go-sdk/oss"
)
// BucketInventorySample shows how to set, get, list and delete the bucket inventory configuration
func BucketInventorySample() {
// New client
client, err := oss.New(endpoint, accessID, accessKey)
if err != nil {
HandleError(err)
}
// Create the bucket with default parameters
err = client.CreateBucket(bucketName)
if err != nil {
HandleError(err)
}
// the inventory configuration,not use any encryption
bl := true
invConfig := oss.InventoryConfiguration{
Id: "report1",
IsEnabled: &bl,
Prefix: "filterPrefix/",
OSSBucketDestination: oss.OSSBucketDestination{
Format: "CSV",
AccountId: accountID,
RoleArn: stsARN,
Bucket: "acs:oss:::" + bucketName,
Prefix: "prefix1",
},
Frequency: "Daily",
IncludedObjectVersions: "All",
OptionalFields: oss.OptionalFields{
Field: []string{
"Size", "LastModifiedDate", "ETag", "StorageClass", "IsMultipartUploaded", "EncryptionStatus",
},
},
}
// case 1: Set inventory
err = client.SetBucketInventory(bucketName, invConfig)
if err != nil {
HandleError(err)
}
// case 2: Get Bucket inventory
ret, err := client.GetBucketInventory(bucketName, invConfig.Id)
if err != nil {
HandleError(err)
}
fmt.Println("Bucket inventory:", ret)
// case 3: List Bucket inventory
invConfig2 := oss.InventoryConfiguration{
Id: "report2",
IsEnabled: &bl,
Prefix: "filterPrefix/",
OSSBucketDestination: oss.OSSBucketDestination{
Format: "CSV",
AccountId: accountID,
RoleArn: stsARN,
Bucket: "acs:oss:::" + bucketName,
Prefix: "prefix1",
},
Frequency: "Daily",
IncludedObjectVersions: "All",
OptionalFields: oss.OptionalFields{
Field: []string{
"Size", "LastModifiedDate", "ETag", "StorageClass", "IsMultipartUploaded", "EncryptionStatus",
},
},
}
err = client.SetBucketInventory(bucketName, invConfig2)
if err != nil {
HandleError(err)
}
NextContinuationToken := ""
listInvConf, err := client.ListBucketInventory(bucketName, NextContinuationToken)
if err != nil {
HandleError(err)
}
fmt.Println("Bucket inventory list:", listInvConf)
// case 4: Delete Bucket inventory
err = client.DeleteBucketInventory(bucketName, invConfig.Id)
if err != nil {
HandleError(err)
}
// Delete bucket
err = client.DeleteBucket(bucketName)
if err != nil {
HandleError(err)
}
fmt.Println("BucketInventorySample completed")
}