diff --git a/src/Qiniu/Storage/BucketManager.php b/src/Qiniu/Storage/BucketManager.php index dc66eb2a..52fef82a 100644 --- a/src/Qiniu/Storage/BucketManager.php +++ b/src/Qiniu/Storage/BucketManager.php @@ -7,6 +7,7 @@ use Qiniu\Http\Error; use Qiniu\Http\Client; use Qiniu\Http\Proxy; +use Qiniu\Http\Response; /** * 主要涉及了空间资源管理及批量操作接口的实现,具体的接口规格可以参考 @@ -924,7 +925,20 @@ public function batch($operations) $scheme = "https://"; } $params = 'op=' . implode('&op=', $operations); - return $this->postV2($scheme . Config::RS_HOST . '/batch', $params); + $errResp = new Response(0, 0); + if (count($operations) <= 0) { + $errResp->error = 'empty operations'; + return array(null, new Error($scheme . '/batch', $errResp)); + } + $bucket = ''; + foreach ($operations as $op) { + $segments = explode('/', $op); + if (count($segments) < 3) { + continue; + } + list($bucket,) = \Qiniu\decodeEntry($segments[2]); + } + return $this->rsPost($bucket, '/batch', $params); } /** diff --git a/src/Qiniu/functions.php b/src/Qiniu/functions.php index 5e0402ac..cc258d7e 100644 --- a/src/Qiniu/functions.php +++ b/src/Qiniu/functions.php @@ -140,6 +140,16 @@ function entry($bucket, $key = null) return base64_urlSafeEncode($en); } + function decodeEntry($entry) + { + $en = base64_urlSafeDecode($entry); + $en = explode(':', $en); + if (count($en) == 1) { + return array($en[0], null); + } + return array($en[0], $en[1]); + } + /** * array 辅助方法,无值时不set * diff --git a/tests/Qiniu/Tests/BucketTest.php b/tests/Qiniu/Tests/BucketTest.php index d30b6748..f9e71d69 100755 --- a/tests/Qiniu/Tests/BucketTest.php +++ b/tests/Qiniu/Tests/BucketTest.php @@ -46,6 +46,7 @@ public static function prepareEnvironment() global $testAuth; $config = new Config(); + $config->setUcHost('kodo-qa.bucket.jfcs-k8s-qa2.qiniu.io'); self::$bucketManager = new BucketManager($testAuth, $config); global $dummyAuth; diff --git a/tests/Qiniu/Tests/EntryTest.php b/tests/Qiniu/Tests/EntryTest.php index ebed1875..73bfac4c 100644 --- a/tests/Qiniu/Tests/EntryTest.php +++ b/tests/Qiniu/Tests/EntryTest.php @@ -46,4 +46,43 @@ public function testKeyNeedReplaceSlashSymbol() $encodeEntryURI = Qiniu\entry($bucket, $key); $this->assertEquals('cWluaXVwaG90b3M6MDEydHM_YQ==', $encodeEntryURI); } + public function testDecodeEntry() + { + $entry = 'cWluaXVwaG90b3M6Z29nb3BoZXIuanBn'; + list($bucket, $key) = Qiniu\decodeEntry($entry); + $this->assertEquals('qiniuphotos', $bucket); + $this->assertEquals('gogopher.jpg', $key); + } + + public function testDecodeEntryWithEmptyKey() + { + $entry = 'cWluaXVwaG90b3M6'; + list($bucket, $key) = Qiniu\decodeEntry($entry); + $this->assertEquals('qiniuphotos', $bucket); + $this->assertEquals('', $key); + } + + public function testDecodeEntryWithNullKey() + { + $entry = 'cWluaXVwaG90b3M='; + list($bucket, $key) = Qiniu\decodeEntry($entry); + $this->assertEquals('qiniuphotos', $bucket); + $this->assertNull($key); + } + + public function testDecodeEntryWithPlusSymbol() + { + $entry = 'cWluaXVwaG90b3M6MDEydHM-YQ=='; + list($bucket, $key) = Qiniu\decodeEntry($entry); + $this->assertEquals('qiniuphotos', $bucket); + $this->assertEquals('012ts>a', $key); + } + + public function testDecodeEntryWithSlashSymbol() + { + $entry = 'cWluaXVwaG90b3M6MDEydHM_YQ=='; + list($bucket, $key) = Qiniu\decodeEntry($entry); + $this->assertEquals('qiniuphotos', $bucket); + $this->assertEquals('012ts?a', $key); + } }