Thinkp5 docking with Baidu cloud object storage BOS (upload, delete)

Original address: https://www.cnblogs.com/best-always/p/10273320.html

First, download the SDK package at Download on official website , or use composer at the root of the project.

composer require baidubce/bce-sdk-php

There are five files in the compressed package, and only two files are actually used, and then they are placed in the extend file directory

 

Introduce in the controller to be used


//If it is a direct use introduced by composer, you can:
use BaiduBce\Services\Bos\BosClient;
//If you are using a compressed package:
include_once './extend/BaiduBce.phar';//Here is the file structure
require './extend/SampleConf.php';// configuration information
BaiduBce.phar
├──src
│   └── BaiduBce                //This is the file directly downloaded by composer
│       ├── Auth                //BCE Signature correlation
│       ├── Exception           //BCE Client exceptions
│       ├── Http                //BCE Of Http Communication related
│       ├── Log                 //BCE Journal
│       ├── Services
│       │   └── Bos                   //BOS Home directory, which must be kept
│       │       ├── BosClient.php     //BOS Operation class, all operations can be BosClient Class can complete
│       │       ├── BosOptions.php    //BOS Custom configuration
│       │       └── CannedAcl.php     //CannedAcl Modular
│       └── Util                //BCE Utility tools
└──vendor                       //Third party Library

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 domain name',
                    );
                $client = new BosClient($BOS_TEST_CONFIG);//If there is a configuration file that directly configures the information in the configuration file, you do not need to write the above array.
                $bucketName = 'rests';//The bucket name is the same as the folder you created in the bucket. It will be created automatically if not.
                $client->putObjectFromFile($bucketName, $info->getSaveName(), 'uploads' . DS . $info->getSaveName());
          //The first parameter is bucket name, the second parameter is file name, and the third parameter is file path.
// Get upload information after successful upload $data['code'] = 0; $data['msg'] = ''; $data['list'] = [ 'src' => 'cartoon/' . $info->getSaveName(), 'name' => $info->getFilename(), 'preview' => 'uploads' . DS . $info->getSaveName(), ];
          //The above return data is the data needed for personal projects
$url = ROOT_PATH . 'uploads' . DS . $info->getSaveName(); unset($info);//If you do not release theunlink()The function will report an error. if (file_exists($url)) { unlink($url);//Delete local file } echo json_encode($data); } else { // Upload failed to get error information echo $file->getError(); } } }

Delete method:

    /**
     * Delete baidu cloud storage file
     * @access public
     * @param  string $object File name
     * @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 domain name',
            );
        $client = new BosClient($BOS_TEST_CONFIG);
        $client->deleteObject($bucket, $object);
    } 

The copyright of this article belongs to the author and the blog park. It supports the original creation and is welcome to reprint. However, without the consent of the author, this statement must be retained and the original link should be provided in the obvious position of the article page. Otherwise, the right to pursue legal responsibility is reserved.

Keywords: PHP SDK

Added by djwiltsh on Sat, 07 Dec 2019 11:23:15 +0200