获取v2的swift的token
null Module_Storage_Driver_Swift::get_real_token_v2( )
protected function get_real_token_v2()
{
if (IS_DEBUG)Core::debug()->info($this->protocol .'://'. $this->account . '@' . $this->host . ($this->port!=80&&$this->port!=443?':'.$this->port:'') . '/' . $this->token_api_version .'/tokens', 'Swift get token url');
$fp = $this->fp($this->protocol, $this->host, $this->port, $this->timeout);
$body = json_encode(array
(
"auth" => array
(
"passwordCredentials" => array
(
'username' => $this->account,
'password' => $this->key,
),
"tenantName" => $this->tenant_name,
),
));
$headers = array
(
'Host' => $this->host,
'Content-Length' => strlen($body),
'Content-Type' => 'application/json',
'Accept' => 'application/json',
);
$message = $this->build_request_line('POST', '/'.$this->token_api_version .'/tokens') . $this->build_headers($headers) . $body;
fwrite($fp, $message);
$rs = $this->read($fp);
try
{
if ($rs['code']<200 || $rs['code']>=300)
{
throw new Exception(__('Swift get token error. Code: :code.', array(':code'=>$rs['code'])));
}
$body = @json_decode($rs['body'], true);
if (!$body || !is_array($body))
{
throw new Exception(__('Swift get token error. error body content.'));
}
# 获取token
if (isset($body['access']['token']))
{
$this->token = $body['access']['token']['id'];
$expires = strtotime($body['access']['token']['expires']);
if ($expires)
{
$expires = $expires - 20;
$this->token_timeout = $expires - time();
}
}
else
{
throw new Exception(__('Swift get token error.not found X-Auth-Token'));
}
if (IS_DEBUG)Core::debug()->info($this->token, 'Swift Token');
foreach ($body['access']['serviceCatalog'] as $item)
{
if ($item['type'] == 'object-store')
{
foreach ($item['endpoints'] as $item2)
{
if (!$this->region || $item2['region']==$this->region)
{
$this->storage_url = $item2['internalURL'];
$h = parse_url($this->storage_url);
$this->storage_host = $h['host'];
$this->storage_path = $h['path'];
$this->storage_protocol = $h['scheme'];
$this->storage_port = $h['port']?$h['port']:$h['scheme']=='https'?443:80;
break 2;
}
}
}
}
if ($this->storage_url)
{
$conection_hash1 = $this->get_connection_hash();
$conection_hash2 = $this->get_connection_hash(true);
if ($conection_hash1==$conection_hash2)
{
# 将连接加到$connections里复用
Storage_Driver_Swift::set_connection($conection_hash1, $fp);
}
else
{
# 不是同一个服务器,关闭token服务器连接
fclose($fp);
}
}
}
catch (Exception $e)
{
fclose($fp);
throw $e;
}
}