YII2.0 access to Alibaba cloud OSS object storage

Keywords: PHP SDK

After searching for half a day's data, we finally managed to connect YII2.0 to OSS object storage. It is assumed that you have installed YII2.0, and you have installed compose.
Step 1: install the OSS object component (i.e. the PHP API of OSS) console, enter the root directory of YII2.0, input: composer require aliyuncs / OSS SDK PHP, and then install it. After installation, aliyuncs folder is available in the root directory under verdor, as shown in the figure below:

Here, the OSS object storage API has been installed. Next, it is integrated with YII2.0.
Part 2: create two folders common/components / in the root directory, and then create a file Aliyunoss.php in the components folder, as shown in the figure

Next, we configure this component in the configuration file:

'Aliyunoss' => [
            'class' => 'app\common\components\Aliyunoss',
        ],

As shown in the figure:

Here, YII2.0 successfully defines a component, and then we write the OSS API logic code in this component. First, configure OSS, and then configure OSS in params file, as shown in the figure:


Four parameters can be found in oss background. Here YII2.0 and oss are integrated. The next step is to type the code
Part 3: write the following code in the common/components/Aliyunoss file:

<?php

namespace app\common\components;

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


class Aliyunoss extends Component
{
    public static $oss;

    public function __construct()
    {
        parent::__construct();
        $accessKeyId = Yii::$app->params['oss']['accessKeyId'];                 //Get the accessKeyId of alicloud oss
        $accessKeySecret = Yii::$app->params['oss']['accessKeySecret'];         //Access keysecret of alicloud oss
        $endpoint = Yii::$app->params['oss']['endPoint'];                       //Get the endPoint of alicloud oss
        self::$oss = new OssClient($accessKeyId, $accessKeySecret, $endpoint);  //Instantiate OssClient object
    }



   //Delete files / or folders
   public function del_file()
   {
        $res = false;
        $bucket = Yii::$app->params['oss']['bucket'];    //Get alicloud oss bucket
        if (self::$oss->deleteObject($bucket, "wocao/")){
        //Call the deleteObject method to upload the server file to alicloud oss
            $res = true;
        }
        return $res;
   }



   //Upload file
   public function upload_file()
   {
        try
        {
            $bucket = Yii::$app->params['oss']['bucket'];

            $a = self::$oss->uploadFile($bucket, time()."."."png", "kwk.png");
            print_r($a);
            echo "success";
        }
        catch(OssException $e)
        {
            printf(__FUNCTION__ . ": FAILED\n");
            printf($e->getMessage() . "\n");
            return;
        }
   }

   //Download File
   public function down_file()
   {
        $localfile ="yuzengyuan."."jpg";

        $options = array(
            OssClient::OSS_FILE_DOWNLOAD => $localfile,
        );
        try
        {
             $bucket = Yii::$app->params['oss']['bucket'];
             self::$oss->getObject($bucket, "1513585444.png", $options);
        } 
        catch( OssException $e) 
        {
            printf(__FUNCTION__ . ": FAILED\n");
            printf($e->getMessage() . "\n");

            return;
        }
        print(__FUNCTION__ . ": OK, " .$localfile. "\n");
   }

   //Determine whether there is a file
   function doesObjectExist()
    {

        try{
            $bucket = Yii::$app->params['oss']['bucket'];
            $exist = self::$oss->doesObjectExist($bucket, $object);
        } catch(OssException $e) {
            printf(__FUNCTION__ . ": FAILED\n");
            printf($e->getMessage() . "\n");
            return;
        }
        print(__FUNCTION__ . ": OK" . "\n");
        var_dump($exist);
    }



   //Create virtual directory
   public function create_dir()
   {
        try
        {
            self::$oss->createObjectDir("budfesdfd", "dirrrr");
        }
        catch(OssException $e) 
        {
            printf(__FUNCTION__ . ": FAILED\n");
            printf($e->getMessage() . "\n");
            return;
        }
        print(__FUNCTION__ . ": OK" . "\n");
   }

   //Delete virtual directory
   function createObjectDir() 
   {

        try{
            $bucket = Yii::$app->params['oss']['bucket'];
            self::$oss->createObjectDir($bucket, "phpyuzengyuan");
        } catch(OssException $e) {
            printf(__FUNCTION__ . ": FAILED\n");
            printf($e->getMessage() . "\n");
            return;
        }
        print(__FUNCTION__ . ": OK" . "\n");
    }



   //List all user storage
   public function listBuckets()
    {
        $bucketList = null;
        try{
            $bucketListInfo = self::$oss->listBuckets();
        } catch(OssException $e) {
            printf(__FUNCTION__ . ": FAILED\n");
            printf($e->getMessage() . "\n");
            return;
        }
        $bucketList = $bucketListInfo->getBucketList();
        foreach($bucketList as $bucket) {
          echo $bucket->getLocation() . "<br>" . $bucket->getName() . "<br>" . $bucket->getCreatedate() . "<br>";
          echo "<hr>";
        }
    }

     //Create storage space
   public function create_Bucket()
   {
         try
         {
            self::$oss->createBucket("budfesdfd");
         } 
         catch(OssException $e) 
         {
            printf(__FUNCTION__ . ": FAILED\n");
            printf($e->getMessage() . "\n");
            return;
         }
         echo "success";
   }

    //Delete storage space
    function deleteBucket()
    {
        try{
            self::$oss->deleteBucket("yuzengbdfds");
        } catch(OssException $e) {
            printf(__FUNCTION__ . ": FAILED\n");
            printf($e->getMessage() . "\n");
            return;
        }
        print(__FUNCTION__ . ": OK" . "\n");
    }

    //Determine whether the storage space exists
    function doesBucketExist()
    {
        try {
            $res = self::$oss->doesBucketExist("budfe1sdfd");
        } catch (OssException $e) {
            printf(__FUNCTION__ . ": FAILED\n");
            printf($e->getMessage() . "\n");
            return;
        }
        if ($res === true) {
            print(__FUNCTION__ . ": OK" . "\n");
        } else {
            print(__FUNCTION__ . ": FAILED" . "\n");
        }
    }
}

Then you can use it in the controller, you need to introduce the namespace of aliyunoss.php, and then you can call it.

Detailed API can be viewed https://help.aliyun.com/document_detail/32099.html?spm=5176.doc32102.6.758.J6e3WE Here

Posted by webosb on Thu, 07 May 2020 08:59:56 -0700