config; $id = $config['storage_aliyun_accesskeyid']; $key = $config['storage_aliyun_accesskeysecret']; $bucket = $config['storage_aliyun_bucket']; $endpoint = $config['storage_aliyun_endpoint']; $host = "https://$bucket.$endpoint"; // CNAME别名 if (!empty($config['storage_aliyun_user_url'])) { $host = $config['storage_aliyun_transport_protocols'] . "://" . $config['storage_aliyun_user_url']; } //回调地址 //!$callbackUrl && $callbackUrl = Url::toFront(['storage/oss'], true); $callback_param = [ 'callbackUrl' => $callbackUrl, 'callbackBody' => 'filename=${object}&size=${size}&mimeType=${mimeType}&height=${imageInfo.height}&width=${imageInfo.width}&format=${imageInfo.format}&md5=${x:md5}&merchant_id=${x:merchant_id}&type=${x:type}&host=${x:host}&upload_id=${x:upload_id}', 'callbackBodyType' => "application/x-www-form-urlencoded" ]; $base64_callback_body = base64_encode(Json::encode($callback_param)); $expiration = $this->expiration(time() + $expire); // 最大文件大小 $conditions[] = ['content-length-range', 0, $maxSize]; // 表示用户上传的数据,必须是以$dir开始,不然上传会失败,这一步不是必须项,只是为了安全起见,防止用户通过policy上传到别人的目录。 // $conditions[] = ['starts-with','$filename', $dir]; $arr = [ 'expiration' => $expiration, 'conditions' => $conditions ]; $policy = Json::encode($arr); $base64_policy = base64_encode($policy); $signature = base64_encode(hash_hmac('sha1', $base64_policy, $key, true)); return [ 'Filename' => '${filename}', 'key' => $path . '${filename}', 'OSSAccessKeyId' => $id, 'success_action_status' => '201', 'host' => $host, 'policy' => $base64_policy, 'signature' => $signature, 'callback' => $base64_callback_body, 'x:type' => $type, 'x:host' => $host, ]; } /** * 截止日期 * * @param $time * @return string * @throws \Exception */ protected function expiration($time) { $dtStr = date("c", $time); $datatime = new \DateTime($dtStr); $expiration = $datatime->format(\DateTime::ISO8601); $pos = strpos($expiration, '+'); $expiration = substr($expiration, 0, $pos); return $expiration . "Z"; } /** * @param $baseInfo * @param $fullPath * @return mixed */ protected function baseUrl($baseInfo, $fullPath) { $user_url = $this->config['storage_aliyun_user_url']; if (!empty($user_url)) { $baseInfo['url'] = $this->config['storage_aliyun_transport_protocols'] . '://' . $user_url . '/' . $baseInfo['url']; } else { $raw = $this->adapter->supports->getFlashData(); $baseInfo['url'] = $raw['info']['url']; } return $baseInfo; } /** * @param $baseInfo * @param $fullPath * @return mixed */ protected function domainName($baseInfo, $fullPath) { $user_url = $this->config['storage_aliyun_user_url']; if (!empty($user_url)) { $baseInfo['domain_name'] = $this->config['storage_aliyun_transport_protocols'] . '://' . $user_url . '/'; } else { $raw = $this->adapter->supports->getFlashData(); $baseInfo['domain_name'] = $raw['info']['domain_name']; } return $baseInfo; } /** * @return mixed|void * @throws \Exception */ protected function create() { $this->adapter = new OssAdapter([ 'accessId' => $this->config['storage_aliyun_accesskeyid'], 'accessSecret' => $this->config['storage_aliyun_accesskeysecret'], 'bucket' => $this->config['storage_aliyun_bucket'], 'endpoint' => ($this->config['storage_aliyun_is_internal'] ?? false) == true ? $this->config['storage_aliyun_endpoint_internal'] : $this->config['storage_aliyun_endpoint'], // 'timeout' => 3600, // 'connectTimeout' => 10, // 'isCName' => false, // 'token' => '', ]); } }