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

黄舟
Release: 2023-03-16 09:40:01
Original
2604 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:


<?php
namespace app\components;

use Yii;
use yii\base\Component;
use OSS\OssClient;

class Aliyunoss extends Component
{
  public static $oss;

  public function __construct()
  {
    parent::__construct();
    $accessKeyId = Yii::$app->params[&#39;oss&#39;][&#39;accessKeyId&#39;];         //获取阿里云oss的accessKeyId
    $accessKeySecret = Yii::$app->params[&#39;oss&#39;][&#39;accessKeySecret&#39;];     //获取阿里云oss的accessKeySecret
    $endpoint = Yii::$app->params[&#39;oss&#39;][&#39;endPoint&#39;];            //获取阿里云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[&#39;oss&#39;][&#39;bucket&#39;];        //获取阿里云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[&#39;oss&#39;][&#39;bucket&#39;];  //获取阿里云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:


&#39;oss&#39; =>[
    &#39;accessKeyId&#39;=>&#39;您的accessKeyId&#39;,
    &#39;accessKeySecret&#39;=>&#39;您的accessKeySecret&#39;,
    &#39;bucket&#39; => &#39;您的bucket&#39;,
    &#39;endPoint&#39; => &#39;您的endPoint&#39;,
  ]
Copy after login

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


&#39;Aliyunoss&#39; => [
  &#39;class&#39; => &#39;app\components\Aliyunoss&#39;,
],
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