12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455 |
- <?php
- namespace App\Servers\Aliyun;
- use OSS\OssClient;
- use OSS\Core\OssException;
- use App\Facades\Servers\Logs\Log;
- use OSS\Credentials\StaticCredentialsProvider;
- /**
- * 阿里oss
- *
- * @author jun
- */
- class Oss {
- private \OSS\OssClient $ossClient;
- function __construct()
- {
- $accessKeyId = config('aliyun.accessKeyId','');
- $accessKeySecret = config('aliyun.accessKeySecret','');
- $provider = new StaticCredentialsProvider($accessKeyId,$accessKeySecret);
- $endpoint = "https://oss-cn-shenzhen.aliyuncs.com";
- $config = array(
- "provider" => $provider,
- "endpoint" => $endpoint,
- "signatureVersion" => OssClient::OSS_SIGNATURE_VERSION_V4,
- "region"=> "cn-shenzhen"
- );
- $this->ossClient = new OssClient($config);
- }
- /**
- * 使用签名URL上传
- * @param string bucket 存储空间名称
- * @param string object 不包含Bucket名称在内的Object完整路径
- *
- * */
- function signUrl($bucket, $object, $options=['Content-Type'=>'multipart/form-data'] )
- {
- // 指定签名URL的过期时间为600s(最长可达32400s)。
- $timeout = 600;
- try {
- // 生成签名URL。
- $signedUrl = $this->ossClient->signUrl($bucket, $object, $timeout, "PUT",$options);
- // 返回结果
- return $signedUrl;
- } catch (OssException $e) {
- Log::error('oss/signUrl','获取签名URL上传失败,错误:'.json_encode($e->getMessage()));
- return ['error'=>$e->getMessage()];
- }
- }
-
- }
|