本文小编为大家详细介绍“OSS云环境文件上传与签名的代码怎么写”,内容详细,步骤清晰,细节处理妥当,希望这篇“OSS云环境文件上传与签名的代码怎么写”文章能帮助大家解决疑惑,下面跟着小编的思路慢慢深入,一起来学习新知识吧。
OSS文件上传与签名
相关包安装命令:
// 阿里云osscomposer require aliyuncs/oss-sdk-php// 华为云obscomposer require obs/esdk-obs-php
需注意包版本,是否与 php 版本适用。
.env 配置项:
# OSS相关配置OSS_DRIVER=HW_OBS#华为OBSOSS_HW_ENDPOINT=https://obs.cn-east-3.myhuaweicloud.comOSS_HW_KEY=ME0AVBTNJTSJB2LH0EGIOSS_HW_SECRET=eCGffrwdx3Rt5QEmKbtEvruvGgg1mCUjMsnHfjWoOSS_HW_BUCKET=pub-obs-test-1#阿里云OSS_ENDPOINT=https://oss-cn-hangzhou.aliyuncs.comOSS_KEYID=LTAI4Ftno9DsfiVHADX73osaOSS_KEYSECRET=vo9KuqgaDN727eOOz1tDg77Egeg7wEOSS_BUCKET=xgimi-ipr
代码:
接口声明
<?phpnamespace App\Service\OSS;interface IOSS{ public function publicUpload($fullFileName, $filePath); public function getUrl($fullFileName, $expires); public function replaceUrl($url);}
阿里 OSS 实现
<?phpnamespace App\Service\OSS;use OSS\OssClient;class AliOSS implements IOSS{ private $endPoint; private $keyId; private $secret; private $bucket; private $ossClient; private $expires = 3 * 24 * 3600; private $aliHost = ''; private $myHost = ''; public function __construct() { $this->endPoint = env("OSS_ENDPOINT"); $this->keyId = env("OSS_KEYID"); $this->secret = env("OSS_KEYSECRET"); $this->bucket = env("OSS_BUCKET"); try { $this->ossClient = new OssClient($this->keyId, $this->secret, $this->endPoint); } catch (\Exception $e) { } } public function publicUpload($fullFileName, $filePath) { return $this->ossClient->uploadFile($this->bucket, $fullFileName, $filePath); } public function getUrl($fullFileName, $expires) { $expires = $expires ? $expires : $this->expires; $signUrl = $this->ossClient->signUrl($this->bucket, $fullFileName, $expires); return $signUrl; } public function replaceUrl($url) { return str_replace($this->aliHost, $this->myHost, $url); }}
华为 OBS 实现
<?phpnamespace App\Service\OSS;use Obs\ObsClient;class HuaweiOBS implements IOSS{ private $endPoint; private $key; private $secret; private $bucket; private $obsClient; private $expires = 3 * 24 * 3600; private $hwHost = ''; private $myHost = ''; public function __construct() { $this->endPoint = env("OSS_HW_ENDPOINT"); $this->key = env("OSS_HW_KEY"); $this->secret = env("OSS_HW_SECRET"); $this->bucket = env("OSS_HW_BUCKET"); try { $this->obsClient = new ObsClient(['key' => $this->key, 'secret' => $this->secret, 'endpoint' => $this->endPoint]); } catch (\Exception $e) { } } public function publicUpload($fullFileName, $filePath) { $res = $this->obsClient->putObject([ 'Bucket' => $this->bucket, 'Key' => $fullFileName, 'SourceFile' => $filePath ]); return $res; } public function getUrl($fullFileName, $expires) { $expires = $expires ? $expires : $this->expires; // 生成下载对象的带授权信息的URL $res = $this->obsClient->createSignedUrl([ 'Method' => 'GET', 'Bucket' => $this->bucket, 'Key' => $fullFileName, 'Expires' => $expires ]); return $res['SignedUrl']; } public function replaceUrl($url) { return str_replace($this->hwHost, $this->myHost, $url); }}
Demo: 业务逻辑 + OSS 类
<?phpnamespace App\Service;class UploadFile{ public static function upload($files, $prefix = '') { if (empty($files)) { return ['ok' => false, 'message' => '请上传文件!']; } if (is_array($files)) { $pics = []; foreach ($files as $key => $file) { if ($file->isValid()) { $name = $file->getClientOriginalName(); $fullName = OSS::getFullFileName($name, $prefix); $ret = OSS::publicUpload($fullName, $file, $prefix); if ($ret) { $url = OSS::getUrl($fullName); $url = OSS::replaceUrl($url); $pics[] = ['name' => $name, 'url' => $url, 'file_name' => $fullName]; } } else { return ['ok' => false, 'message' => '无效文件!']; } } if (count($pics) > 0) { return ['ok' => true, 'data' => $pics]; } } else { $name = $files->getClientOriginalName(); $fullName = OSS::getFullFileName($name, $prefix); $ret = OSS::publicUpload($fullName, $files, $prefix); if ($ret) { $url = OSS::getUrl($fullName); $url = OSS::replaceUrl($url); return ['ok' => true, 'data' => ['name' => $name, 'url' => $url, 'file_name' => $fullName]]; } else { return ['ok' => false, 'message' => '无效文件!']; } } }}
<?phpnamespace App\Service;use App\Service\OSS\AliOSS;use App\Service\OSS\HuaweiOBS;use Exception;class OSS{ const DEFAULT_DRIVER = 'HW_OBS'; const OSS_PREFIX = 'oss/'; public $OSSService; public function __construct() { if (env('OSS_DRIVER') === self::DEFAULT_DRIVER) { $this->OSSService = new HuaweiOBS(); } else { $this->OSSService = new AliOSS(); } } public static function getInstance() { return new self(); } public static function publicUpload($fullName, $filePath, $prefix) { return self::getInstance()->OSSService->publicUpload($fullName, $filePath); } public static function getUrl($fullName, $expires = '') { return self::getInstance()->OSSService->getUrl($fullName, $expires); } public static function replaceUrl($url) { return self::getInstance()->OSSService->replaceUrl($url); } public static function getFullFileName($fileName, $prefix) { return self::OSS_PREFIX . $prefix . self::setFileName($fileName); } public static function setFileName($fileName) { $nameArray = explode('.', $fileName); $extension = $nameArray[count($nameArray) - 1]; $newName = date('Ymd') . '/' . date('YmdHis') . rand(10000, 99999) . '.' . $extension; return $newName; }}
有时间可以对其进行功能接口补充,丰富更多云接口能力。
附:
composer 包:https://packagist.org/packages/league/flysystem
composer require league/flysystem
读到这里,这篇“OSS云环境文件上传与签名的代码怎么写”文章已经介绍完毕,想要掌握这篇文章的知识点还需要大家自己动手实践使用过才能领会,如果想了解更多相关内容的文章,欢迎关注编程网行业资讯频道。