upload_multi_demos.php 2.6KB

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