TestOSS.php 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: lionared
  5. * Date: 2018/4/24
  6. * Time: 下午2:28
  7. */
  8. define('BASE_ROOT_PATH',str_replace('/test','',dirname(__FILE__)));
  9. define('MOBILE_SERVER',true);
  10. require_once(BASE_ROOT_PATH. '/data/api/aliyun-oss-php-sdk-2.3.0/autoload.php');
  11. require_once(BASE_ROOT_PATH . '/fooder.php');
  12. use \OSS\OssClient;
  13. use \OSS\Core\OssException;
  14. class TestOSS extends PHPUnit_Framework_TestCase
  15. {
  16. static $ossConfig = [];
  17. public static function setUpBeforeClass()
  18. {
  19. Base::run_util();
  20. static::$ossConfig = [
  21. 'accessKeyId' => 'LTAIy9x1zb2qhNfI',
  22. 'accessKeySecret' => 'I5whhorQVfJji2yVk3N0xtFzL3sUr1',
  23. 'endPoint' => 'oss-cn-shenzhen.aliyuncs.com',
  24. ];
  25. }
  26. public function testOssClient()
  27. {
  28. $config = static::$ossConfig;
  29. $imagePath = "http://p1.pstatp.com/large/pgc-image/1523870471647f8800fad69";
  30. $bucket = 'lrlz-image';
  31. $filename = sha1($imagePath);
  32. ini_set('error_reporting', 0);
  33. $content = file_get_contents($imagePath);
  34. if (! $content) {
  35. return false;
  36. }
  37. $filetype = getImageType($content);
  38. if ($filetype == 'unknown') {
  39. return false;
  40. }
  41. $filename .= ".{$filetype}";
  42. try {
  43. $client = new OssClient($config['accessKeyId'], $config['accessKeySecret'], $config['endPoint']);
  44. $ret = $client->putObject($bucket, $filename, $content);
  45. if (! empty($ret)) {
  46. return $ret['oss-request-url'];
  47. }
  48. } catch (OssException $ex) {
  49. Log::record("upload image to Aliyun oss failed: {$ex->getMessage()}",Log::ERR);
  50. return false;
  51. }
  52. return false;
  53. }
  54. }