


Thinkphp5 connects to Baidu Cloud Object Storage BOS (code example)
The content of this article is about Thinkphp5 docking with Baidu Cloud Object Storage BOS (code example). It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.
First download the SDK package, you can download it from the official website, or use composer in the project root directory.
composer require baidubce/bce-sdk-php
There are five files in the compressed package, but only two are actually used, and then placed in the extend file directory
Introduce it into the controller you need to use
//如果是用composer 引入的直接use就可以了 : use BaiduBce\Services\Bos\BosClient; //如果使用的压缩包: include_once './extend/BaiduBce.phar';//下面是文件结构 require './extend/SampleConf.php';// 配置信息 BaiduBce.phar ├──src │ └── BaiduBce //composer直接下载的就是这个文件 │ ├── Auth //BCE签名相关 │ ├── Exception //BCE客户端的异常 │ ├── Http //BCE的Http通信相关 │ ├── Log //BCE日志 │ ├── Services │ │ └── Bos //BOS主目录,此目录必须保留 │ │ ├── BosClient.php //BOS操作类,所有操作可以通过BosClient类可以完成 │ │ ├── BosOptions.php //BOS自定义配置 │ │ └── CannedAcl.php //CannedAcl模块 │ └── Util //BCE公用工具 └──vendor //第三方库
Upload method:
public function test_upload() { error_reporting(-1); $file = request()->file('file'); if ($file) { $info = $file->move(ROOT_PATH . 'uploads'); if ($info) { $BOS_TEST_CONFIG = array( 'credentials' => array( 'accessKeyId' => 'your accessKeyId', 'secretAccessKey' => 'your aecretAccessKey', ), 'endpoint' => 'bucket域名', ); $client = new BosClient($BOS_TEST_CONFIG);//如果是有配置文件直接在配置文件里面配置信息就可以了不需要写上面的数组。 $bucketName = 'rests';//bucket名字,相当于你在bucket创建好的那个文件夹,如果没有会自动创建。 $client->putObjectFromFile($bucketName, $info->getSaveName(), 'uploads' . DS . $info->getSaveName()); //第一个参数:bucket名字、第二个参数:文件名字、第三个参数:文件路径。 // 成功上传后 获取上传信息 $data['code'] = 0; $data['msg'] = ''; $data['list'] = [ 'src' => 'cartoon/' . $info->getSaveName(), 'name' => $info->getFilename(), 'preview' => 'uploads' . DS . $info->getSaveName(), ]; //上面的返回数据是看个人项目需要的数据 $url = ROOT_PATH . 'uploads' . DS . $info->getSaveName(); unset($info);//如果不释放这个变量下面的unlink()函数会报错。 if (file_exists($url)) { unlink($url);//删除本地文件 } echo json_encode($data); } else { // 上传失败获取错误信息 echo $file->getError(); } } }
Delete method :
/** * 删除百度云存储文件 * @access public * @param string $object 文件名字 * @param string $bucket BucketName * @return false|File */ public function file_del($object, $bucket = 'test') { $BOS_TEST_CONFIG = array( 'credentials' => array( 'accessKeyId' => 'your accessKeyId', 'secretAccessKey' => 'your secretAccessKey', ), 'endpoint' => 'bucket域名', ); $client = new BosClient($BOS_TEST_CONFIG); $client->deleteObject($bucket, $object); }
The above is the detailed content of Thinkphp5 connects to Baidu Cloud Object Storage BOS (code example). For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Solution to the error reported when deploying thinkphp5 in Pagoda: 1. Open the Pagoda server, install the php pathinfo extension and enable it; 2. Configure the ".access" file with the content "RewriteRule ^(.*)$ index.php?s=/$1 [QSA ,PT,L]”; 3. In website management, just enable thinkphp’s pseudo-static.

Solution to thinkphp5 url rewriting not working: 1. Check whether the mod_rewrite.so module is loaded in the httpd.conf configuration file; 2. Change None in AllowOverride None to All; 3. Modify the Apache configuration file .htaccess to "RewriteRule ^ (.*)$ index.php [L,E=PATH_INFO:$1]" and save it.

How to remove the thinkphp5 title bar icon: 1. Find the favicon.ico file under the thinkphp5 framework public; 2. Delete the file or choose another picture to rename it to favicon.ico and replace the original favicon.ico file.

Methods for thinkphp5 to obtain the requested URL: 1. Use the "$request = Request::instance();" method of the "\think\Request" class to obtain the current URL information; 2. Use the built-in helper function "$request-> url()" to obtain the complete URL address including the domain name.

thinkphp5 post cannot get a value because TP5 uses the strpos function to find the app/json string in the content-type value of the Header. The solution is to set the content-type value of the Header to app/json.

Solution to thinkphp5 prompting that the controller does not exist: 1. Check whether the namespace in the corresponding controller is written correctly and change it to the correct namespace; 2. Open the corresponding tp file and modify the class name.

How to query yesterday's data in ThinkPHP5: 1. Open ThinkPHP5 related files; 2. Query yesterday's data through the expression "db('table')->whereTime('c_time', 'yesterday')->select();" Can.

How to set error prompts in thinkphp5: 1. Enter the public folder in the project root directory and open the index.php entry file; 2. View the comments on the debug mode switch; 3. Adjust the value of the "APP_DEBUG" constant to true to display Error message prompt.
