upload_tokens.php 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. <?php
  2. require_once __DIR__ . '/../autoload.php';
  3. use Qiniu\Auth;
  4. // 控制台获取密钥:https://portal.qiniu.com/user/key
  5. $accessKey = getenv('QINIU_ACCESS_KEY');
  6. $secretKey = getenv('QINIU_SECRET_KEY');
  7. $bucket = getenv('QINIU_TEST_BUCKET');
  8. // 初始化 Auth 状态
  9. $auth = new Auth($accessKey, $secretKey);
  10. //---------------------------------------- demo1 ----------------------------------------
  11. // 设置凭证有效期(单位秒)
  12. $expires = 3600;
  13. $policy = null;
  14. $upToken = $auth->uploadToken($bucket, null, $expires, $policy, true);
  15. print($upToken . "\n");
  16. //---------------------------------------- demo2 ----------------------------------------
  17. // 自定义凭证有效期(示例2小时)
  18. $expires = 7200;
  19. $upToken = $auth->uploadToken($bucket, null, $expires, $policy, true);
  20. print($upToken . "\n");
  21. //---------------------------------------- demo3 ----------------------------------------
  22. // 覆盖上传凭证
  23. $expires = 3600;
  24. $keyToOverwrite = 'qiniu.mp4';
  25. $upToken = $auth->uploadToken($bucket, $keyToOverwrite, $expires, $policy, true);
  26. print($upToken . "\n");
  27. //---------------------------------------- demo4 ----------------------------------------
  28. // 自定义上传回复(非callback模式)凭证
  29. $returnBody = '{"key":"$(key)","hash":"$(etag)","fsize":$(fsize),"bucket":"$(bucket)","name":"$(x:name)"}';
  30. $policy = array(
  31. 'returnBody' => $returnBody
  32. );
  33. $upToken = $auth->uploadToken($bucket, null, $expires, $policy, true);
  34. print($upToken . "\n");
  35. //---------------------------------------- demo5 ----------------------------------------
  36. // 带回调业务服务器的凭证(application/json)
  37. $policy = array(
  38. 'callbackUrl' => 'http://api.example.com/qiniu/upload/callback',
  39. 'callbackBody' => '{"key":"$(key)","hash":"$(etag)","fsize":$(fsize),"bucket":"$(bucket)","name":"$(x:name)"}',
  40. 'callbackBodyType' => 'application/json'
  41. );
  42. $upToken = $auth->uploadToken($bucket, null, $expires, $policy, true);
  43. print($upToken . "\n");
  44. //---------------------------------------- demo6 ----------------------------------------
  45. // 带回调业务服务器的凭证(application/x-www-form-urlencoded)
  46. $policy = array(
  47. 'callbackUrl' => 'http://api.example.com/qiniu/upload/callback',
  48. 'callbackBody' => 'key=$(key)&hash=$(etag)&bucket=$(bucket)&fsize=$(fsize)&name=$(x:name)'
  49. );
  50. $upToken = $auth->uploadToken($bucket, null, $expires, $policy, true);
  51. print($upToken . "\n");
  52. //---------------------------------------- demo7 ----------------------------------------
  53. // 带数据处理的凭证
  54. $saveMp4Entry = \Qiniu\base64_urlSafeEncode($bucket . ":avthumb_test_target.mp4");
  55. $saveJpgEntry = \Qiniu\base64_urlSafeEncode($bucket . ":vframe_test_target.jpg");
  56. $avthumbMp4Fop = "avthumb/mp4|saveas/" . $saveMp4Entry;
  57. $vframeJpgFop = "vframe/jpg/offset/1|saveas/" . $saveJpgEntry;
  58. $policy = array(
  59. 'persistentOps' => $avthumbMp4Fop . ";" . $vframeJpgFop,
  60. 'persistentPipeline' => "video-pipe",
  61. 'persistentNotifyUrl' => "http://api.example.com/qiniu/pfop/notify",
  62. );
  63. $upToken = $auth->uploadToken($bucket, null, $expires, $policy, true);
  64. print($upToken . "\n");