123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263 |
- <?php
- /**
- * Created by PhpStorm.
- * User: xiaofeng
- * Date: 2018/6/11
- * Time: 上午9:28
- */
- namespace common\api;
- use Yii;
- use yii\base\Component;
- use OSS\OssClient;
- class AliyunossServer extends Component{
- public static $oss;
- public function __construct()
- {
- parent::__construct();
- $accessKeyId = Yii::$app->params['aliyunoss']['accessKeyId']; //获取阿里云oss的accessKeyId
- $accessKeySecret = Yii::$app->params['aliyunoss']['accessKeySecret']; //获取阿里云oss的accessKeySecret
- $endpoint = Yii::$app->params['aliyunoss']['endPoint']; //获取阿里云oss的endPoint
- self::$oss = new OssClient($accessKeyId, $accessKeySecret, $endpoint); //实例化OssClient对象
- }
- /**
- * 使用阿里云oss上传文件
- * @param $object 保存到阿里云oss的文件名
- * @param $filepath 文件在本地的绝对路径
- * @return bool 上传是否成功
- */
- public function upload($object, $filepath)
- {
- $res = false;
- $bucket = Yii::$app->params['aliyunoss']['bucket']; //获取阿里云oss的bucket
- if (self::$oss->uploadFile($bucket, $object, $filepath)) { //调用uploadFile方法把服务器文件上传到阿里云oss
- $res = true;
- }
- return $res;
- }
- /**
- * 删除指定文件
- * @param $object 被删除的文件名
- * @return bool 删除是否成功
- */
- public function delete($object)
- {
- $res = false;
- $bucket = Yii::$app->params['oss']['bucket']; //获取阿里云oss的bucket
- if (self::$oss->deleteObject($bucket, $object)){ //调用deleteObject方法把服务器文件上传到阿里云oss
- $res = true;
- }
- return $res;
- }
- public function test(){
- echo 123;
- echo "success";
- }
- }
|