Thinkphp5.1同步文件到阿里云OSS对象存储

1. 使用composer安装阿里云的组件

composer require aliyuncs/oss-sdk-php

安装过后会在根目录/vendor目录下有aliyuncs文件夹

2. 写自己的上传文件类

<?php
// +----------------------------------------------------------------------
// | HNZHISHENG.NET [ Beautiful and practical ]
// +----------------------------------------------------------------------
// | Copyright (c) 2020 http://www.hnzhisheng.net All rights reserved.
// | DateTime: 2020/11/7 17:32
// +----------------------------------------------------------------------
// | Author: hotlinhao <duyuebing@126.com>
// | Created by PhpStorm. Dyb
// | description:
// +----------------------------------------------------------------------
namespace myTools;

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

class AliOss extends MyBase{
    private $key;
    private $secret;
    private $bucket;
    private $endpoint = "http://oss-cn-hangzhou.aliyuncs.com";
    private $client;
    private $tmpCount = 0;

    public function __construct()
    {
        //$this->key = $access_key;
        //$this->secret = $access_secret;
        //$this->bucket = $bucket;
        $this->key = config('upload.oss_key');
        $this->secret = config('upload.oss_secret');
        $this->bucket = config('upload.oss_bucket');

        try{
            $this->client = new OssClient($this->key,$this->secret,$this->endpoint);
        }catch (OssException $e){
            addExceptionLog('连接OSS',$e->getMessage());
        }
    }

    /**
     * 检查Bucket是否存在,不存在则创建
     * @auther hotlinhao
     */
    private function checkBucketExists(){

    }

    /**
     * 上传文件
     * @auther hotlinhao
     * @param $path 去掉uploads的文件路径
     */
    public function upload($path){
        $uploadPath = config('upload.path'); //一般为 ./uploads/
        $content = file_get_contents($uploadPath . $path);
        //同步的OSS的文件名,相对于bucket
        $object = str_replace('./','',$uploadPath).$path;
        try{
            $this->client->putObject($this->bucket,$object,$content);
        }catch (OssException $e){
            addExceptionLog('OSS上传出错',$e->getMessage());
        }
    }

    /**
     * 同步上传目录的文件
     * @auther hotlinhao
     * @param string $path 路径,默认为上传的路径
     */
    public function sync($path = ''){
        if(empty($path)){
            $path = config('upload.path');
        }
        $path = $this->dirPath($path);
        $files = glob($path .'*');
        foreach ($files as $v){
            if(is_dir($v)){
                $this->sync($v);
            }else{
                $this->syncUpload($v);
            }
        }
    }

    private function dirPath($path){
        $path = str_replace('\\','/',$path);
        if(substr($path,-1) != '/') $path = $path . '/';
        return $path;
    }

    /**
     * 同步时的上传,这里的上传是./uploads这样的相对路径
     * @auther hotlinhao
     * @param $path
     */
    private function syncUpload($path){
        $content = file_get_contents( $path);
        //同步的OSS的文件名,相对于bucket
        $object = str_replace('./','',$path);
        try{
            $this->client->putObject($this->bucket,$object,$content);
        }catch (OssException $e){
            addExceptionLog('OSS上传出错',$e->getMessage());
        }
    }
}

我们自己项目中的文件,不要全部照抄有些内容是你用不到的。

核心:

$this->client = new OssClient($this->key,$this->secret,$this->endpoint);
$content = file_get_contents($uploadPath . $path);
//同步的OSS的文件名,相对于bucket
$object = str_replace('./','',$uploadPath).$path;

  $this->client->putObject($this->bucket,$object,$content);

核心就是上面的四句,上传文件至OSS

上一篇:重置docker中gitlab中root账号的密码


下一篇:15丨深入解析Pod对象(二):使用进阶