学习使用php判断阿里云oss图片单图或批量上传、查询图片文件是否存在

22 篇文章 1 订阅

学习使用php判断阿里云oss图片单图或批量上传、查询图片文件是否存在

doesObjectExist

主要函数doesObjectExist

/**
 * Base64上传文件
 * @param string|array $images
 * @param string $model_path
 * @param string $model_type
 * @param string $upload_path
 * @param bool $autoName
 * @return array
 */
protected function oss_base64_upload($images, $model_path = '', $model_type = 'images', $upload_path = '', $autoName = true)
{
    $accessKeyId = $this->oss_config['accessKeyId'];
    $accessKeySecret = $this->oss_config['accessKeySecret'];
    $endpoint = $this->oss_config['endpoint'];
    $bucket = $this->oss_config['bucket'];
    $isCName = true;
 
    if (empty($images)) {
        return ['Code' => 0, 'Msg' => '文件列表不能为空'];
    }
 
    $file_raw = [];
    $file_type = ['pjpeg', 'jpeg', 'jpg', 'gif', 'bmp', 'png', 'mov', '3gp', 'mp4', 'avi'];
    $preg_type = "image";
    $model_type = strtolower($model_type);
    if ($model_type == 'video') {
        $preg_type = $model_type;
    }
 
    //数组批量上传
    if (is_array($images) && count($images) > 0) {
        /*
         * $images 批量上传示例(值为一维单列或多列数组)
         * $images = [
         *      "base64/image1..........."
         *      "base64/image2..........."
         * ]
         */
 
        foreach ($images as $key => $value) {
            $value = trim($value);
            if (preg_match("/^(data:\s*$preg_type\/(\w+);base64,)/", $value, $result)) {
                $type = strtolower($result[2]);
                if (in_array($type, $file_type)) {
                    $file_raw[] = [
                        'raw' => base64_decode(str_replace($result[1], '', $value)), //文件流
                        'extension' => $type, //文件后缀
                        'index' => $key,
                    ];
                } else {
                    return ['Code' => 0, 'Msg' => '文件类型错误'];
                }
            } else {
                return ['Code' => 0, 'Msg' => '文件base64格式不合法'];
            }
        }
    }
 
    //字符串单图上传
    if (is_string($images)) {
        /*
         * $images 上传单个示例,字符串
         * $images = "base64/image..........."
         */
 
        $images = trim($images);
        if (preg_match("/^(data:\s*$preg_type\/(\w+);base64,)/", $images, $result)) {
            $type = strtolower($result[2]);
            if (in_array($type, $file_type)) {
                $file_raw[] = [
                    'raw' => base64_decode(str_replace($result[1], '', $images)), //文件流
                    'extension' => $type, //文件后缀
                    'index' => 0,
                ];
            } else {
                return ['Code' => 0, 'Msg' => '文件类型错误'];
            }
        } else {
            return ['Code' => 0, 'Msg' => '文件base64格式不合法'];
        }
    }
 
    if (empty($upload_path)) {
        $model_path = strstr('/', $model_path) ? $model_path : $model_path . '/';
        $upload_path = "{$model_type}/{$model_path}" . date('Y-m-d') . '/';
    }
 
    require_once(THINK_PATH . 'Extend/Vendor/aliyun-oss/autoload.php');
    $ossClient = new \OSS\OssClient($accessKeyId, $accessKeySecret, $endpoint, $isCName);
 
    $photo_list = [];
    try {
        if (!empty($file_raw)) {
            foreach ($file_raw as $value) {
                $name = substr(md5(base64_encode($value['raw']) . base64_encode(time() . mt_rand(33, 126))), 8, 16);
                if ($autoName === true) {
                    $file_name = $upload_path . $name . "." . strtolower($value['extension']);
                } else {
                    $file_name = $upload_path;
                }
                $getOssInfo = $ossClient->putObject($bucket, $file_name, $value['raw']);
                $getOssPdfUrl = $getOssInfo['info']['url'];
                if ($getOssPdfUrl) {
                    $photo_list[$value['index']] = self::http_to_https($getOssPdfUrl);
                }
            }
        }
    } catch (OssException $e) {
        return ['Code' => 0, 'Msg' => $e->getMessage()];
    }
 
    return ['Code' => 1, 'Msg' => $photo_list];
}
 
/**
 * OSS内文件是否存在
 * @param string $object @文件路径
 * @return bool
 */
protected function doesObjectExist($object = '')
{
    $exist = false;
    if (empty($object)) {
        return $exist;
    }
 
    $accessKeyId = $this->oss_config['accessKeyId'];
    $accessKeySecret = $this->oss_config['accessKeySecret'];
    $endpoint = $this->oss_config['endpoint'];
    $bucket = $this->oss_config['bucket'];
    $isCName = true;
 
    require_once(THINK_PATH . 'Extend/Vendor/aliyun-oss/autoload.php');
    $ossClient = new \OSS\OssClient($accessKeyId, $accessKeySecret, $endpoint, $isCName);
 
    try {
        $exist = $ossClient->doesObjectExist($bucket, $object);
    } catch (OssException $e) {
        $exist = false;
        //printf($e->getMessage() . "\n");
    }
 
    return $exist;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值