<?php
/**
 * Created by PhpStorm.
 * User: tandunzhao
 * Date: 2018/1/19
 * Time: 下午12:58
 */

namespace App\Libs;

use Illuminate\Support\Facades\Log;
use OSS\Core\OssException;
use OSS\OssClient;

class AliOSS
{

    public static function uploadImg($ossPath, $filePath)
    {
        $ossImgBackData = self::ossObject()->uploadFile(env('OSS_BUCKET', 'zhuishuyun'), $ossPath, $filePath);
        return $ossImgBackData['oss-request-url'];
    }


    /**
     * OSS文件操作
     */
    public static function ossObject()
    {
        $accessKeyId     = env('OSS_ACCESS_ID');
        $accessKeySecret = env('OSS_ACCESS_KEY');
        $endpoint        = env('OSS_END_POINT');
        try {
            $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint);
        } catch (OssException $e) {
            Log::error($e->getMessage() . "      " . date("y-m-d H:i:s" . "\n"));
            return null;
        }
        return $ossClient;
    }
}