Integrate the PHP-SDK2.0.4 of Alibaba Cloud OSS in OneThink (ThinkPHP3.2.3) to realize the local file upload process

编辑上传文件方法upload

1. SDK installation

github address: https://github.com/aliyun/aliyun-oss-php-sdk

2. Copy aliyun-oss-php-sdk-master\src\oss to passport.hmwis.com\thinkphp\library\vendor\oss, as shown in Figure 1 and 2

复制aliyun-oss-php-sdk-master\src\OSS至passport.hmwis.com\ThinkPHP\Library\Vendor\OSS
Copy aliyun-oss-php-sdk-master\src\oss to passport.hmwis.com\Thinkphp\Library\vendor\oss
复制aliyun-oss-php-sdk-master\src\OSS至passport.hmwis.com\ThinkPHP\Library\Vendor\OSS
Copy aliyun-oss-php-sdk-master\src\oss to passport.hmwis.com\Thinkphp\Library\vendor\oss

3. Register a new namespace OSS and edit the file passport.hmwis.com\application\common\conf\config.php, as shown in Figure 3

autoload_namespace=> array(additions=>OneThink_Addon_Path,oss=> Vendor_Path.oss), // Extension Module List

注册新的命名空间OSS
Sign up for a new namespace OSS

4. When instantiating the class library below the OSS, such as

new oss\ossclient();

The corresponding class library file OSSCLIENT.class.php is automatically loaded, so the file name in the oss directory needs to be renamed, and the oss client.php is renamed to ossclient.class.php, all files in the oss directory are processed like this, as shown in Figure 4

重命名OSS目录下的文件名,将OssClient.php重命名为OssClient.class.php,OSS目录下所有文件皆如是处理
Rename the file name in the oss directory, rename ossclient.php to ossclient.class.php, all files in the oss directory are processed as follows

5. Edit passport.hmwis.com\application\common\conf\config.php, and set the access domain name of the OSS data center in the production environment to the intranet, as shown in Figure 5

