Skip to content

Commit d1fa3c1

Browse files
committed
Merge pull request #142 from rwifeng/add_demos
Add demos
2 parents a7c996c + 407ab17 commit d1fa3c1

File tree

12 files changed

+342
-0
lines changed

12 files changed

+342
-0
lines changed

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ before_script:
1313
- travis_retry composer install --no-interaction --prefer-source --dev
1414
script:
1515
- ./vendor/bin/phpcs --standard=PSR2 src
16+
- ./vendor/bin/phpcs --standard=PSR2 examples
1617
- ./vendor/bin/phpcs --standard=PSR2 tests
1718
- ./vendor/bin/phpunit --coverage-text --coverage-clover=coverage.clover tests/Qiniu/Tests/
1819
after_script:

autoload.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
$path = __DIR__ . DIRECTORY_SEPARATOR . 'src';
4+
set_include_path(get_include_path() . PATH_SEPARATOR . $path);
5+
6+
spl_autoload_extensions('.php');
7+
spl_autoload_register();
8+
9+
require_once 'src/Qiniu/functions.php';

examples/bucket_mgr.php

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
require_once '../autoload.php';
3+
4+
use Qiniu\Auth;
5+
use Qiniu\Storage\BucketManager;
6+
7+
$accessKey = 'Access_Key';
8+
$secretKey = 'Secret_Key';
9+
10+
//初始化Auth状态:
11+
$auth = new Auth($accessKey, $secretKey);
12+
13+
//初始化BucketManager
14+
$bucketMgr = new BucketManager($auth);
15+
16+
//你要测试的空间, 并且这个key在你空间中存在
17+
$bucket = 'Bucket_Name';
18+
$key = 'php-logo.png';
19+
20+
//获取文件的状态信息
21+
list($ret, $err) = $bucketMgr->stat($bucket, $key);
22+
echo "\n====> $key stat : \n";
23+
if ($err !== null) {
24+
var_dump($err);
25+
} else {
26+
var_dump($ret);
27+
}
28+
29+
//将文件从文件$key 复制到文件$key2。 可以在不同bucket复制
30+
$key2 = 'php-logo2.png';
31+
$err = $bucketMgr->copy($bucket, $key, $bucket, $key2);
32+
echo "\n====> copy $key to $key2 : \n";
33+
if ($err !== null) {
34+
var_dump($err);
35+
} else {
36+
echo "Success!";
37+
}
38+
39+
//将文件从文件$key2 移动到文件$key3。 可以在不同bucket移动
40+
$key3 = 'php-logo3.png';
41+
$err = $bucketMgr->move($bucket, $key2, $bucket, $key3);
42+
echo "\n====> move $key2 to $key3 : \n";
43+
if ($err !== null) {
44+
var_dump($err);
45+
} else {
46+
echo "Success!";
47+
}
48+
49+
//删除$bucket 中的文件 $key
50+
$err = $bucketMgr->delete($bucket, $key3);
51+
echo "\n====> delete $key3 : \n";
52+
if ($err !== null) {
53+
var_dump($err);
54+
} else {
55+
echo "Success!";
56+
}

examples/callback.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
require_once '../autoload.php';
3+
4+
use Qiniu\Auth;
5+
6+
$accessKey = 'Access_Key';
7+
$secretKey = 'Secret_Key';
8+
$auth = new Auth($accessKey, $secretKey);
9+
10+
//获取回调的body信息
11+
$callbackBody = file_get_contents('php://input');
12+
13+
//回调的contentType
14+
$contentType = 'application/x-www-form-urlencoded';
15+
16+
//回调的签名信息,可以验证该回调是否来自七牛
17+
$authorization = $_SERVER['HTTP_AUTHORIZATION'];
18+
19+
//七牛回调的url,具体可以参考:http://developer.qiniu.com/docs/v6/api/reference/security/put-policy.html
20+
$url = 'http://172.30.251.210/callback.php';
21+
22+
$isQiniuCallback = $auth->verifyCallback($contentType, $authorization, $url, $callbackBody);
23+
24+
if ($isQiniuCallback) {
25+
$resp = array('ret' => 'success');
26+
} else {
27+
$resp = array('ret' => 'failed');
28+
}
29+
30+
echo json_encode($resp);

examples/download_token.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
require_once '../autoload.php';
3+
4+
use Qiniu\Auth;
5+
6+
$accessKey = 'Access_Key';
7+
$secretKey = 'Secret_Key';
8+
9+
// 构建Auth对象
10+
$auth = new Auth($accessKey, $secretKey);
11+
12+
// 私有空间中的外链 http://<domain>/<file_key>
13+
$baseUrl = 'http://sslayer.qiniudn.com/1.jpg?imageView2/1/h/500';
14+
// 对链接进行签名
15+
$signedUrl = $auth->privateDownloadUrl($baseUrl);
16+
17+
echo $signedUrl;

