Skip to content

Commit 8c82c31

Browse files
committed
Merge pull request #17 from rwifeng/batch
add batch ops
2 parents 45259d5 + 5371693 commit 8c82c31

File tree

2 files changed

+82
-0
lines changed

2 files changed

+82
-0
lines changed

qiniu/rs.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,5 +176,47 @@ function Qiniu_RS_Copy($self, $bucketSrc, $keySrc, $bucketDest, $keyDest) // =>
176176
return Qiniu_Client_CallNoRet($self, $QINIU_RS_HOST . $uri);
177177
}
178178

179+
179180
// ----------------------------------------------------------
181+
//batch
182+
183+
function Qiniu_RS_Batch($self, $ops) // => ($data, $error)
184+
{
185+
global $QINIU_RS_HOST;
186+
$url = $QINIU_RS_HOST . '/batch';
187+
$params = 'op=' . implode('&op=', $ops);
188+
return Qiniu_Client_CallWithForm($self, $url, $params);
189+
}
190+
191+
function Qiniu_RS_BatchStat($self, $entryPaths)
192+
{
193+
foreach ($entryPaths as $entryPath) {
194+
$params[] = Qiniu_RS_URIStat($entryPath->bucket, $entryPath->key);
195+
}
196+
return Qiniu_RS_Batch($self,$params);
197+
}
198+
199+
function Qiniu_RS_BatchDelete($self, $entryPaths)
200+
{
201+
foreach ($entryPaths as $entryPath) {
202+
$params[] = Qiniu_RS_URIDelete($entryPath->bucket, $entryPath->key);
203+
}
204+
return Qiniu_RS_Batch($self, $params);
205+
}
206+
207+
function Qiniu_RS_BatchMove($self, $entryPairs)
208+
{
209+
foreach ($entryPairs as $entryPair) {
210+
$params[] = Qiniu_RS_URIMove($entryPair->src->bucket, $entryPair->src->key, $entryPair->dest->bucket, $entryPair->dest->key);
211+
}
212+
return Qiniu_RS_Batch($self, $params);
213+
}
214+
215+
function Qiniu_RS_BatchCopy($self, $entryPairs)
216+
{
217+
foreach ($entryPairs as $entryPair) {
218+
$params[] = Qiniu_RS_URICopy($entryPair->src->bucket, $entryPair->src->key, $entryPair->dest->bucket, $entryPair->dest->key);
219+
}
220+
return Qiniu_RS_Batch($self, $params);
221+
}
180222

tests/RsTest.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,5 +43,45 @@ public function testDeleteMoveCopy()
4343
$err = Qiniu_RS_Delete($this->client, $this->bucket, $key2);
4444
$this->assertNotNull($err, "delete key2 false");
4545
}
46+
47+
public function testBatchStat()
48+
{
49+
$key2 = 'testOp2' . getTid();
50+
Qiniu_RS_Delete($this->client, $this->bucket, $key2);
51+
$entries = array(new Qiniu_RS_EntryPath($this->bucket, $this->key), new Qiniu_RS_EntryPath($this->bucket, $key2));
52+
list($ret, $err) = Qiniu_RS_BatchStat($this->client, $entries);
53+
$this->assertNotNull($err);
54+
$this->assertEquals($ret[0]['code'], 200);
55+
$this->assertEquals($ret[1]['code'], 612);
56+
}
57+
58+
public function testBatchDeleteMoveCopy()
59+
{
60+
$key2 = 'testOp2' . getTid();
61+
$key3 = 'testOp3' . getTid();
62+
$key4 = 'testOp4' . getTid();
63+
$e1 = new Qiniu_RS_EntryPath($this->bucket, $this->key);
64+
$e2 = new Qiniu_RS_EntryPath($this->bucket, $key2);
65+
$e3 = new Qiniu_RS_EntryPath($this->bucket, $key3);
66+
$e4 = new Qiniu_RS_EntryPath($this->bucket, $key4);
67+
Qiniu_RS_BatchDelete($this->client, array($e2, $e3,$e4));
68+
69+
$entryPairs = array(new Qiniu_RS_EntryPathPair($e1, $e2), new Qiniu_RS_EntryPathPair($e1, $e3));
70+
list($ret, $err) = Qiniu_RS_BatchCopy($this->client, $entryPairs);
71+
$this->assertNull($err);
72+
$this->assertEquals($ret[0]['code'], 200);
73+
$this->assertEquals($ret[1]['code'], 200);
74+
75+
list($ret, $err) = Qiniu_RS_BatchMove($this->client, array(new Qiniu_RS_EntryPathPair($e2, $e4)));
76+
$this->assertNull($err);
77+
$this->assertEquals($ret[0]['code'], 200);
78+
79+
list($ret, $err) = Qiniu_RS_BatchDelete($this->client, array($e3, $e4));
80+
$this->assertNull($err);
81+
$this->assertEquals($ret[0]['code'], 200);
82+
$this->assertEquals($ret[1]['code'], 200);
83+
84+
Qiniu_RS_BatchDelete($this->client, array($e2, $e3, $e4));
85+
}
4686
}
4787

0 commit comments

Comments
 (0)