upload_multi_demos.php 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <?php
  2. require_once __DIR__ . '/../autoload.php';
  3. use Qiniu\Auth;
  4. use Qiniu\Storage\UploadManager;
  5. // 控制台获取密钥:https://portal.qiniu.com/user/key
  6. $accessKey = getenv('QINIU_ACCESS_KEY');
  7. $secretKey = getenv('QINIU_SECRET_KEY');
  8. $bucket = getenv('QINIU_TEST_BUCKET');
  9. // 用户默认没有私有队列,需要在这里创建然后填写 https://portal.qiniu.com/dora/media-gate/pipeline
  10. $pipeline = 'sdktest';
  11. $auth = new Auth($accessKey, $secretKey);
  12. $token = $auth->uploadToken($bucket);
  13. $uploadMgr = new UploadManager();
  14. //---------------------------------------- upload demo1 ----------------------------------------
  15. // 上传字符串到七牛
  16. list($ret, $err) = $uploadMgr->put($token, null, 'content string');
  17. echo "\n====> put result: \n";
  18. if ($err !== null) {
  19. var_dump($err);
  20. } else {
  21. var_dump($ret);
  22. }
  23. //---------------------------------------- upload demo2 ----------------------------------------
  24. // 上传文件到七牛
  25. $filePath = './php-logo.png';
  26. $key = 'php-logo.png';
  27. list($ret, $err) = $uploadMgr->putFile($token, $key, $filePath);
  28. echo "\n====> putFile result: \n";
  29. if ($err !== null) {
  30. var_dump($err);
  31. } else {
  32. var_dump($ret);
  33. }
  34. //---------------------------------------- upload demo3 ----------------------------------------
  35. // 上传文件到七牛后, 七牛将文件名和文件大小回调给业务服务器.
  36. // 可参考文档: https://developer.qiniu.com/kodo/manual/1206/put-policy
  37. $policy = array(
  38. 'callbackUrl' => 'http://172.30.251.210/upload_verify_callback.php',
  39. 'callbackBody' => 'filename=$(fname)&filesize=$(fsize)'
  40. // 'callbackBodyType' => 'application/json',
  41. // 'callbackBody' => '{"filename":$(fname), "filesize": $(fsize)}' //设置application/json格式回调
  42. );
  43. $token = $auth->uploadToken($bucket, null, 3600, $policy);
  44. list($ret, $err) = $uploadMgr->putFile($token, null, $key);
  45. echo "\n====> putFile result: \n";
  46. if ($err !== null) {
  47. var_dump($err);
  48. } else {
  49. var_dump($ret);
  50. }
  51. //---------------------------------------- upload demo4 ----------------------------------------
  52. // 上传视频,上传完成后进行 m3u8 的转码, 并给视频打水印
  53. $wmImg = Qiniu\base64_urlSafeEncode('http://devtools.qiniudn.com/qiniu.png');
  54. $pfop = "avthumb/m3u8/wmImage/$wmImg";
  55. // 转码完成后回调到业务服务器。(公网可以访问,并相应 200 OK)
  56. $notifyUrl = 'http://notify.fake.com';
  57. $policy = array(
  58. 'persistentOps' => $pfop,
  59. 'persistentNotifyUrl' => $notifyUrl,
  60. 'persistentPipeline' => $pipeline
  61. );
  62. $token = $auth->uploadToken($bucket, null, 3600, $policy);
  63. print($token);
  64. list($ret, $err) = $uploadMgr->putFile($token, null, $key);
  65. echo "\n====> putFile result: \n";
  66. if ($err !== null) {
  67. var_dump($err);
  68. } else {
  69. var_dump($ret);
  70. }