Files
car_info/app/service/OssService.php
T

111 lines
4.1 KiB
PHP
Raw Normal View History

2025-04-30 11:30:08 +08:00
<?php
namespace app\service;
use OSS\OssClient;
use OSS\Core\OssException;
class OssService
{
private $accessKeyId;
private $accessKeySecret;
private $bucket;
private $endpoint;
private $ossClient;
public function __construct()
{
2025-04-30 14:45:58 +08:00
try {
$this->accessKeyId = env('OSS_ACCESS_KEY_ID');
$this->accessKeySecret = env('OSS_ACCESS_KEY_SECRET');
$this->bucket = env('OSS_BUCKET', 'vehicle-album');
$this->endpoint = env('OSS_ENDPOINT', 'https://oss-cn-beijing.aliyuncs.com');
// 记录配置信息
error_log("OSS Configuration:");
error_log("AccessKeyId: " . substr($this->accessKeyId, 0, 4) . "****");
error_log("Bucket: " . $this->bucket);
error_log("Endpoint: " . $this->endpoint);
$this->ossClient = new OssClient($this->accessKeyId, $this->accessKeySecret, $this->endpoint);
// 测试连接
$this->ossClient->doesBucketExist($this->bucket);
} catch (OssException $e) {
error_log("OSS Client Initialization Error: " . $e->getMessage());
throw new \RuntimeException('OSS Client Initialization Error: ' . $e->getMessage());
}
2025-04-30 11:30:08 +08:00
}
/**
* 上传图片到 OSS
* @param string $filePath 本地文件路径
* @param string $objectKey OSS上存储的对象名称
* @return string 图片的 URL
*/
public function uploadImage($filePath, $objectKey)
{
try {
// 上传文件到 OSS
$this->ossClient->uploadFile($this->bucket, $objectKey, $filePath);
// 构造图片的私有 URL
$url = $this->generatePrivateUrl($objectKey);
return $url;
} catch (OssException $e) {
// 错误处理
throw new \RuntimeException('OSS Error: ' . $e->getMessage());
}
}
/**
* 生成图片的私有访问 URL
* @param string $objectKey
* @return string
*/
public function generatePrivateUrl($objectKey, $expireTime = 3600)
{
2025-04-30 14:45:58 +08:00
try {
2025-04-30 15:18:02 +08:00
// 验证参数
if (empty($objectKey)) {
throw new \InvalidArgumentException('ObjectKey cannot be empty');
}
if ($expireTime <= 0) {
throw new \InvalidArgumentException('ExpireTime must be greater than 0');
}
2025-04-30 15:06:32 +08:00
// 记录更详细的调试信息
2025-04-30 14:45:58 +08:00
error_log("OSS URL Generation Debug:");
error_log("Bucket: " . $this->bucket);
error_log("ObjectKey: " . $objectKey);
error_log("ExpireTime: " . $expireTime);
2025-04-30 15:06:32 +08:00
error_log("Current Time: " . time());
error_log("Expiration Time: " . (time() + $expireTime));
error_log("AccessKeyId: " . substr($this->accessKeyId, 0, 4) . "****");
error_log("Endpoint: " . $this->endpoint);
2025-04-30 15:18:02 +08:00
// 检查对象是否存在
if (!$this->ossClient->doesObjectExist($this->bucket, $objectKey)) {
error_log("OSS Object does not exist: " . $objectKey);
throw new \RuntimeException('Object does not exist in OSS');
}
2025-04-30 15:06:32 +08:00
// 生成 OSS 私有 URL
$url = $this->ossClient->signUrl($this->bucket, $objectKey, $expireTime);
// 记录生成的 URL
2025-04-30 14:45:58 +08:00
error_log("Generated URL: " . $url);
2025-04-30 15:06:32 +08:00
2025-04-30 14:45:58 +08:00
return $url;
} catch (OssException $e) {
error_log("OSS Error: " . $e->getMessage());
2025-04-30 15:06:32 +08:00
error_log("Error Code: " . $e->getCode());
error_log("Error Trace: " . $e->getTraceAsString());
2025-04-30 15:18:02 +08:00
error_log("Request ID: " . $e->getRequestId());
2025-04-30 14:45:58 +08:00
throw new \RuntimeException('OSS Error: ' . $e->getMessage());
2025-04-30 15:18:02 +08:00
} catch (\Exception $e) {
error_log("General Error in generatePrivateUrl: " . $e->getMessage());
error_log("Error Trace: " . $e->getTraceAsString());
throw new \RuntimeException('Error generating private URL: ' . $e->getMessage());
2025-04-30 14:45:58 +08:00
}
2025-04-30 11:30:08 +08:00
}
}