Callback.php 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. <?php
  2. require_once __DIR__ . '/Common.php';
  3. use OSS\OssClient;
  4. $bucket = Common::getBucketName();
  5. $ossClient = Common::getOssClient();
  6. if (is_null($ossClient)) exit(1);
  7. //*******************************简单使用***************************************************************
  8. /** putObject 使用callback上传内容到oss文件
  9. * callbackurl参数指定请求回调的服务器url
  10. * callbackbodytype参数可为application/json或application/x-www-form-urlencoded, 可选参数,默认为application/x-www-form-urlencoded
  11. * OSS_CALLBACK_VAR参数可以不设置
  12. */
  13. $url =
  14. '{
  15. "callbackUrl":"callback.oss-demo.com:23450",
  16. "callbackHost":"oss-cn-hangzhou.aliyuncs.com",
  17. "callbackBody":"bucket=${bucket}&object=${object}&etag=${etag}&size=${size}&mimeType=${mimeType}&imageInfo.height=${imageInfo.height}&imageInfo.width=${imageInfo.width}&imageInfo.format=${imageInfo.format}&my_var1=${x:var1}&my_var2=${x:var2}",
  18. "callbackBodyType":"application/x-www-form-urlencoded"
  19. }';
  20. $var =
  21. '{
  22. "x:var1":"value1",
  23. "x:var2":"值2"
  24. }';
  25. $options = array(OssClient::OSS_CALLBACK => $url,
  26. OssClient::OSS_CALLBACK_VAR => $var
  27. );
  28. $result = $ossClient->putObject($bucket, "b.file", "random content", $options);
  29. Common::println($result['body']);
  30. Common::println($result['info']['http_code']);
  31. /**
  32. * completeMultipartUpload 使用callback上传内容到oss文件
  33. * callbackurl参数指定请求回调的服务器url
  34. * callbackbodytype参数可为application/json或application/x-www-form-urlencoded, 可选参数,默认为application/x-www-form-urlencoded
  35. * OSS_CALLBACK_VAR参数可以不设置
  36. */
  37. $object = "multipart-callback-test.txt";
  38. $copiedObject = "multipart-callback-test.txt.copied";
  39. $ossClient->putObject($bucket, $copiedObject, file_get_contents(__FILE__));
  40. /**
  41. * step 1. 初始化一个分块上传事件, 也就是初始化上传Multipart, 获取upload id
  42. */
  43. $upload_id = $ossClient->initiateMultipartUpload($bucket, $object);
  44. /**
  45. * step 2. uploadPartCopy
  46. */
  47. $copyId = 1;
  48. $eTag = $ossClient->uploadPartCopy($bucket, $copiedObject, $bucket, $object, $copyId, $upload_id);
  49. $upload_parts[] = array(
  50. 'PartNumber' => $copyId,
  51. 'ETag' => $eTag,
  52. );
  53. $listPartsInfo = $ossClient->listParts($bucket, $object, $upload_id);
  54. /**
  55. * step 3.
  56. */
  57. $json =
  58. '{
  59. "callbackUrl":"callback.oss-demo.com:23450",
  60. "callbackHost":"oss-cn-hangzhou.aliyuncs.com",
  61. "callbackBody":"{\"mimeType\":${mimeType},\"size\":${size},\"x:var1\":${x:var1},\"x:var2\":${x:var2}}",
  62. "callbackBodyType":"application/json"
  63. }';
  64. $var =
  65. '{
  66. "x:var1":"value1",
  67. "x:var2":"值2"
  68. }';
  69. $options = array(OssClient::OSS_CALLBACK => $json,
  70. OssClient::OSS_CALLBACK_VAR => $var);
  71. $result = $ossClient->completeMultipartUpload($bucket, $object, $upload_id, $upload_parts, $options);
  72. Common::println($result['body']);
  73. Common::println($result['info']['http_code']);