当前位置: 首页 > news >正文

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

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

    • doesObjectExist

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;
}
http://www.lryc.cn/news/156566.html

相关文章:

  • 重磅| Falcon 180B 正式在 Hugging Face Hub 上发布!
  • Linux命令行
  • [持续更新]计算机经典面试题基础篇Day1
  • ProcessWindowFunction 结合自定义触发器的陷阱
  • 什么是jvm
  • kettle通过java步骤获取汉字首拼
  • Conformer: Local Features Coupling Global Representationsfor Visual Recognition
  • java8-Stream流常用API
  • React 任务调度
  • 小白开始学习C++
  • SpringMVC入门的注解、参数传递、返回值和页面跳转---超详细教学
  • 【复习socket】每天40min,我们一起用70天稳扎稳打学完《JavaEE初阶》——28/70 第二十八天
  • vue2踩坑之项目:生成二维码使用vue-print-nb打印二维码
  • 【iVX】十五分钟制作一款小游戏,iVX真有怎么神?
  • SpringMVC常用注解、参数传递、返回值
  • 新公司第一次上架新APP需要提前准备哪些材料?
  • 『C语言进阶』指针进阶(一)
  • 2605. 从两个数字数组里生成最小数字(Java)
  • 深度解析 PostgreSQL Protocol v3.0(一)
  • Mysql中having语句与where语句的用法与区别
  • 基于qt软件的网上聊天室软件
  • 本是同根生-双数据库集群keepalived virtual_route_id冲突导致连接故障
  • 『力扣每日一题06』字符串中的第一个唯一字符
  • selenium鼠标操作方法
  • 医者无疆 | AI赋能大医精诚,医疗制药的进阶与突破
  • 使用 Hue 玩转 Amazon EMR(SparkSQL, Phoenix) 和 Amazon Redshift
  • Unity中神秘的Transform和transform(小写)的关系
  • 【LeetCode-中等题】78. 子集
  • 学习设计模式之代理模式,但是宝可梦
  • 自学Python01-创建文件写入内容