examples/fetch.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
require_once '../autoload.php';
3+
4+
use Qiniu\Auth;
5+
use Qiniu\Storage\BucketManager;
6+
7+
$accessKey = 'Access_Key';
8+
$secretKey = 'Secret_Key';
9+
10+
$auth = new Auth($accessKey, $secretKey);
11+
$bmgr = new BucketManager($auth);
12+
13+
$url = 'http://php.net/favicon.ico';
14+
$bucket = 'Bucket_Name';
15+
$key = time() . '.ico';
16+
17+
list($ret, $err) = $bmgr->fetch($url, $bucket, $key);
18+
echo "=====> fetch $url to bucket: $bucket key: $key\n";
19+
if ($err !== null) {
20+
var_dump($err);
21+
} else {
22+
echo 'Success';
23+
}

examples/list_file.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
require_once '../autoload.php';
3+
4+
use Qiniu\Auth;
5+
use Qiniu\Storage\BucketManager;
6+
7+
$accessKey = 'Access_Key';
8+
$secretKey = 'Secret_Key';
9+
$auth = new Auth($accessKey, $secretKey);
10+
$bucketMgr = new BucketManager($auth);
11+
12+
$bucket = 'Bucket_Name';
13+
$prefix = '';
14+
$marker = '';
15+
$limit = 3;
16+
17+
list($iterms, $marker, $err) = $bucketMgr->listFiles($bucket, $prefix, $marker, $limit);
18+
if ($err !== null) {
19+
echo "\n====> list file err: \n";
20+
var_dump($err);
21+
} else {
22+
echo "Marker: $marker\n";
23+
echo "\nList Iterms====>\n";
24+
var_dump($iterms);
25+
}

examples/mkzip.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
require_once '../autoload.php';
3+
4+
use Qiniu\Auth;
5+
use Qiniu\Processing\PersistentFop;
6+
7+
// 去我们的portal 后台来获取AK, SK
8+
$accessKey = 'access key';
9+
$secretKey = 'secret key';
10+
11+
$accessKey = 'Access_Key';
12+
$secretKey = 'Secret_Key';
13+
$auth = new Auth($accessKey, $secretKey);
14+
15+
16+
$bucket = 'Bucket_Name';
17+
$key = '1.png';
18+
19+
// 异步任务的队列, 去后台新建: https://portal.qiniu.com/mps/pipeline
20+
$pipeline = 'abc';
21+
22+
$pfop = new PersistentFop($auth, $bucket, $pipeline);
23+
24+
// 进行zip压缩的url
25+
$url1 = 'http://Bucket_Name.qiniudn.com/php-logo.png';
26+
$url2 = 'http://Bucket_Name.qiniudn.com/php-sdk.html';
27+
28+
//压缩后的key
29+
$zipKey = 'test.zip';
30+
31+
$fops = 'mkzip/2/url/' . \Qiniu\base64_urlSafeEncode($url1);
32+
$fops .= '/url/' . \Qiniu\base64_urlSafeEncode($url2);
33+
$fops .= '|saveas/' . \Qiniu\base64_urlSafeEncode("$bucket:$zipKey");
34+
35+
list($id, $err) = $pfop->execute($key, $fops);
36+
37+
echo "\n====> pfop mkzip result: \n";
38+
if ($err != null) {
39+
var_dump($err);
40+
} else {
41+
echo "PersistentFop Id: $id\n";
42+
43+
$res = "http://api.qiniu.com/status/get/prefop?id=$id";
44+
echo "Processing result: $res";
45+
}

examples/pfop.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
require_once '../autoload.php';
3+
4+
use Qiniu\Auth;
5+
use Qiniu\Processing\PersistentFop;
6+
7+
//对已经上传到七牛的视频发起异步转码操作
8+
9+
$accessKey = 'Access_Key';
10+
$secretKey = 'Secret_Key';
11+
$auth = new Auth($accessKey, $secretKey);
12+
13+
//要转码的文件所在的空间和文件名。
14+
$bucket = 'Bucket_Name';
15+
$key = '1.mp4';
16+
17+
//转码是使用的队列名称。 https://portal.qiniu.com/mps/pipeline
18+
$pipeline = 'abc';
19+
$pfop = new PersistentFop($auth, $bucket, $pipeline);
20+
21+
//要进行转码的转码操作。 http://developer.qiniu.com/docs/v6/api/reference/fop/av/avthumb.html
22+
$fops = "avthumb/mp4/s/640x360/vb/1.25m";
23+
24+
list($id, $err) = $pfop->execute($key, $fops);
25+
echo "\n====> pfop avthumb result: \n";
26+
if ($err != null) {
27+
var_dump($err);
28+
} else {
29+
echo "PersistentFop Id: $id\n";
30+
}
31+
32+
//查询转码的进度和状态
33+
list($ret, $err) = $pfop->status($id);
34+
echo "\n====> pfop avthumb status: \n";
35+
if ($err != null) {
36+
var_dump($err);
37+
} else {
38+
var_dump($ret);
39+
}

examples/php-logo.png

63.5 KB
Loading

0 commit comments

Comments
 (0)