学习使用PHP判断阿里云oss图片单图或批量上传、查询图片文件是否存在 doesObjectExist doesObjectExist 主要函数doesObjectExist prot
主要函数doesObjectExist
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) { 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 = 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];} 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;}
来源地址:https://blog.csdn.net/guo_qiangqiang/article/details/132670481
--结束END--
本文标题: 学习使用php判断阿里云oss图片单图或批量上传、查询图片文件是否存在
本文链接: https://lsjlt.com/news/412312.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0