Skip to content

Commit e478db9

Browse files
committed
add rs get buckets, domains example
refractor the rs_copy, stat, change type example refractor the bucket manager add domains function to get domains of a bucket
1 parent 2cce799 commit e478db9

12 files changed

+148
-84
lines changed

examples/auth.php

-11
This file was deleted.

examples/file_copy.php

-28
This file was deleted.

examples/file_stat.php

-27
This file was deleted.

examples/get_bucket_domains.php

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
require_once __DIR__ . '/../autoload.php';
3+
4+
use \Qiniu\Auth;
5+
6+
$accessKey = getenv('QINIU_ACCESS_KEY');
7+
$secretKey = getenv('QINIU_SECRET_KEY');
8+
$bucket = getenv('QINIU_TEST_BUCKET');
9+
10+
11+
$auth = new Auth($accessKey, $secretKey);
12+
$config = new \Qiniu\Config();
13+
$bucketManager = new \Qiniu\Storage\BucketManager($auth, $config);
14+
list($domains, $err) = $bucketManager->domains($bucket);
15+
if ($err) {
16+
print_r($err);
17+
} else {
18+
print_r($domains);
19+
}

examples/get_buckets.php

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
require_once __DIR__ . '/../autoload.php';
3+
4+
use \Qiniu\Auth;
5+
6+
$accessKey = getenv('QINIU_ACCESS_KEY');
7+
$secretKey = getenv('QINIU_SECRET_KEY');
8+
$bucket = getenv('QINIU_TEST_BUCKET');
9+
10+
11+
$auth = new Auth($accessKey, $secretKey);
12+
$config = new \Qiniu\Config();
13+
$bucketManager = new \Qiniu\Storage\BucketManager($auth, $config);
14+
list($buckets, $err) = $bucketManager->buckets(true);
15+
if ($err) {
16+
print_r($err);
17+
} else {
18+
print_r($buckets);
19+
}

examples/rs_chtype.php

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
require_once __DIR__ . '/../autoload.php';
3+
4+
use \Qiniu\Auth;
5+
6+
$accessKey = getenv('QINIU_ACCESS_KEY');
7+
$secretKey = getenv('QINIU_SECRET_KEY');
8+
$bucket = getenv('QINIU_TEST_BUCKET');
9+
10+
$key = "qiniu.mp4";
11+
$auth = new Auth($accessKey, $secretKey);
12+
$config = new \Qiniu\Config();
13+
$bucketManager = new \Qiniu\Storage\BucketManager($auth, $config);
14+
15+
$fileType = 1;//0 表示普通存储,1表示低频存储
16+
17+
$err = $bucketManager->changeType($bucket, $key, $fileType);
18+
if ($err) {
19+
print_r($err);
20+
}

examples/rs_copy.php

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
require_once __DIR__ . '/../autoload.php';
3+
4+
use \Qiniu\Auth;
5+
6+
$accessKey = getenv('QINIU_ACCESS_KEY');
7+
$secretKey = getenv('QINIU_SECRET_KEY');
8+
$bucket = getenv('QINIU_TEST_BUCKET');
9+
10+
$key = "qiniu.mp4";
11+
$auth = new Auth($accessKey, $secretKey);
12+
$config = new \Qiniu\Config();
13+
$bucketManager = new \Qiniu\Storage\BucketManager($auth, $config);
14+
15+
$srcBucket = $bucket;
16+
$destBucket = $bucket;
17+
$srcKey = $key . "1";
18+
$destKey = $key . "_copy";
19+
$err = $bucketManager->copy($srcBucket, $srcKey, $destBucket, $destKey, true);
20+
if ($err) {
21+
print_r($err);
22+
}

examples/rs_stat.php

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
require_once __DIR__ . '/../autoload.php';
3+
4+
use \Qiniu\Auth;
5+
6+
$accessKey = getenv('QINIU_ACCESS_KEY');
7+
$secretKey = getenv('QINIU_SECRET_KEY');
8+
$bucket = getenv('QINIU_TEST_BUCKET');
9+
10+
$key="qiniu.mp4";
11+
$auth = new Auth($accessKey, $secretKey);
12+
$config = new \Qiniu\Config();
13+
$bucketManager = new \Qiniu\Storage\BucketManager($auth, $config);
14+
list($fileInfo,$err)=$bucketManager->stat($bucket, $key);
15+
if ($err) {
16+
print_r($err);
17+
} else {
18+
print_r($fileInfo);
19+
}

