没有不值得去解决的问题,也没有不值得去学习的技术!

Integrating Alibaba Cloud OSS PHP-SDK 2.0.4 With OneThink (ThinkPHP3.2.3): Web Direct Upload, Server-Signed Upload, and Upload Callback

Web direct upload test successful

1. To integrate Alibaba Cloud OSS PHP-SDK 2.0.4 into OneThink (ThinkPHP3.2.3) to implement web direct upload, server-side signed direct upload, and upload callback, the prerequisite is to first follow the implementation at: https://www.shuijingwanwq.com/2016/01/13/909/ to configure the CORS for the Bucekt (storage4-hmwis-com), as shown in Figure 0;

Configure CORS for the Bucekt (storage4-hmwis-com)
Configure CORS for the Bucekt (storage4-hmwis-com)

2. Review the help documentation on Web Direct Upload Practices — Server-Signed Direct Upload and Upload Callback. Download the code examples: application server signature return code: oss-h5-upload-js-php-callback.tar.gz, and callback application server version: callback-php-demo.zip, as shown in Figures 1 and 2.

Application server signature return code: oss-h5-upload-js-php-callback.tar.gz
Application server signature return code: oss-h5-upload-js-php-callback.tar.gz
Callback application server version: callback-php-demo.zip
Callback application server version: callback-php-demo.zip

3. Copy the function gmt_iso8601 from E:\wwwroot\oss-h5-upload-js-php-callback\php\get.php to E:\wwwroot\ke.hmwis.com\Application\Common\Common\function.php, as shown in Figure 3.

Copy function gmt_iso8601
Copy function gmt_iso8601

4. Open E:\wwwroot\ke.hmwis.com\Application\Home\Controller\FileController.class.php and create a new upload form method, as shown in Figure 4.

/* File upload form, based on OSS, web direct upload */
public function ossUpload(){
$this->display();
}

Create a new upload form method
Create a new upload form method

5. Open E:\wwwroot\oss-h5-upload-js-php\index.html, copy all content to E:\wwwroot\ke.hmwis.com\Application\Home\View\default\File\ossUpload.html, and adjust the corresponding file paths within it, as shown in Figure 5.

Edit the upload form template
Edit the upload form template

6. Open http://ke.ygt.cm/File/ossUpload.html and view the web page source code, as shown in Figure 6.

View the upload form web page source code
View the upload form web page source code

7. Copy the corresponding files from E:\wwwroot\oss-h5-upload-js-php-callback to the E:\wwwroot\static.hmwis.com\ke\Public\static\oss directory, as shown in Figure 7.

Copy the corresponding files to the appropriate resource directory
Copy the corresponding files to the appropriate resource directory

8. Open E:\wwwroot\oss-h5-upload-js-php-callback\php\get.php and import the OSS client class and exception class, as shown in Figure 8.

use OSS\OssClient;
use OSS\Core\OssException;

Import the OSS client class and exception class
Import the OSS client class and exception class

9. Open E:\wwwroot\oss-h5-upload-js-php-callback\php\get.php, copy all code except function gmt_iso8601 into the public function ossGet() in E:\wwwroot\ke.hmwis.com\Application\Home\Controller\FileController.class.php, and instantiate the OSS 2.0.4 client class, as shown in Figure 9.

Create public function ossGet() and instantiate the OSS 2.0.4 client class
Create public function ossGet() and instantiate the OSS 2.0.4 client class

10. Configure the virtual host for zhidao.ygt.cm:

* Note: If you want to use the HTTP_AUTHORIZATION header, you must first set up a rewrite in Apache or Nginx. Taking Apache as an example, modify
* the configuration file /etc/httpd/conf/httpd.conf (based on your Apache installation path), as shown in Figure 10.

Add a line:

RewriteRule .* – [env=HTTP_AUTHORIZATION:%{HTTP:Authorization},last]

Configure the virtual host for zhidao.ygt.cm to use the HTTP_AUTHORIZATION header
Configure the virtual host for zhidao.ygt.cm to use the HTTP_AUTHORIZATION header

11. Edit E:\wwwroot\static.hmwis.com\ke\Public\static\oss\upload.js, as shown in Figure 11.

phpUrl = ‘http://ke.ygt.cm/File/ossGet.html’

Set phpUrl
Set phpUrl

12. Edit E:\wwwroot\oss-h5-upload-js-php-callback\callback.php, as shown in Figure 12.

file_put_contents(‘./data/1.txt’, json_encode($body));

Edit callback.php to test callback data
Edit callback.php to test callback data

13. The upload test is successful, as shown in Figures 13 and 14.

Web direct upload test successful
Web direct upload test successful
Verified on OSS that the file has been uploaded successfully
Verified on OSS that the file has been uploaded successfully

14. The callback test is successful. View zhidao.ygt.cm/data/1.txt, as shown in Figure 15.

“filename=user-dir%2F10.png&size=218185&mimeType=image%2Fpng&height=453&width=674”

Callback test successful
Callback test successful

关于作者 · 持续学习与技术实践

我是一名拥有 15 年以上开发经验的技术从业者,自 2013 年起持续运营个人技术博客,记录工作与个人项目中遇到的真实问题,以及解决问题过程中的探索、实践和思考。

除了技术写作,我也在持续开发和维护自己的独立项目,探索新技术和新工具在实际场景中的应用。我始终相信,没有不值得去解决的问题,也没有不值得去学习的技术。

如果这篇文章对你有所帮助,欢迎浏览博客中的其他文章,也欢迎交流技术经验与想法。

关于我  ·  GitHub  ·  邮件联系