/* OSS initialization settings */
oss=> array(
access_key_id=>, // AccessKeyId from OSS
access_key_secret=>, // AccessKeySecret obtained from OSS
Endpoint=>oss-cn-hangzhou-internal.aliyuncs.com, // The OSS data center you selected access the domain name, such as oss-cn-hangzhou.aliyuncs.com
,

生产环境下的OSS数据中心访问域名设置为内网
The OSS data center access domain name in the production environment is set to the intranet

6. Edit passport.hmwis.com\application\common\conf\debug.php, and set the access domain name of the OSS data center in the development environment to the external network, as shown in Figure 6

/* OSS initialization settings */
oss=> array(
access_key_id=>, // AccessKeyId from OSS
access_key_secret=>, // AccessKeySecret obtained from OSS
Endpoint=>oss-cn-hangzhou.aliyuncs.com, // The OSS data center you selected access the domain name, such as oss-cn-hangzhou.aliyuncs.com
,

开发环境下的OSS数据中心访问域名设置为外网
The OSS data center access domain name in the development environment is set to the external network

7. Realize the function of uploading local files to OSS, such as modifying the avatar, as shown in Figure 7:

实现上传本地文件至OSS的功能,如修改头像
Realize the function of uploading local files to OSS, such as modifying the avatar

8. Edit passport.hmwis.com\Application\Home\Controller\ProfileController.class.php, import the client class and exception class of OSS, as shown in Figure 8
use oss\ossclient;
use oss\core\OSSException;

导入OSS的客户端类、异常类
Import OSS client class, exception class

9. Instantiate the OssClient class and call the upload local file method, as shown in Figure 9

/**
* According to the config configuration, get an OssClient instance
*/
try {
$ossclient = new ossclient(c(oss.access_key_id), C(oss.access_key_secret), C(oss.endpoint), false);
} catch (OSSException $E) {
$return[‘status’]= 0;
$return[‘info’]= $e->getMessage();
/* return json data */
$this->ajaxreturn($return);
}
$bucket =avatar-ygt-cm;
try {
$ossclient->uploadfile($bucket, $oss_file_path, $avatar);
} catch (OSSException $E) {
$return[‘status’]= 0;
$return[‘info’]= $e->getMessage();
/* return json data */
$this->ajaxreturn($return);
}

实例化OssClient类,调用上传本地文件方法
Instantiate the OssClient class and call the upload local file method

10. Based on the ThinkPHP file upload operation, use the Think\Upload class customization to upload the file to the OSS through the form, and copy the Passport. hmwis.com\thinkphp\library\think\upload.class.php to passport.hmwi S.com\Thinkphp\Library\Vendor\OSS\OSSupload.class.php, edit PassPor t.hmwis.com\thinkphp\library\vendor\oss\ossupload.class.php, as shown in Figure 10

namespace oss;
use oss\ossclient;
use oss\core\OSSException;

基于ThinkPHP文件上传操作使用Think\Upload类定制,实现通过表单上传文件至OSS
Use the Think\Upload class to customize based on the ThinkPHP file upload operation, and upload files to OSS through the form

11. Edit the default upload configuration, as shown in Figure 11

/**
* Default upload configuration
* @var array
*/
private $config = array(
accessKeyId=>L0K4ZMXS44FAJAWH, //AccessKeyId
AccessKeySecret=>hju4dphzkl6pwt6k0hltkvmnae9rzo, //AccessKeySecret
Endpoint=>oss-cn-hangzhou.aliyuncs.com, //area address
Mimes=> array(), //Allow the uploaded file MIME type
maxsize=> 0, //The uploaded file size limit (0-no limit)
exts=> array(), //Allow the uploaded file suffix
autosub=> true, //auto subdirectory saves the file
Subname=> array(Date,Y-M-D), // subdirectory creation method,[0]- function name,[1]- Parameters, multiple parameters use arrays
rootpath=>./uploads/, // save the root path
savepath=>, // save the path
savename=> array(unqid,), // upload file naming rules,[0]- function name,[1]- Parameters, multiple parameters use arrays
saveext=>, // save the suffix for the file, and use the original suffix for empty
replace=> false, //Whether the same name is overwritten
hassh=> true, //whether to generate hash encoding
callback=> false, //Detect if there is a callback in the file, if there is a file information array
);

Note: It is mainly to cancel the operation function related to the file upload driver and file directory. Due to too many changes, it is recommended to check the svn log, or directly copy ossupload.class.php;

主要为取消文件上传驱动及文件目录相关的操作功能实现,由于改动过多,建议查看SVN日志,或者直接复制OssUpload.class.php
It is mainly to cancel the operation function related to the cancellation of the file upload driver and file directory. Due to too many changes, it is recommended to check the svn log, or directly copy ossupload.class.php

12. Edit the upload file method upload, as shown in Figure 12

/* Save the file and record the file saved successfully */
tiveting
if ($this->uploader->save($file,$this->replace)) {
unset($file[‘error’], $file[‘tmp_name’]);
$info[$key]= $file;
} else {
$this->error = $this->uploader->getError();
}*/
/**
* According to the config configuration, get an OssClient instance
*/
try {
$ossclient = new ossclient($this->accessKeyId, $this->AccessKeySecret, $this->endpoint, false);
} catch (OSSException $E) {
$this->error = $e->getMessage();
}

/**
* Upload local files
*/
$bucket =avatar-ygt-cm;
$object = $this->rootpath . $file[‘savepath’]. $file[‘savename’];
try {
$ossclient->uploadfile($bucket, $object, $file[‘tmp_name’]);
unset($file[‘error’], $file[‘tmp_name’]);
$file[‘oss’]= $bucket ./ / *. $object;
$info[$key]= $file;
} catch (OSSException $E) {
$this->error = $e->getMessage();
}

编辑上传文件方法upload
Edit upload file method upload

13. Realize upload files to OSS through form, edit passport.hmwis.com\application\home\controller\profileController.class.php, import the file upload class of oss
use oss\ossupload;
For all upload implementations, it is recommended to refer to the document of the Think\Upload class, which is basically the same

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.