Skip to content

Commit d0e3902

Browse files
committed
demos => examples
1 parent 776b931 commit d0e3902

File tree

10 files changed

+332
-0
lines changed

10 files changed

+332
-0
lines changed

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 = 'XI0n2kV1LYwzcxqSZQxJ7bpycxDIAXFGJMWUt_zG';
8+
$secretKey = '9WTmIAiwKQ2Nq6o93mfKd6VQqq56HjjLZonMWLJl';
9+
10+
//初始化Auth状态:
11+
$auth = new Auth($accessKey, $secretKey);
12+
13+
//初始化BucketManager
14+
$bucketMgr = new BucketManager($auth);
15+
16+
//你要测试的空间, 并且这个key在你空间中存在
17+
$bucket = 'rwxf';
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 = 'QWYn5TFQsLLU1pL5MFEmX3s5DmHdUThav9WyOWOm';
7+
$secretKey = 'Bxckh6FA-Fbs9Yt3i3cbKVK22UPBmAOHJcL95pGz';
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 = 'XI0n2kV1LYwzcxqSZQxJ7bpycxDIAXFGJMWUt_zG';
7+
$secretKey = '9WTmIAiwKQ2Nq6o93mfKd6VQqq56HjjLZonMWLJl';
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 = 'XI0n2kV1LYwzcxqSZQxJ7bpycxDIAXFGJMWUt_zG';
8+
$secretKey = '9WTmIAiwKQ2Nq6o93mfKd6VQqq56HjjLZonMWLJl';
9+
10+
$auth = new Auth($accessKey, $secretKey);
11+
$bmgr = new BucketManager($auth);
12+
13+
$url = 'http://php.net/favicon.ico';
14+
$bucket = 'rwxf';
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 = 'XI0n2kV1LYwzcxqSZQxJ7bpycxDIAXFGJMWUt_zG';
8+
$secretKey = '9WTmIAiwKQ2Nq6o93mfKd6VQqq56HjjLZonMWLJl';
9+
$auth = new Auth($accessKey, $secretKey);
10+
$bucketMgr = new BucketManager($auth);
11+
12+
$bucket = 'rwxf';
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 = 'XI0n2kV1LYwzcxqSZQxJ7bpycxDIAXFGJMWUt_zG';
12+
$secretKey = '9WTmIAiwKQ2Nq6o93mfKd6VQqq56HjjLZonMWLJl';
13+
$auth = new Auth($accessKey, $secretKey);
14+
15+
16+
$bucket = 'rwxf';
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://phpsdk.qiniudn.com/php-logo.png';
26+
$url2 = 'http://phpsdk.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 = 'XI0n2kV1LYwzcxqSZQxJ7bpycxDIAXFGJMWUt_zG';
10+
$secretKey = '9WTmIAiwKQ2Nq6o93mfKd6VQqq56HjjLZonMWLJl';
11+
$auth = new Auth($accessKey, $secretKey);
12+
13+
//要转码的文件所在的空间和文件名。
14+
$bucket = 'rwxf';
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

examples/up.php

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
<?php
2+
require_once '../autoload.php';
3+
4+
use Qiniu\Auth;
5+
use Qiniu\Storage\UploadManager;
6+
7+
$accessKey = 'QWYn5TFQsLLU1pL5MFEmX3s5DmHdUThav9WyOWOm';
8+
$secretKey = 'Bxckh6FA-Fbs9Yt3i3cbKVK22UPBmAOHJcL95pGz';
9+
$auth = new Auth($accessKey, $secretKey);
10+
11+
$bucket = 'phpsdk';
12+
$token = $auth->uploadToken($bucket);
13+
$uploadMgr = new UploadManager();
14+
15+
//----------------------------------------upload demo1 ----------------------------------------
16+
// 上传字符串到七牛
17+
list($ret, $err) = $uploadMgr->put($token, null, 'content string');
18+
echo "\n====> put result: \n";
19+
if ($err !== null) {
20+
var_dump($err);
21+
} else {
22+
var_dump($ret);
23+
}
24+
25+
26+
//----------------------------------------upload demo2 ----------------------------------------
27+
// 上传文件到七牛
28+
$filePath = './php-logo.png';
29+
$key = 'php-logo.png';
30+
list($ret, $err) = $uploadMgr->putFile($token, $key, $filePath);
31+
echo "\n====> putFile result: \n";
32+
if ($err !== null) {
33+
var_dump($err);
34+
} else {
35+
var_dump($ret);
36+
}
37+
38+
39+
//----------------------------------------upload demo3 ----------------------------------------
40+
// 上传文件到七牛后, 七牛将文件名和文件大小回调给业务服务器.
41+
// 可参考文档: http://developer.qiniu.com/docs/v6/api/reference/security/put-policy.html
42+
$policy = array(
43+
'callbackUrl' => 'http://172.30.251.210/callback.php',
44+
'callbackBody' => 'filename=$(fname)&filesize=$(fsize)'
45+
// 'callbackBodyType' => 'application/json',
46+
// 'callbackBody' => '{"filename":$(fname), "filesize": $(fsize)}' //设置application/json格式回调
47+
);
48+
$token = $auth->uploadToken($bucket, null, 3600, $policy);
49+
50+
51+
list($ret, $err) = $uploadMgr->putFile($token, null, $key);
52+
echo "\n====> putFile result: \n";
53+
if ($err !== null) {
54+
var_dump($err);
55+
} else {
56+
var_dump($ret);
57+
}
58+
59+
60+
//----------------------------------------upload demo4 ----------------------------------------
61+
//上传视频,上传完成后进行m3u8的转码, 并给视频打水印
62+
$wmImg = Qiniu\base64_urlSafeEncode('http://rwxf.qiniudn.com/logo-s.png');
63+
$pfop = "avthumb/m3u8/wmImage/$wmImg";
64+
65+
//转码完成后回调到业务服务器。(公网可以访问,并相应200 OK)
66+
$notifyUrl = 'http://notify.fake.com';
67+
68+
//独立的转码队列:https://portal.qiniu.com/mps/pipeline
69+
$pipeline = 'abc';
70+
71+
$policy = array(
72+
'persistentOps' => $pfop,
73+
'persistentNotifyUrl' => $notifyUrl,
74+
'persistentPipeline' => $pipeline
75+
);
76+
$token = $auth->uploadToken($bucket, null, 3600, $policy);
77+
78+
list($ret, $err) = $uploadMgr->putFile($token, null, $key);
79+
echo "\n====> putFile result: \n";
80+
if ($err !== null) {
81+
var_dump($err);
82+
} else {
83+
var_dump($ret);
84+
}

examples/upload_token.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
require_once '../autoload.php';
3+
4+
use Qiniu\Auth;
5+
6+
$accessKey = 'eSnBeEIyUqGGtidOTmsgQCwE23gjUDNJlsI6_mz9';
7+
$secretKey = 'd4eyXtO4JF_XaLkpNAWHnzygOcBbkx_Ywlhi8sKr';
8+
$auth = new Auth($accessKey, $secretKey);
9+
10+
$bucket = 'devtest';
11+
$upToken = $auth->uploadToken($bucket);
12+
13+
echo $upToken;

0 commit comments

Comments
 (0)