Case sharing of integrating Alibaba Cloud oss ​​with yii2.0 (picture)

黄舟
Release: 2023-03-16 09:40:01
Original
2453 people have browsed it

This article mainly introduces the sample code of Yii2.0 integrating Alibaba Cloud oss. The editor thinks it is quite good, so I will share it with you now and give it as a reference. Let’s follow the editor and take a look.

The main idea is to first use composer to download the PHP SDK of Alibaba Cloud OSS, and then customize a component to call Alibaba Cloud OSS globally.

The specific steps are as follows:

1 Go to Alibaba Cloud official website to find php sdk,

2 Use composer to install it, enter in the project directory (basic/):


composer require aliyuncs/oss-sdk-php
Copy after login

After the installation is completed, as shown below

3 Custom components:

3.1 In the basic directory, create the components folder,

3.2 Create Aliyunoss.php in the components directory

The code reference is as follows:


params['oss']['accessKeyId'];         //获取阿里云oss的accessKeyId
    $accessKeySecret = Yii::$app->params['oss']['accessKeySecret'];     //获取阿里云oss的accessKeySecret
    $endpoint = Yii::$app->params['oss']['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['oss']['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";
  }
}
?>
Copy after login

3.3 Add an array in basic/config/params.php to place the configuration information of Alibaba Cloud oss

The code reference is as follows:


'oss' =>[
    'accessKeyId'=>'您的accessKeyId',
    'accessKeySecret'=>'您的accessKeySecret',
    'bucket' => '您的bucket',
    'endPoint' => '您的endPoint',
  ]
Copy after login

3.4 Add a component configuration in basic/config/main.php


'Aliyunoss' => [
  'class' => 'app\components\Aliyunoss',
],
Copy after login

3.5 Try to call it in the controller


Yii::$app->Aliyunoss->test();
Copy after login

3.6 Screenshot of successful call:

At this point, Alibaba Cloud oss ​​has been successfully introduced into the yii2 framework. Calling the upload method and calling the delete method are the same as the example. You can also add new functions to the Aliyunoss component based on the methods in OssClient. Generally speaking, using the upload and delete functions can meet most needs.

The above is the detailed content of Case sharing of integrating Alibaba Cloud oss ​​with yii2.0 (picture). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!