examples/rs_upload_token.php

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
use Qiniu\Auth;
4+
5+
$accessKey = getenv('QINIU_ACCESS_KEY');
6+
$secretKey = getenv('QINIU_SECRET_KEY');
7+
8+
//初始化Auth状态:
9+
$auth = new Auth($accessKey, $secretKey);

src/Qiniu/Auth.php

+2-13
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,7 @@ public function uploadToken(
7676
$key = null,
7777
$expires = 3600,
7878
$policy = null,
79-
$strictPolicy = true,
80-
Zone $zone = null
79+
$strictPolicy = true
8180
) {
8281
$deadline = time() + $expires;
8382
$scope = $bucket;
@@ -88,15 +87,6 @@ public function uploadToken(
8887
$args = self::copyPolicy($args, $policy, $strictPolicy);
8988
$args['scope'] = $scope;
9089
$args['deadline'] = $deadline;
91-
92-
if ($zone === null) {
93-
$zone = new Zone();
94-
}
95-
96-
list($upHosts, $err) = $zone->getUpHosts($this->accessKey, $bucket);
97-
if ($err === null) {
98-
$args['upHosts'] = $upHosts;
99-
}
10090

10191
$b = json_encode($args);
10292
return $this->signWithData($b);
@@ -131,8 +121,7 @@ public function uploadToken(
131121

132122
'deleteAfterDays',
133123
'fileType',
134-
135-
'upHosts',
124+
'isPrefixalScope',
136125
);
137126

138127
private static function copyPolicy(&$policy, $originPolicy, $strictPolicy)

src/Qiniu/Storage/BucketManager.php

+34-5
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,21 @@ public function __construct(Auth $auth, Config $config = null)
3434
*/
3535
public function buckets($shared = true)
3636
{
37-
return $this->rsGet('/buckets?shared=' . $shared);
37+
$includeShared = "false";
38+
if ($shared === true) {
39+
$includeShared = "true";
40+
}
41+
return $this->rsGet('/buckets?shared=' . $includeShared);
42+
}
43+
44+
/**
45+
* 获取指定空间绑定的所有的域名
46+
*
47+
* @return string[] 包含所有空间域名
48+
*/
49+
public function domains($bucket)
50+
{
51+
return $this->apiGet('/v6/domain/list?tbl=' . $bucket);
3852
}
3953

4054
/**
@@ -148,7 +162,7 @@ public function copy($from_bucket, $from_key, $to_bucket, $to_key, $force = fals
148162
$from = \Qiniu\entry($from_bucket, $from_key);
149163
$to = \Qiniu\entry($to_bucket, $to_key);
150164
$path = '/copy/' . $from . '/' . $to;
151-
if ($force) {
165+
if ($force === true) {
152166
$path .= '/force/true';
153167
}
154168
list(, $error) = $this->rsPost($path);
@@ -310,8 +324,8 @@ public function batch($operations)
310324
public function deleteAfterDays($bucket, $key, $days)
311325
{
312326
$entry = \Qiniu\entry($bucket, $key);
313-
$url = "/deleteAfterDays/$entry/$days";
314-
list(, $error) = $this->rsPost($url);
327+
$path = "/deleteAfterDays/$entry/$days";
328+
list(, $error) = $this->rsPost($path);
315329
return $error;
316330
}
317331

@@ -333,18 +347,33 @@ private function getRsHost()
333347
return $scheme . Config::RS_HOST;
334348
}
335349

350+
private function getApiHost()
351+
{
352+
$scheme = "http://";
353+
if ($this->config->useHTTPS == true) {
354+
$scheme = "https://";
355+
}
356+
return $scheme . Config::API_HOST;
357+
}
358+
336359
private function rsPost($path, $body = null)
337360
{
338361
$url = $this->getRsHost() . $path;
339362
return $this->post($url, $body);
340363
}
341364

365+
private function apiGet($path)
366+
{
367+
$url = $this->getApiHost() . $path;
368+
return $this->get($url);
369+
}
370+
342371
private function rsGet($path)
343372
{
344373
$url = $this->getRsHost() . $path;
345374
return $this->get($url);
346375
}
347-
376+
348377
private function get($url)
349378
{
350379
$headers = $this->auth->authorization($url);

test-env.sh

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export QINIU_ACCESS_KEY=xxx
2+
export QINIU_SECRET_KEY=xxx
3+
export QINIU_TEST_BUCKET=phpsdk
4+
export QINIU_TEST_DOMAIN=phpsdk.qiniudn.com

0 commit comments

Comments
 (0)