/** * 当个上传文件并返回上传的URL地址 * @param $filename string 要保存的文件名称 注意如果存在目录的话 * @param $filepath string 文件在服务器(本地的绝对或者相对路径,这里的服务器实际上是本地)上的路径 * @return bool or url */ public function upload2AliyunSdkSingle($filename, $filepath) { $dir = C('ALIYUN_DIR'); //保存在阿里云的基本路径 $endpoint = C('ALIYUN_endpoint'); //阿里云的节点信息 请自行查阅本字段意义 $midStr = 'upload/' . date('y') . '/' . date('m') . '/' . date('d') . '/'; //这里是为了保存的路径暂时以当前时间为准 try { $ossClient = new OssClient(C('ALIYUN_KeyId'), C('ALIYUN_KeySecret'), $endpoint); //这里要放入你从阿里云获得的key和secret这里我做成了配置项读取 } catch (OssException $e) { print $e->getErrorMessage(); } $ossClient->setTimeout(3600); // 设置请求超时时间 $ossClient->setConnectTimeout(10); //设置连接超时时间 try { $ossClient->uploadFile(C('ALIYUN_bucket'), $dir . $midStr . $filename, $filepath); } catch (OssException $e) { return false; } $ret = str_replace('http://', 'http://' . C('ALIYUN_bucket') . '.', $endpoint . '/' . $dir . $midStr . $filename); //返回拼接的http url return $ret; }
public function testConstrunct() { try { $ossClient = new OssClient(Config::OSS_ACCESS_ID, Config::OSS_ACCESS_KEY, Config::OSS_ENDPOINT); $this->assertFalse($ossClient->isUseSSL()); $ossClient->setUseSSL(true); $this->assertTrue($ossClient->isUseSSL()); $this->assertTrue(true); $this->assertEquals(3, $ossClient->getMaxRetries()); $ossClient->setMaxTries(4); $this->assertEquals(4, $ossClient->getMaxRetries()); $ossClient->setTimeout(10); $ossClient->setConnectTimeout(20); } catch (OssException $e) { assertFalse(true); } }
public function testConstrunct() { try { $ossClient = new OssClient('id', 'key', 'http://oss-cn-hangzhou.aliyuncs.com'); $this->assertFalse($ossClient->isUseSSL()); $ossClient->setUseSSL(true); $this->assertTrue($ossClient->isUseSSL()); $this->assertTrue(true); $this->assertEquals(3, $ossClient->getMaxRetries()); $ossClient->setMaxTries(4); $this->assertEquals(4, $ossClient->getMaxRetries()); $ossClient->setTimeout(10); $ossClient->setConnectTimeout(20); } catch (OssException $e) { assertFalse(true); } }
<?php header("Content-type: text/html; charset=utf-8"); header('Access-Control-Allow-Origin:*'); require_once '/aliyun-oss-php-sdk-master/autoload.php'; use OSS\OssClient; use OSS\Core\OssException; $accessKeyId = "7LdVru52GybHb1DB"; $accessKeySecret = "zWrGBwmcgWxgBtdrGlPpEfyacRmuAd"; $endpoint = "oss-cn-shanghai.aliyuncs.com"; $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint); $ossClient->setTimeout(3600); $ossClient->setConnectTimeout(5); function create_guid() { $charid = strtoupper(md5(uniqid(mt_rand(), true))); $uuid = substr($charid, 0, 8) . substr($charid, 8, 4) . substr($charid, 12, 4) . substr($charid, 16, 4); return $uuid; } function WriteLog($info) { $file = 'F:\\wampserver\\setup\\wamp\\www\\uploadmedid.txt'; file_put_contents($file, $info, FILE_APPEND); } function test_input($data) { $data = trim($data); $data = stripslashes($data); $data = htmlspecialchars($data); return $data; }