12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364 |
- <?php
- /**
- * Created by PhpStorm.
- * User: lionared
- * Date: 2018/4/24
- * Time: 下午2:28
- */
- define('BASE_ROOT_PATH',str_replace('/test','',dirname(__FILE__)));
- define('MOBILE_SERVER',true);
- require_once(BASE_ROOT_PATH. '/data/api/aliyun-oss-php-sdk-2.3.0/autoload.php');
- require_once(BASE_ROOT_PATH . '/fooder.php');
- use \OSS\OssClient;
- use \OSS\Core\OssException;
- class TestOSS extends PHPUnit_Framework_TestCase
- {
- static $ossConfig = [];
- public static function setUpBeforeClass()
- {
- Base::run_util();
- static::$ossConfig = [
- 'accessKeyId' => 'LTAIy9x1zb2qhNfI',
- 'accessKeySecret' => 'I5whhorQVfJji2yVk3N0xtFzL3sUr1',
- 'endPoint' => 'oss-cn-shenzhen.aliyuncs.com',
- ];
- }
- public function testOssClient()
- {
- $config = static::$ossConfig;
- $imagePath = "http://p1.pstatp.com/large/pgc-image/1523870471647f8800fad69";
- $bucket = 'lrlz-image';
- $filename = sha1($imagePath);
- ini_set('error_reporting', 0);
- $content = file_get_contents($imagePath);
- if (! $content) {
- return false;
- }
- $filetype = getImageType($content);
- if ($filetype == 'unknown') {
- return false;
- }
- $filename .= ".{$filetype}";
- try {
- $client = new OssClient($config['accessKeyId'], $config['accessKeySecret'], $config['endPoint']);
- $ret = $client->putObject($bucket, $filename, $content);
- if (! empty($ret)) {
- return $ret['oss-request-url'];
- }
- } catch (OssException $ex) {
- Log::record("upload image to Aliyun oss failed: {$ex->getMessage()}",Log::ERR);
- return false;
- }
- return false;
- }
- }
|