文件上传、图片上传对接阿里云OSS

This commit is contained in:
柳清爽
2025-03-21 09:21:11 +08:00
parent 307096a509
commit cb67dd094b
5 changed files with 281 additions and 3 deletions

View File

@@ -0,0 +1,77 @@
<?php
namespace app\common\util;
use OSS\OssClient;
use OSS\Core\OssException;
class AliyunOSS
{
// OSS配置信息
const ACCESS_KEY_ID = 'your_access_key_id';
const ACCESS_KEY_SECRET = 'your_access_key_secret';
const ENDPOINT = 'oss-cn-hangzhou.aliyuncs.com';
const BUCKET = 'your_bucket_name';
/**
* 获取OSS客户端实例
* @return OssClient
* @throws OssException
*/
public static function getClient()
{
try {
return new OssClient(
self::ACCESS_KEY_ID,
self::ACCESS_KEY_SECRET,
self::ENDPOINT
);
} catch (OssException $e) {
throw new OssException('创建OSS客户端失败' . $e->getMessage());
}
}
/**
* 上传文件到OSS
* @param string $filePath 本地文件路径
* @param string $objectName OSS对象名称
* @return array
* @throws OssException
*/
public static function uploadFile($filePath, $objectName)
{
try {
$client = self::getClient();
// 上传文件
$result = $client->uploadFile(self::BUCKET, $objectName, $filePath);
// 获取文件访问URL
$url = $client->signUrl(self::BUCKET, $objectName, 3600);
return [
'success' => true,
'url' => $url,
'object_name' => $objectName,
'size' => filesize($filePath),
'mime_type' => mime_content_type($filePath)
];
} catch (OssException $e) {
return [
'success' => false,
'error' => $e->getMessage()
];
}
}
/**
* 生成OSS对象名称
* @param string $originalName 原始文件名
* @return string
*/
public static function generateObjectName($originalName)
{
$ext = pathinfo($originalName, PATHINFO_EXTENSION);
$name = md5(uniqid(mt_rand(), true));
return date('Y/m/d/') . $name . '.' . $ext;
}
}

View File

@@ -0,0 +1,65 @@
<?php
namespace app\common\util;
use Darabonba\OpenApi\Models\Config;
use AlibabaCloud\SDK\Dysmsapi\V20170525\Dysmsapi;
use AlibabaCloud\SDK\Dysmsapi\V20170525\Models\SendSmsRequest;
use AlibabaCloud\Tea\Utils\Utils\RuntimeOptions;
class AliyunSMS {
const ACCESS_KEY_ID = 'LTAI5tFjVRYAFmo6fayvv2Te';
const ACCESS_KEY_SECRET = 'mdc7KETPGVon8PiA2kfM47rAOpJne8';
const SIGN_NAME = '广州宏科网络';
const TC_VCODE = 'SMS_464445466';
static public function createClient() {
$config = new Config([
// AccessKey ID
'accessKeyId' => static::ACCESS_KEY_ID,
// AccessKey Secret
'accessKeySecret' => static::ACCESS_KEY_SECRET
]);
// 访问的域名
$config->endpoint = 'dysmsapi.aliyuncs.com';
return new Dysmsapi($config);
}
/**
* 发送验证码
*
* @param $phoneNumbers
* @param $templateCode
* @param array $templateParam
* @return bool
*/
static public function send($phoneNumbers, $templateCode, array $templateParam = array()) {
$client = static::createClient();
$sendSmsRequest = new SendSmsRequest([
'phoneNumbers' => $phoneNumbers,
'signName' => static::SIGN_NAME,
'templateCode' => $templateCode,
'templateParam' => json_encode($templateParam)
]);
$runtime = new RuntimeOptions([]);
$logFile = ROOT_PATH . DS . 'aliyun-sms.txt';
try {
$data = $client->sendSmsWithOptions($sendSmsRequest, $runtime);
if ($data->body->code === 'OK') {
return TRUE;
}
$logData = print_r($data, TRUE);
} catch (\Exception $ex) {
$logData = print_r($ex, TRUE);
}
file_put_contents($logFile, '[' . date('Y-m-d H:i:s') . ']' . PHP_EOL . $logData . PHP_EOL . PHP_EOL, FILE_APPEND);
return FALSE;
}
}