upload_tokens.php 2.4KB

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