github.com/aliyun/aliyun-oss-go-sdk@v3.0.2+incompatible/sample/bucket_metaquery.go (about) 1 package sample 2 3 import ( 4 "fmt" 5 "github.com/aliyun/aliyun-oss-go-sdk/oss" 6 "os" 7 ) 8 9 func BucketMetaQuerySample() { 10 // New client 11 client, err := oss.New(endpoint, accessID, accessKey) 12 if err != nil { 13 HandleError(err) 14 } 15 // Open data indexing 16 err = client.OpenMetaQuery(bucketName) 17 if err != nil { 18 HandleError(err) 19 } 20 21 // Get meta data indexing query 22 result, err := client.GetMetaQueryStatus(bucketName) 23 if err != nil { 24 HandleError(err) 25 } 26 fmt.Printf("State:%s\n", result.State) 27 fmt.Printf("Phase%s\n", result.Phase) 28 fmt.Printf("CreateTime:%s\n", result.CreateTime) 29 fmt.Printf("UpdateTime:%s\n", result.UpdateTime) 30 31 // Do data indexing query 32 // 1. Simple query 33 query := oss.MetaQuery{ 34 NextToken: "", 35 MaxResults: 10, 36 Query: `{"Field": "Size","Value": "30","Operation": "gt"}`, 37 Sort: "Size", 38 Order: "asc", 39 } 40 queryResult, err := client.DoMetaQuery(bucketName, query) 41 if err != nil { 42 fmt.Println("Error:", err) 43 os.Exit(-1) 44 } 45 fmt.Printf("NextToken:%s\n", queryResult.NextToken) 46 for _, file := range queryResult.Files { 47 fmt.Printf("File name: %s\n", file.Filename) 48 fmt.Printf("size: %d\n", file.Size) 49 fmt.Printf("File Modified Time:%s\n", file.FileModifiedTime) 50 fmt.Printf("Oss Object Type:%s\n", file.OssObjectType) 51 fmt.Printf("Oss Storage Class:%s\n", file.OssStorageClass) 52 fmt.Printf("Object ACL:%s\n", file.ObjectACL) 53 fmt.Printf("ETag:%s\n", file.ETag) 54 fmt.Printf("Oss CRC64:%s\n", file.OssCRC64) 55 fmt.Printf("Oss Tagging Count:%d\n", file.OssTaggingCount) 56 for _, tagging := range file.OssTagging { 57 fmt.Printf("Oss Tagging Key:%s\n", tagging.Key) 58 fmt.Printf("Oss Tagging Value:%s\n", tagging.Value) 59 } 60 for _, userMeta := range file.OssUserMeta { 61 fmt.Printf("Oss User Meta Key:%s\n", userMeta.Key) 62 fmt.Printf("Oss User Meta Key Value:%s\n", userMeta.Value) 63 } 64 } 65 66 //2. Aggregate query 67 68 query = oss.MetaQuery{ 69 NextToken: "", 70 MaxResults: 100, 71 Query: `{"Field": "Size","Value": "30","Operation": "gt"}`, 72 Sort: "Size", 73 Order: "asc", 74 Aggregations: []oss.MetaQueryAggregationRequest{ 75 { 76 Field: "Size", 77 Operation: "max", 78 }, 79 { 80 Field: "Size", 81 Operation: "sum", 82 }, 83 { 84 Field: "Size", 85 Operation: "group", 86 }, 87 }, 88 } 89 queryResult, err = client.DoMetaQuery(bucketName, query) 90 if err != nil { 91 fmt.Println("Error:", err) 92 os.Exit(-1) 93 } 94 fmt.Printf("Next Token:%s\n", queryResult.NextToken) 95 for _, aggregation := range queryResult.Aggregations { 96 fmt.Printf("Aggregation Field:%s\n", aggregation.Field) 97 fmt.Printf("Aggregation Operation:%s\n", aggregation.Operation) 98 fmt.Printf("Aggregation Value:%d\n", aggregation.Value) 99 for _, group := range aggregation.Groups { 100 fmt.Printf("Group Value:%s\n", group.Value) 101 fmt.Printf("Group Count:%d\n", group.Count) 102 } 103 } 104 105 // 3.Query all 106 query = oss.MetaQuery{ 107 NextToken: "", 108 MaxResults: 10, 109 Query: `{"Field": "Size","Value": "30","Operation": "gt"}`, 110 Sort: "Size", 111 Order: "asc", 112 } 113 for { 114 queryResult, err = client.DoMetaQuery(bucketName, query) 115 if err != nil { 116 fmt.Println("Error:", err) 117 os.Exit(-1) 118 } 119 fmt.Printf("NextToken:%s\n", queryResult.NextToken) 120 for _, file := range queryResult.Files { 121 fmt.Printf("File name: %s\n", file.Filename) 122 fmt.Printf("size: %d\n", file.Size) 123 fmt.Printf("File Modified Time:%s\n", file.FileModifiedTime) 124 fmt.Printf("Oss Object Type:%s\n", file.OssObjectType) 125 fmt.Printf("Oss Storage Class:%s\n", file.OssStorageClass) 126 fmt.Printf("Object ACL:%s\n", file.ObjectACL) 127 fmt.Printf("ETag:%s\n", file.ETag) 128 fmt.Printf("Oss CRC64:%s\n", file.OssCRC64) 129 fmt.Printf("Oss Tagging Count:%d\n", file.OssTaggingCount) 130 for _, tagging := range file.OssTagging { 131 fmt.Printf("Oss Tagging Key:%s\n", tagging.Key) 132 fmt.Printf("Oss Tagging Value:%s\n", tagging.Value) 133 } 134 for _, userMeta := range file.OssUserMeta { 135 fmt.Printf("Oss User Meta Key:%s\n", userMeta.Key) 136 fmt.Printf("Oss User Meta Key Value:%s\n", userMeta.Value) 137 } 138 } 139 if queryResult.NextToken != "" { 140 query.NextToken = queryResult.NextToken 141 } else { 142 break 143 } 144 } 145 146 //4.Do Meta query use xml 147 xml := `<?xml version="1.0" encoding="UTF-8"?> 148 <MetaQuery> 149 <NextToken></NextToken> 150 <MaxResults>5</MaxResults> 151 <Query>{"Field": "Size","Value": "1048576","Operation": "gt"}</Query> 152 <Sort>Size</Sort> 153 <Order>asc</Order> 154 </MetaQuery>` 155 queryResult, err = client.DoMetaQueryXml(bucketName, xml) 156 if err != nil { 157 fmt.Println("Error:", err) 158 os.Exit(-1) 159 } 160 fmt.Printf("NextToken:%s\n", queryResult.NextToken) 161 for _, file := range queryResult.Files { 162 fmt.Printf("File name: %s\n", file.Filename) 163 fmt.Printf("size: %d\n", file.Size) 164 fmt.Printf("File Modified Time:%s\n", file.FileModifiedTime) 165 fmt.Printf("Oss Object Type:%s\n", file.OssObjectType) 166 fmt.Printf("Oss Storage Class:%s\n", file.OssStorageClass) 167 fmt.Printf("Object ACL:%s\n", file.ObjectACL) 168 fmt.Printf("ETag:%s\n", file.ETag) 169 fmt.Printf("Oss CRC64:%s\n", file.OssCRC64) 170 fmt.Printf("Oss Tagging Count:%d\n", file.OssTaggingCount) 171 for _, tagging := range file.OssTagging { 172 fmt.Printf("Oss Tagging Key:%s\n", tagging.Key) 173 fmt.Printf("Oss Tagging Value:%s\n", tagging.Value) 174 } 175 for _, userMeta := range file.OssUserMeta { 176 fmt.Printf("Oss User Meta Key:%s\n", userMeta.Key) 177 fmt.Printf("Oss User Meta Key Value:%s\n", userMeta.Value) 178 } 179 } 180 181 // Close meta data indexing 182 err = client.CloseMetaQuery(bucketName) 183 if err != nil { 184 HandleError(err) 185 } 186 187 fmt.Println("BucketDataIndexingSample completed") 188 }