Skip to content

Commit fe2ff83

Browse files
committed
Add meta query interface
1 parent 572d0f8 commit fe2ff83

15 files changed

Lines changed: 1604 additions & 0 deletions

samples/BucketMetaQuery.php

Lines changed: 347 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,347 @@
1+
<?php
2+
3+
require_once __DIR__ . '/Common.php';
4+
5+
use OSS\OssClient;
6+
use OSS\Core\OssException;
7+
use OSS\Model\MetaQuery;
8+
use OSS\Model\MetaQueryAggregation;
9+
$bucket = Common::getBucketName();
10+
$ossClient = Common::getOssClient();
11+
if (is_null($ossClient)) exit(1);
12+
13+
//******************************* Simple Usage****************************************************************
14+
15+
// Open Meta Query
16+
$ossClient->openMetaQuery($bucket);
17+
Common::println("bucket $bucket open meta query success".PHP_EOL);
18+
19+
// Get Meta Query Status
20+
$rs = $ossClient->getMetaQueryStatus($bucket);
21+
Common::println("bucket $bucket meta query status state:".$rs->getState().PHP_EOL);
22+
Common::println("bucket $bucket meta query status phase:".$rs->getPhase().PHP_EOL);
23+
Common::println("bucket $bucket meta query status create time:".$rs->getCreateTime().PHP_EOL);
24+
Common::println("bucket $bucket meta query status update time:".$rs->getUpdateTime().PHP_EOL);
25+
26+
// Do meta query
27+
// Query files larger than 500
28+
$query = '{"Field": "Size","Value": "500","Operation": "gt"}';
29+
$maxResults=100;
30+
$sort="Size";
31+
$order="asc";
32+
$metaQuery = new MetaQuery($query,$maxResults,$sort,$order);
33+
//Add aggregation condition
34+
$agg = new MetaQueryAggregation("Size","sum");
35+
$aggOne = new MetaQueryAggregation("Size","count");
36+
$metaQuery->addAggregation($agg);
37+
$metaQuery->addAggregation($aggOne);
38+
$rs = $ossClient->doMetaQuery($bucket,$metaQuery);
39+
if ($rs->getNextToken() != ""){
40+
printf("Next Token:".$rs->getNextToken().PHP_EOL);
41+
}
42+
if (count($rs->getFiles()) > 0){
43+
foreach ($rs->getFiles() as $file ){
44+
printf("File info ===============".$file->getFileName()."============================== start ".PHP_EOL);
45+
printf("File Name:".$file->getFileName().PHP_EOL);
46+
printf("File Size:".$file->getSize().PHP_EOL);
47+
printf("File Modified Time:".$file->getFileModifiedTime().PHP_EOL);
48+
printf("File Oss Object Type:".$file->getOssObjectType().PHP_EOL);
49+
printf("File Oss Storage Class:".$file->getOssStorageClass().PHP_EOL);
50+
printf("File Object Acl:".$file->getObjectAcl().PHP_EOL);
51+
printf("File ETag:".$file->getETag().PHP_EOL);
52+
printf("File Oss Crc64:".$file->getOssCrc64().PHP_EOL);
53+
printf("File Oss Tagging Count:".$file->getOssTaggingCount().PHP_EOL);
54+
printf("File Server Side Encryption:".$file->getServerSideEncryption().PHP_EOL);
55+
printf("File Server Side Encryption Customer Algorithm:".$file->getServerSideEncryptionCustomerAlgorithm().PHP_EOL);
56+
if (count($file->getOssUserMeta()) > 0){
57+
foreach ($file->getOssUserMeta() as $ossUserMeta ){
58+
printf("File OSS User Meta Key:".$ossUserMeta->getKey().PHP_EOL);
59+
printf("File OSS User Meta Value:".$ossUserMeta->getValue().PHP_EOL);
60+
}
61+
}
62+
if (count($file->getOssTagging()) > 0){
63+
foreach ($file->getOssTagging() as $ossTagging ){
64+
printf("File OSS Tagging Key:".$ossTagging->getKey().PHP_EOL);
65+
printf("File OSS Tagging Value:".$ossTagging->getValue().PHP_EOL);
66+
}
67+
}
68+
}
69+
}
70+
if (count($rs->getAggregations()) > 0){
71+
foreach ($rs->getAggregations() as $key=> $aggregation){
72+
printf("Aggregation Field:".$aggregation->getField().PHP_EOL);
73+
printf("Aggregation Operation:".$aggregation->getOperation().PHP_EOL);
74+
printf("Aggregation Value:".$aggregation->getValue().PHP_EOL);
75+
foreach ($aggregation->getGroup() as $group ){
76+
printf("Aggregation Group Value:".$group->getValue().PHP_EOL);
77+
printf("Aggregation Group Count:".$group->getCount().PHP_EOL);
78+
}
79+
}
80+
}
81+
82+
// Do Meta Query By Xml
83+
$xml = <<<BBB
84+
<?xml version="1.0" encoding="UTF-8"?>
85+
<MetaQuery>
86+
<NextToken></NextToken>
87+
<MaxResults>5</MaxResults>
88+
<Query>{"Field": "Size","Value": "1048576","Operation": "gt"}</Query>
89+
<Sort>Size</Sort>
90+
<Order>asc</Order>
91+
</MetaQuery>
92+
BBB;
93+
94+
$rs = $ossClient->doMetaQueryXml($bucket,$xml);
95+
if ($rs->getNextToken() != ""){
96+
printf("Next Token:".$rs->getNextToken().PHP_EOL);
97+
}
98+
if (count($rs->getFiles()) > 0){
99+
foreach ($rs->getFiles() as $file ){
100+
printf("File info ===============".$file->getFileName()."============================== start ".PHP_EOL);
101+
printf("File Name:".$file->getFileName().PHP_EOL);
102+
printf("File Size:".$file->getSize().PHP_EOL);
103+
printf("File Modified Time:".$file->getFileModifiedTime().PHP_EOL);
104+
printf("File Oss Object Type:".$file->getOssObjectType().PHP_EOL);
105+
printf("File Oss Storage Class:".$file->getOssStorageClass().PHP_EOL);
106+
printf("File Object Acl:".$file->getObjectAcl().PHP_EOL);
107+
printf("File ETag:".$file->getETag().PHP_EOL);
108+
printf("File Oss Crc64:".$file->getOssCrc64().PHP_EOL);
109+
printf("File Oss Tagging Count:".$file->getOssTaggingCount().PHP_EOL);
110+
printf("File Server Side Encryption:".$file->getServerSideEncryption().PHP_EOL);
111+
printf("File Server Side Encryption Customer Algorithm:".$file->getServerSideEncryptionCustomerAlgorithm().PHP_EOL);
112+
if (count($file->getOssUserMeta()) > 0){
113+
foreach ($file->getOssUserMeta() as $ossUserMeta ){
114+
printf("File OSS User Meta Key:".$ossUserMeta->getKey().PHP_EOL);
115+
printf("File OSS User Meta Value:".$ossUserMeta->getValue().PHP_EOL);
116+
}
117+
}
118+
if (count($file->getOssTagging()) > 0){
119+
foreach ($file->getOssTagging() as $ossTagging ){
120+
printf("File OSS Tagging Key:".$ossTagging->getKey().PHP_EOL);
121+
printf("File OSS Tagging Value:".$ossTagging->getValue().PHP_EOL);
122+
}
123+
}
124+
}
125+
}
126+
if (count($rs->getAggregations()) > 0){
127+
foreach ($rs->getAggregations() as $key=> $aggregation){
128+
printf("Aggregation Field:".$aggregation->getField().PHP_EOL);
129+
printf("Aggregation Operation:".$aggregation->getOperation().PHP_EOL);
130+
printf("Aggregation Value:".$aggregation->getValue().PHP_EOL);
131+
foreach ($aggregation->getGroup() as $group ){
132+
printf("Aggregation Group Value:".$group->getValue().PHP_EOL);
133+
printf("Aggregation Group Count:".$group->getCount().PHP_EOL);
134+
}
135+
}
136+
}
137+
138+
139+
// Close Meta Query
140+
$ossClient->closeMetaQuery($bucket);
141+
Common::println("bucket $bucket has closed meta query".PHP_EOL);
142+
143+
//******************************* For complete usage, see the following functions ****************************************************
144+
openMetaQuery($ossClient,$bucket);
145+
getMetaQueryStatus($ossClient,$bucket);
146+
doMetaQuery($ossClient,$bucket);
147+
doMetaQueryXml($ossClient,$bucket);
148+
closeMetaQuery($ossClient,$bucket);
149+
150+
/**
151+
* Open Meta Query
152+
*
153+
* @param OssClient $ossClient OssClient instance
154+
* @param string $bucket bucket name
155+
* @return null
156+
*/
157+
function openMetaQuery($ossClient,$bucket){
158+
try {
159+
$ossClient->openMetaQuery($bucket);
160+
} catch (OssException $e) {
161+
printf(__FUNCTION__ . ": FAILED\n");
162+
printf($e->getMessage() . "\n");
163+
return;
164+
}
165+
print(__FUNCTION__ . ": OK" . "\n");
166+
}
167+
/**
168+
* Get Meta Query Status
169+
*
170+
* @param OssClient $ossClient OssClient instance
171+
* @param string $bucket bucket name
172+
* @return null
173+
*/
174+
function getMetaQueryStatus($ossClient,$bucket){
175+
try {
176+
$rs = $ossClient->getMetaQueryStatus($bucket);
177+
printf("bucket $bucket meta query status state:".$rs->getState().PHP_EOL);
178+
printf("bucket $bucket meta query status phase:".$rs->getPhase().PHP_EOL);
179+
printf("bucket $bucket meta query status create time:".$rs->getCreateTime().PHP_EOL);
180+
printf("bucket $bucket meta query status update time:".$rs->getUpdateTime().PHP_EOL);
181+
} catch (OssException $e) {
182+
printf(__FUNCTION__ . ": FAILED\n");
183+
printf($e->getMessage() . "\n");
184+
return;
185+
}
186+
print(__FUNCTION__ . ": OK" . "\n");
187+
}
188+
/**
189+
* Do Meta Query
190+
*
191+
* @param OssClient $ossClient OssClient instance
192+
* @param string $bucket bucket name
193+
* @return null
194+
*/
195+
function doMetaQuery($ossClient,$bucket){
196+
try {
197+
// Query files larger than 500
198+
$query = '{"Field": "Size","Value": "500","Operation": "gt"}';
199+
$maxResults=100;
200+
$sort="Size";
201+
$order="asc";
202+
$metaQuery = new MetaQuery($query,$maxResults,$sort,$order);
203+
//Add aggregation condition
204+
$agg = new MetaQueryAggregation("Size","sum");
205+
$aggOne = new MetaQueryAggregation("Size","count");
206+
$metaQuery->addAggregation($agg);
207+
$metaQuery->addAggregation($aggOne);
208+
$rs = $ossClient->doMetaQuery($bucket,$metaQuery);
209+
if ($rs->getNextToken() != ""){
210+
printf("Next Token:".$rs->getNextToken().PHP_EOL);
211+
}
212+
if (count($rs->getFiles()) > 0){
213+
foreach ($rs->getFiles() as $file ){
214+
printf("File info ===============".$file->getFileName()."============================== start ".PHP_EOL);
215+
printf("File Name:".$file->getFileName().PHP_EOL);
216+
printf("File Size:".$file->getSize().PHP_EOL);
217+
printf("File Modified Time:".$file->getFileModifiedTime().PHP_EOL);
218+
printf("File Oss Object Type:".$file->getOssObjectType().PHP_EOL);
219+
printf("File Oss Storage Class:".$file->getOssStorageClass().PHP_EOL);
220+
printf("File Object Acl:".$file->getObjectAcl().PHP_EOL);
221+
printf("File ETag:".$file->getETag().PHP_EOL);
222+
printf("File Oss Crc64:".$file->getOssCrc64().PHP_EOL);
223+
printf("File Oss Tagging Count:".$file->getOssTaggingCount().PHP_EOL);
224+
printf("File Server Side Encryption:".$file->getServerSideEncryption().PHP_EOL);
225+
printf("File Server Side Encryption Customer Algorithm:".$file->getServerSideEncryptionCustomerAlgorithm().PHP_EOL);
226+
if (count($file->getOssUserMeta()) > 0){
227+
foreach ($file->getOssUserMeta() as $ossUserMeta ){
228+
printf("File OSS User Meta Key:".$ossUserMeta->getKey().PHP_EOL);
229+
printf("File OSS User Meta Value:".$ossUserMeta->getValue().PHP_EOL);
230+
}
231+
}
232+
if (count($file->getOssTagging()) > 0){
233+
foreach ($file->getOssTagging() as $ossTagging ){
234+
printf("File OSS Tagging Key:".$ossTagging->getKey().PHP_EOL);
235+
printf("File OSS Tagging Value:".$ossTagging->getValue().PHP_EOL);
236+
}
237+
}
238+
}
239+
}
240+
if (count($rs->getAggregations()) > 0){
241+
foreach ($rs->getAggregations() as $key=> $aggregation){
242+
printf("Aggregation Field:".$aggregation->getField().PHP_EOL);
243+
printf("Aggregation Operation:".$aggregation->getOperation().PHP_EOL);
244+
printf("Aggregation Value:".$aggregation->getValue().PHP_EOL);
245+
foreach ($aggregation->getGroup() as $group ){
246+
printf("Aggregation Group Value:".$group->getValue().PHP_EOL);
247+
printf("Aggregation Group Count:".$group->getCount().PHP_EOL);
248+
}
249+
}
250+
}
251+
} catch (OssException $e) {
252+
printf(__FUNCTION__ . ": FAILED\n");
253+
printf($e->getMessage() . "\n");
254+
return;
255+
}
256+
print(__FUNCTION__ . ": OK" . "\n");
257+
258+
}
259+
260+
/**
261+
* Do Meta Query
262+
*
263+
* @param OssClient $ossClient OssClient instance
264+
* @param string $bucket bucket name
265+
* @return null
266+
*/
267+
function doMetaQueryXml($ossClient,$bucket){
268+
try {
269+
// Query files larger than 500
270+
$xml = <<<BBB
271+
<?xml version="1.0" encoding="UTF-8"?>
272+
<MetaQuery>
273+
<NextToken></NextToken>
274+
<MaxResults>5</MaxResults>
275+
<Query>{"Field": "Size","Value": "1048576","Operation": "gt"}</Query>
276+
<Sort>Size</Sort>
277+
<Order>asc</Order>
278+
</MetaQuery>
279+
BBB;
280+
$rs = $ossClient->doMetaQueryXml($bucket,$xml);
281+
if ($rs->getNextToken() != ""){
282+
printf("Next Token:".$rs->getNextToken().PHP_EOL);
283+
}
284+
if (count($rs->getFiles()) > 0){
285+
foreach ($rs->getFiles() as $file ){
286+
printf("File info ===============".$file->getFileName()."============================== start ".PHP_EOL);
287+
printf("File Name:".$file->getFileName().PHP_EOL);
288+
printf("File Size:".$file->getSize().PHP_EOL);
289+
printf("File Modified Time:".$file->getFileModifiedTime().PHP_EOL);
290+
printf("File Oss Object Type:".$file->getOssObjectType().PHP_EOL);
291+
printf("File Oss Storage Class:".$file->getOssStorageClass().PHP_EOL);
292+
printf("File Object Acl:".$file->getObjectAcl().PHP_EOL);
293+
printf("File ETag:".$file->getETag().PHP_EOL);
294+
printf("File Oss Crc64:".$file->getOssCrc64().PHP_EOL);
295+
printf("File Oss Tagging Count:".$file->getOssTaggingCount().PHP_EOL);
296+
printf("File Server Side Encryption:".$file->getServerSideEncryption().PHP_EOL);
297+
printf("File Server Side Encryption Customer Algorithm:".$file->getServerSideEncryptionCustomerAlgorithm().PHP_EOL);
298+
if (count($file->getOssUserMeta()) > 0){
299+
foreach ($file->getOssUserMeta() as $ossUserMeta ){
300+
printf("File OSS User Meta Key:".$ossUserMeta->getKey().PHP_EOL);
301+
printf("File OSS User Meta Value:".$ossUserMeta->getValue().PHP_EOL);
302+
}
303+
}
304+
if (count($file->getOssTagging()) > 0){
305+
foreach ($file->getOssTagging() as $ossTagging ){
306+
printf("File OSS Tagging Key:".$ossTagging->getKey().PHP_EOL);
307+
printf("File OSS Tagging Value:".$ossTagging->getValue().PHP_EOL);
308+
}
309+
}
310+
}
311+
}
312+
if (count($rs->getAggregations()) > 0){
313+
foreach ($rs->getAggregations() as $key=> $aggregation){
314+
printf("Aggregation Field:".$aggregation->getField().PHP_EOL);
315+
printf("Aggregation Operation:".$aggregation->getOperation().PHP_EOL);
316+
printf("Aggregation Value:".$aggregation->getValue().PHP_EOL);
317+
foreach ($aggregation->getGroup() as $group ){
318+
printf("Aggregation Group Value:".$group->getValue().PHP_EOL);
319+
printf("Aggregation Group Count:".$group->getCount().PHP_EOL);
320+
}
321+
}
322+
}
323+
} catch (OssException $e) {
324+
printf(__FUNCTION__ . ": FAILED\n");
325+
printf($e->getMessage() . "\n");
326+
return;
327+
}
328+
print(__FUNCTION__ . ": OK" . "\n");
329+
330+
}
331+
/**
332+
* Close Meta Query
333+
*
334+
* @param OssClient $ossClient OssClient instance
335+
* @param string $bucket bucket name
336+
* @return null
337+
*/
338+
function closeMetaQuery($ossClient,$bucket){
339+
try {
340+
$ossClient->closeMetaQuery($bucket);
341+
} catch (OssException $e) {
342+
printf(__FUNCTION__ . ": FAILED\n");
343+
printf($e->getMessage() . "\n");
344+
return;
345+
}
346+
print(__FUNCTION__ . ": OK" . "\n");
347+
}

samples/RunAll.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
require_once __DIR__ . '/BucketReferer.php';
99
require_once __DIR__ . '/BucketLogging.php';
1010
require_once __DIR__ . '/BucketWebsite.php';
11+
require_once __DIR__ . '/BucketMetaQuery.php';
1112
require_once __DIR__ . '/Signature.php';
1213
require_once __DIR__ . '/Object1.php';
1314
require_once __DIR__ . '/MultipartUpload.php';

0 commit comments

Comments
 (0)