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, the URL is https://help.aliyun.com/document_detail/32099.html?spm=5176.87240. 400427.40.s8EbEH
2 Use composer to install, enter in the project directory (basic/):
composer require aliyuncs/oss-sdk-php
After the installation is completed, as shown below
3 Custom components:
3.1 Create the components folder in the basic directory,
3.2 Create Aliyunoss in the components directory. php
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['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"; } } ?>
3.3 Add an array in basic/config/params.php to place Alibaba Cloud oss The configuration information
The code reference is as follows:
'oss' =>[ 'accessKeyId'=>'您的accessKeyId', 'accessKeySecret'=>'您的accessKeySecret', 'bucket' => '您的bucket', 'endPoint' => '您的endPoint', ]
3.4 Add a component configuration in basic/config/main.php
'Aliyunoss' => [ 'class' => 'app\components\Aliyunoss', ],
3.5 Try to call
Yii::$app->Aliyunoss->test();
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 Sample code of yii2.0 oss. For more information, please follow other related articles on the PHP Chinese website!