Signature.php 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. <?php
  2. require_once __DIR__ . '/Common.php';
  3. use NOS\Http\RequestCore;
  4. use NOS\Http\ResponseCore;
  5. use NOS\NosClient;
  6. use NOS\Core\NosException;
  7. $bucket = Common::getTestBucketName();
  8. $nosClient = Common::getNosClient();
  9. if (is_null($nosClient)) exit(1);
  10. //------------------------------------------------------------基本使用----------------------------------------
  11. $nosClient->uploadFile($bucket, "a.file", __FILE__);
  12. $signedUrl = $nosClient->signUrl($bucket, "a.file", 3600);
  13. Common::println($signedUrl);
  14. //-----------------------------------------------------------完整用法-------------------------------------------
  15. $nosClient->putObject($bucket, "test/test-signature-test-upload-and-download.txt", "nos php sample");
  16. getSignedUrlForGettingObject($nosClient,$bucket);
  17. function getSignedUrlForGettingObject($nosClient, $bucket)
  18. {
  19. $object = "test/test-signature-test-upload-and-download.txt";
  20. $timeout = 3600;
  21. try {
  22. $signedUrl = $nosClient->signUrl($bucket, $object, $timeout);
  23. } catch (NosException $e) {
  24. printf(__FUNCTION__ . ": FAILED\n");
  25. printf($e->getMessage() . "\n");
  26. return;
  27. }
  28. print(__FUNCTION__ . ": signedUrl: " . $signedUrl . "\n");
  29. /**
  30. * 可以类似的代码来访问签名的URL,也可以输入到浏览器中去访问
  31. */
  32. $request = new RequestCore($signedUrl);
  33. $request->set_method('GET');
  34. $request->add_header('Content-Type', '');
  35. $request->send_request();
  36. $res = new ResponseCore($request->get_response_header(), $request->get_response_body(), $request->get_response_code());
  37. if ($res->isOK()) {
  38. print(__FUNCTION__ . ": OK" . "\n");
  39. } else {
  40. print(__FUNCTION__ . ": FAILED" . "\n");
  41. };
  42. }